package ch.ihdg.calendarcolor;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.SaturationBar;
import com.larswerkman.holocolorpicker.ValueBar;

public class ColorPickerActivity extends Activity {

    static final String ARG_NAME = "arg_name";
    static final String ARG_ID = "arg_id";
    static final String ARG_COLOR = "arg_color";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_picker);

        int color = getIntent().getIntExtra(ARG_COLOR, 0);
        String name = getIntent().getStringExtra(ARG_NAME);
        final int cal_id = getIntent().getIntExtra(ARG_ID, 0);

        setTitle( name );

        final ColorPicker picker = (ColorPicker) findViewById(R.id.picker);
        SaturationBar saturationBar = (SaturationBar) findViewById(R.id.saturationbar);
        ValueBar valueBar = (ValueBar) findViewById(R.id.valuebar);

        picker.addSaturationBar(saturationBar);
        picker.addValueBar(valueBar);

        picker.setColor(color);
        picker.setOldCenterColor(color);

        //write color hex code in text field
        final EditText hexText = (EditText) findViewById(R.id.hexText);
        String red = String.format("%02x", (color >> 16) & 0xFF ),
               green = String.format("%02x", (color >> 8) & 0xFF),
               blue = String.format("%02x", color & 0xFF);
        hexText.setText( red + green + blue );
        findViewById(R.id.dummy).requestFocus();

        //update hex value when color changes
        picker.setOnColorChangedListener( new ColorPicker.OnColorChangedListener() {
            @Override
            public void onColorChanged(int color) {
                //write color hex code in text field
                String hex = String.format("%02x", (color >> 16) & 0xFF ) +
                             String.format("%02x", (color >> 8) & 0xFF) +
                             String.format("%02x", color & 0xFF);
                if(!hexText.getText().toString().equals(hex)) {
                    hexText.setText( hex );
                }
            }
        });

        //update color picker when a valid hex code is entered
        hexText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {}

            @Override
            public void afterTextChanged(Editable s) {
                if( s.length() == 6 )
                {
                    try{
                        int color = Color.parseColor('#' + s.toString());
                        if( picker.getColor() != color ) {
                            picker.setColor( color );
                        }
                        hexText.setTextColor( Color.BLACK );
                    }
                    catch ( IllegalArgumentException e ) {
                        //ignore
                        hexText.setTextColor( Color.RED );
                    }
                }
            }
        });

        final Button buttoncancel = (Button) findViewById(R.id.buttoncancel);
        buttoncancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        final Button buttonsave = (Button) findViewById(R.id.buttonsave);
        buttonsave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentValues values = new ContentValues();
                // set the new color for the calendar
                values.put(CalendarContract.Calendars.CALENDAR_COLOR, picker.getColor());
                Uri updateUri = ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI, cal_id);
                getContentResolver().update(updateUri, values, null, null);

                finish();
            }
        });
    }
}

@@ -750,16 +750,38 @@
 _color%22;
+%0A    int currentColor;
 %0A%0A    @O
@@ -950,29 +950,32 @@
 );%0A%0A        
-int c
+currentC
 olor = getIn
@@ -1500,24 +1500,31 @@
 r.setColor(c
+urrentC
 olor);%0A     
@@ -1552,16 +1552,23 @@
 rColor(c
+urrentC
 olor);%0A%0A
@@ -1721,32 +1721,39 @@
 ormat(%22%2502x%22, (c
+urrentC
 olor %3E%3E 16) & 0x
@@ -1797,32 +1797,39 @@
 ormat(%22%2502x%22, (c
+urrentC
 olor %3E%3E 8) & 0xF
@@ -1869,32 +1869,39 @@
 format(%22%2502x%22, c
+urrentC
 olor & 0xFF);%0A  
@@ -2049,26 +2049,34 @@
         
-p
+final ColorP
 icker.
-set
 OnColorC
@@ -2089,17 +2089,38 @@
 Listener
-(
+ colorPickerListener =
  new Col
@@ -2225,24 +2225,96 @@
 nt color) %7B%0A
+                //store new color%0A                currentColor = color;%0A
             
@@ -2711,32 +2711,96 @@
      %7D%0A        %7D
+;%0A        picker.setOnColorChangedListener( colorPickerListener 
 );%0A%0A        //up
@@ -3428,24 +3428,120 @@
 = color ) %7B%0A
+                            //Do not update hex field from color picker, after typing the value%0A
             
@@ -3570,19 +3570,220 @@
 .set
+On
 Color
-( colo
+ChangedListener(null);%0A                            picker.setColor(color);%0A                            currentColor = color;%0A                            picker.setOnColorChangedListener( colorPickerListene
 r );
@@ -4769,33 +4769,28 @@
 _COLOR, 
-picker.ge
+curren
 tColor
-()
 );%0A     

fix color inaccuracy when using hex input