package com.cpm.lorealbaautomation.password;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.cpm.lorealbaautomation.Database.Lorealba_Database;
import com.cpm.lorealbaautomation.DealarBoardActivity;
import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.UserLoginActivity;
import com.cpm.lorealbaautomation.blurlockview.BlurLockView;
import com.cpm.lorealbaautomation.blurlockview.Directions.HideType;
import com.cpm.lorealbaautomation.blurlockview.Directions.ShowType;
import com.cpm.lorealbaautomation.blurlockview.Eases.EaseType;
import com.cpm.lorealbaautomation.blurlockview.Password;
import com.cpm.lorealbaautomation.constant.CommonString;
import com.cpm.lorealbaautomation.dailyactivity.StoreImageActivity;
import com.cpm.lorealbaautomation.delegates.PrefHelper;
import com.cpm.lorealbaautomation.gettersetter.LoginGetterSetter;

import java.util.ArrayList;


public class MPinActivity extends AppCompatActivity implements
        BlurLockView.OnPasswordInputListener,
        BlurLockView.OnLeftButtonClickListener {
    BlurLockView blurLockView;
    ImageView imageView1;
    String pin = "";
    boolean IS_PASSWORD_CHECK;
    private SharedPreferences preferences = null;
    private SharedPreferences.Editor editor = null;
    private Dialog dialog;
    String user_id;
    Lorealba_Database db;
    String visit_date;
    ArrayList<LoginGetterSetter> allLoginData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_mpin);
        db = new Lorealba_Database(this);
        db.open();
        preferences = PrefHelper.getPrefs(this);
        editor = preferences.edit();
        visit_date = preferences.getString(CommonString.KEY_DATE, "");
        IS_PASSWORD_CHECK = getIntent().getBooleanExtra(CommonString.IS_PASSWORD_CHECK, false);
        if (getIntent().getSerializableExtra(CommonString.KEY_USERNAME) != null) {
            user_id = getIntent().getStringExtra(CommonString.KEY_USERNAME);

        }

        imageView1 = (ImageView) findViewById(R.id.image_1);

        blurLockView = (BlurLockView) findViewById(R.id.blurlockview);

        preferences = PrefHelper.getPrefs(this);
        editor = preferences.edit();

        // Set the view that need to be blurred
        blurLockView.setBlurredView(imageView1);
        allLoginData = db.getlistAllData(visit_date, user_id);

        // Set the password
        if (IS_PASSWORD_CHECK) {

           // String mpin = preferences.getString(CommonString.MPIN, null);
            String mpin = preferences.getString(user_id, null);
            blurLockView.setCorrectPassword(mpin);
            blurLockView.setLeftButton("Forgot MPin");
            blurLockView.setTitle("Please Enter Four Digit MPin");
        } else {
            blurLockView.setCorrectPassword("abcd");
            blurLockView.setLeftButton("Set MPin");
            blurLockView.setTitle("Please Set Four Digit MPin");
        }

        blurLockView.setIs_Password_Check_Mode(IS_PASSWORD_CHECK);




        blurLockView.setRightButton("Clear");
        blurLockView.setTypeface(getTypeface());
        blurLockView.setOnLeftButtonClickListener(this);
        blurLockView.setOnPasswordInputListener(this);

        //blurLockView.setType(getPasswordType(), false);
        //blurLockView.setPasswordLength(4);

       /* blurLockView.show(
                getIntent().getIntExtra("SHOW_DURATION", 1000),
                getShowType(getIntent().getIntExtra("SHOW_DIRECTION", 0)),
                getEaseType(getIntent().getIntExtra("SHOW_EASE_TYPE", 30)));*/
        blurLockView.setType(Password.NUMBER, true);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    private Password getPasswordType() {
        if ("PASSWORD_NUMBER".equals(getIntent().getStringExtra("PASSWORD_TYPE")))
            return Password.NUMBER;
        else if ("PASSWORD_NUMBER".equals(getIntent().getStringExtra("PASSWORD_TYPE")))
            return Password.TEXT;
        return Password.NUMBER;
    }

    private Typeface getTypeface() {
        if ("SAN".equals(getIntent().getStringExtra("TYPEFACE")))
            return Typeface.createFromAsset(getAssets(), "fonts/San Francisco Regular.ttf");
        else if ("DEFAULT".equals(getIntent().getStringExtra("TYPEFACE")))
            return Typeface.DEFAULT;
        return Typeface.DEFAULT;
    }

    @Override
    public void correct(String inputPassword) {

        if (IS_PASSWORD_CHECK) {
         //   Intent in = new Intent(getApplicationContext(), AutoLoginActivity.class);
            editor.putString(CommonString.KEY_USERNAME, user_id.toLowerCase());
            editor.commit();


           /* db.open();
            db.updateLoginMpnData(user_id, pin);
            Intent in = new Intent(getApplicationContext(), StoreImageActivity.class);
            startActivity(in);*/

           if (allLoginData.size()> 0 && !allLoginData.get(0).getImg().equals("")){
               Intent in = new Intent(getApplicationContext(), DealarBoardActivity.class);
               startActivity(in);
               finish();
           }else {
               Intent in = new Intent(getApplicationContext(), StoreImageActivity.class);
               startActivity(in);
               finish();
           }

        } else {
            Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void incorrect(String inputPassword) {

        if (IS_PASSWORD_CHECK) {

            int incorrect_times = blurLockView.getIncorrectInputTimes();
            if (++incorrect_times >= 3) {
                /*Toast.makeText(this,
                        R.string.error_incorrect_password,
                        Toast.LENGTH_SHORT).show();*/

                Snackbar snackbar = Snackbar.make(blurLockView, "Incorrect MPin limit reached", Snackbar.LENGTH_INDEFINITE);
                View view = snackbar.getView();
                /*TextView tv = (TextView)view.findViewById(android.support.design.R.id.snackbar_text);
                tv.setTextColor(Color.RED);*/
                snackbar.setAction("Reset", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        editor.putString(user_id, null);
                        editor.commit();
                        finish();
                        Intent in = new Intent(getApplicationContext(), UserLoginActivity.class);
                        startActivity(in);

                    }
                });
                snackbar.setActionTextColor(Color.GREEN);
                snackbar.show();
            } else {

                int count = 3 - incorrect_times;
                String attemt_str;
                if (count == 1) {
                    attemt_str = ". Attempt left - " + count;
                } else {
                    attemt_str = ". Attempts left - " + count;
                }

                String msg = getString(R.string.error_incorrect_pin) + attemt_str;
                Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
            }

        } else {
            pin = inputPassword;
        }

    }

    @Override
    public void input(String inputPassword) {
        pin = inputPassword;
    }

    @Override
    public void clear(String remainingPassword) {
        pin = remainingPassword;
    }


    @Override
    public void onClick() {
        if (IS_PASSWORD_CHECK) {
            Snackbar snackbar = Snackbar.make(blurLockView, "Need to reset MPin", Snackbar.LENGTH_INDEFINITE);
            View view = snackbar.getView();
                /*TextView tv = (TextView)view.findViewById(android.support.design.R.id.snackbar_text);
                tv.setTextColor(Color.RED);*/
            snackbar.setAction("Reset", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    editor.putString(user_id, null);
                    editor.commit();
                    finish();
                    Intent in = new Intent(getApplicationContext(), UserLoginActivity.class);
                    startActivity(in);

                }
            });
            snackbar.setActionTextColor(Color.GREEN);
            snackbar.show();
        } else {
            if (pin.length() == 4) {
              //  editor.putString(CommonString.MPIN, pin);
                editor.putString(user_id, pin);
                editor.putString(CommonString.KEY_USERNAME, user_id.toLowerCase());
                editor.commit();
               // Intent in = new Intent(getApplicationContext(), OneQADActivity.class);
                db.open();
                db.updateLoginMpnData(user_id, pin);
                //Intent in = new Intent(getApplicationContext(), DealarBoardActivity.class);
                Intent in = new Intent(getBaseContext(), StoreImageActivity.class);
                startActivity(in);
                finish();
            } else {
                Toast.makeText(this, "Please fill four digit MPin", Toast.LENGTH_SHORT).show();
            }
        }

    }


    private int downsamepleFactor;

    private ShowType getShowType(int p) {
        ShowType showType = ShowType.FROM_TOP_TO_BOTTOM;
        switch (p) {
            case 0:
                showType = ShowType.FROM_TOP_TO_BOTTOM;
                break;
            case 1:
                showType = ShowType.FROM_RIGHT_TO_LEFT;
                break;
            case 2:
                showType = ShowType.FROM_BOTTOM_TO_TOP;
                break;
            case 3:
                showType = ShowType.FROM_LEFT_TO_RIGHT;
                break;
            case 4:
                showType = ShowType.FADE_IN;
                break;
        }
        return showType;
    }

    private HideType getHideType(int p) {
        HideType hideType = HideType.FROM_TOP_TO_BOTTOM;
        switch (p) {
            case 0:
                hideType = HideType.FROM_TOP_TO_BOTTOM;
                break;
            case 1:
                hideType = HideType.FROM_RIGHT_TO_LEFT;
                break;
            case 2:
                hideType = HideType.FROM_BOTTOM_TO_TOP;
                break;
            case 3:
                hideType = HideType.FROM_LEFT_TO_RIGHT;
                break;
            case 4:
                hideType = HideType.FADE_OUT;
                break;
        }
        return hideType;
    }

    private EaseType getEaseType(int p) {
        EaseType easeType = EaseType.Linear;
        switch (p) {
            case 0:
                easeType = EaseType.EaseInSine;
                break;
            case 1:
                easeType = EaseType.EaseOutSine;
                break;
            case 2:
                easeType = EaseType.EaseInOutSine;
                break;
            case 3:
                easeType = EaseType.EaseInQuad;
                break;
            case 4:
                easeType = EaseType.EaseOutQuad;
                break;
            case 5:
                easeType = EaseType.EaseInOutQuad;
                break;
            case 6:
                easeType = EaseType.EaseInCubic;
                break;
            case 7:
                easeType = EaseType.EaseOutCubic;
                break;
            case 8:
                easeType = EaseType.EaseInOutCubic;
                break;
            case 9:
                easeType = EaseType.EaseInQuart;
                break;
            case 10:
                easeType = EaseType.EaseOutQuart;
                break;
            case 11:
                easeType = EaseType.EaseInOutQuart;
                break;
            case 12:
                easeType = EaseType.EaseInQuint;
                break;
            case 13:
                easeType = EaseType.EaseOutQuint;
                break;
            case 14:
                easeType = EaseType.EaseInOutQuint;
                break;
            case 15:
                easeType = EaseType.EaseInExpo;
                break;
            case 16:
                easeType = EaseType.EaseOutExpo;
                break;
            case 17:
                easeType = EaseType.EaseInOutExpo;
                break;
            case 18:
                easeType = EaseType.EaseInCirc;
                break;
            case 19:
                easeType = EaseType.EaseOutCirc;
                break;
            case 20:
                easeType = EaseType.EaseInOutCirc;
                break;
            case 21:
                easeType = EaseType.EaseInBack;
                break;
            case 22:
                easeType = EaseType.EaseOutBack;
                break;
            case 23:
                easeType = EaseType.EaseInOutBack;
                break;
            case 24:
                easeType = EaseType.EaseInElastic;
                break;
            case 25:
                easeType = EaseType.EaseOutElastic;
                break;
            case 26:
                easeType = EaseType.EaseInOutElastic;
                break;
            case 27:
                easeType = EaseType.EaseInBounce;
                break;
            case 28:
                easeType = EaseType.EaseOutBounce;
                break;
            case 29:
                easeType = EaseType.EaseInOutBounce;
                break;
            case 30:
                easeType = EaseType.Linear;
                break;
        }
        return easeType;
    }

    /*public void showCommentforGrooming(){

        dialog = new Dialog(MPinActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(R.layout.comment_layout);
        TextView tv = (TextView) dialog.findViewById(R.id.tv_comment);

        tv.setText("");

        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();

        wlp.gravity = Gravity.TOP;
        //wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(wlp);

        dialog.show();

    }*/

    /**
     * Represents an asynchronous login/registration task used to authenticate
     * the user.
     */

}
