package ie.yesequality.yesequality;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class NotificationActivity extends Activity {

    private static final String PREFS = "REMINDERS";
    public static final String ON_DAY = "on_day";
    public static final String ON_DAY_BEFORE = "on_day_before";
    @InjectView(R.id.switchDayBefore) Switch dayBefore;
    @InjectView(R.id.switchOnDay) Switch onDay;
    @InjectView(R.id.okButton) Button okButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        ButterKnife.inject(this);

        final SharedPreferences notificationPrefs = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = notificationPrefs.edit();
        onDay.setChecked(notificationPrefs.getBoolean(ON_DAY, true));
        dayBefore.setChecked(notificationPrefs.getBoolean(ON_DAY_BEFORE, true));

        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        onDay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                editor.putBoolean(ON_DAY, isChecked);
                editor.commit();
                if (isChecked){
                    registerAlarm(true);
                }
                else {
                    cancelAlarm(true);
                }
            }
        });

        dayBefore.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                editor.putBoolean(ON_DAY_BEFORE, isChecked);
                editor.commit();
                if (isChecked){
                    registerAlarm(false);
                }
                else {
                    cancelAlarm(false);
                }
            }
        });

    }

    private void registerAlarm(boolean onDay) {
        if (onDay){
            // Register alarm for voting day
            Log.d("NotificationActivity", "Registering Alarm for Voting day");
        }
        else {
            // Register alarm for day before voting day
            Log.d("NotificationActivity", "Registering Alarm for day before Voting day");

        }
    }

    private void cancelAlarm(boolean onDay) {
        if (onDay){
            // Canceling alarm for voting day
            Log.d("NotificationActivity", "Canceling Alarm for Voting day");
        }
        else {
            // Canceling alarm for day before voting day
            Log.d("NotificationActivity", "Canceling Alarm for day before Voting day");

        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_notification, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -244,64 +244,8 @@
 em;%0A
-import android.view.View;%0Aimport android.widget.Button;%0A
 impo
@@ -278,16 +278,16 @@
 Button;%0A
+
 import a
@@ -372,16 +372,44 @@
 ctView;%0A
+import butterknife.OnClick;%0A
 %0A%0Apublic
@@ -727,31 +727,29 @@
  onDay;%0A
+%0A
     @
-InjectView
+OnClick
 (R.id.ok
@@ -759,25 +759,64 @@
 ton)
- Button okButton;
+%0A    public void closeScreen() %7B%0A        finish();%0A    %7D
 %0A%0A  
@@ -1352,190 +1352,8 @@
 );%0A%0A
-        okButton.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                finish();%0A            %7D%0A        %7D);%0A%0A
     
@@ -2324,25 +2324,24 @@
         %7D);%0A
-%0A
     %7D%0A%0A    p
@@ -2690,33 +2690,32 @@
 e Voting day%22);%0A
-%0A
         %7D%0A    %7D%0A
@@ -2978,32 +2978,32 @@
 fore voting day%0A
+
             Log.
@@ -3074,17 +3074,16 @@
  day%22);%0A
-%0A
         

refactor to use @OnClick from butterknife