package com.jmartin.writeily;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;

import com.jmartin.writeily.model.Constants;

/**
 * Created by jeff on 2014-08-20.
 */
public class PinActivity extends Activity {

    private Context context;
    private String pin;

    private EditText pin1;
    private EditText pin2;
    private EditText pin3;
    private EditText pin4;

    private boolean isSettingUp = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Get the Intent (to check if coming from Settings)
        String action = getIntent().getAction();

        if (action.equalsIgnoreCase(Constants.SET_PIN_ACTION)) {
            isSettingUp = true;
        } else {
            checkIfPinRequired();
        }

        setContentView(R.layout.activity_pin);
        context = getApplicationContext();

        // Find pin EditTexts
        pin1 = (EditText) findViewById(R.id.pin1);
        pin2 = (EditText) findViewById(R.id.pin2);
        pin3 = (EditText) findViewById(R.id.pin3);
        pin4 = (EditText) findViewById(R.id.pin4);

        attachPinListeners();

        pin = PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.USER_PIN_KEY, "");

        super.onCreate(savedInstanceState);
    }

    @Override
    public void onBackPressed() {
        if (isSettingUp) {
            setResult(RESULT_CANCELED);
        }

        super.onBackPressed();
    }

    private void attachPinListeners() {
        // Pin 1
        pin1.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 editable) {
                checkPin();
                pin2.requestFocus();
            }
        });

        // Pin 2
        pin2.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 editable) {
                checkPin();
                pin3.requestFocus();
            }
        });

        // Pin 3
        pin3.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 editable) {
                checkPin();
                pin4.requestFocus();
            }
        });

        // Pin 4
        pin4.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 editable) {
                checkPin();
            }
        });
    }

    private void checkPin() {
        String enteredPin = pin1.getText().toString() + pin2.getText().toString() + pin3.getText().toString() + pin4.getText().toString();

        if (isSettingUp) {
            // Check if the pin has 4 digits
            if (enteredPin.length() == 4) {
                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
                editor.putString(Constants.USER_PIN_KEY, enteredPin).apply();

                setResult(RESULT_OK);
                finish();
            }
        } else {
            // Check if we can unlock the app
            if (enteredPin.equalsIgnoreCase(pin)) {
                startMain();
            } else {
                if (enteredPin.length() == 4) {
                    Toast.makeText(context, getString(R.string.incorrect_pin_text), Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    private void checkIfPinRequired() {
        boolean pinRequired = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_pin", false);

        if (!pinRequired) {
            startMain();
        }
    }

    private void startMain() {
        Intent mainIntent = new Intent(this, MainActivity.class);
        startActivity(mainIntent);
    }
}

@@ -870,24 +870,173 @@
 tAction();%0A%0A
+        // Get the pin a user may have set%0A        pin = PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.USER_PIN_KEY, %22%22);%0A%0A
         if (
@@ -1543,114 +1543,8 @@
 );%0A%0A
-        pin = PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.USER_PIN_KEY, %22%22);%0A%0A
     
@@ -3679,32 +3679,319 @@
      %7D);%0A    %7D%0A%0A
+    /**%0A     * Checks and validates the pin that was entered with the pin in the SharedPreferences.%0A     * This process runs every time one of the four pin EditText fields is modified.%0A     * Once the entered pin matches the pin the user set, then redirect to the main activity.%0A     */%0A
     private void
@@ -4893,32 +4893,145 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Checks if the user enabled pin protection. If they didn't, redirect to the Main Activity.%0A     */%0A
     private void
@@ -5151,17 +5151,35 @@
 ean(
-%22
+getString(R.string.
 pref_pin
 %22, f
@@ -5174,17 +5174,21 @@
 pref_pin
-%22
+_key)
 , false)
@@ -5248,31 +5248,473 @@
 );%0A        %7D
-%0A    %7D%0A
+ else %7B%0A            // Check if user set up a pin but didn't enter one, in this case, set pin required to false%0A            if (pin.length() %3C 4) %7B%0A                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();%0A                editor.putBoolean(getString(R.string.pref_pin_key), false).apply();%0A                startMain();%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Start the main activity.%0A     */
 %0A    private

Fix bug when user sets up pin but doesn't finish