package net.micode.notes.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.model.WorkingNote;

public class PasswordLockActivity extends Activity {

    private static final String TAG ="PassewordLockActivity";

    private Button mbutton;

    private WorkingNote mWorkingNote;

    private String inputPwd;

    private final static int REQUEST_CODE_OPEN_NODE = 102;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.passwordlock_note);

        getWindow().setBackgroundDrawableResource(R.drawable.password_lock_background);

        long id = getIntent().getLongExtra(Intent.EXTRA_UID,0);

        mWorkingNote = WorkingNote.load(this,id);
        if (mWorkingNote == null) {
            Log.e(TAG, "load note failed with note id" + id);
            finish();
            return;
        }
        mbutton = (Button) findViewById(R.id.password_button_note);
        mbutton.setOnClickListener((l)->{
            testPassword(mWorkingNote.getNoteId());
        });

    }

    private void openNote(long id) {
        Intent intent = new Intent(this, NoteEditActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra("password",inputPwd);
        intent.putExtra(Intent.EXTRA_UID, id);
        this.startActivityForResult(intent, REQUEST_CODE_OPEN_NODE);
    }

    public void testPassword(long id){

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_pwd, null);
        final EditText etPwd = (EditText) view.findViewById(R.id.set_private_pwd);
        final ToggleButton passwordVisibleToggleButton = (ToggleButton) view.findViewById(R.id.password_visible_toggle_button);
        passwordVisibleToggleButton.setOnClickListener(v -> {
            if (passwordVisibleToggleButton.isChecked()) {
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });

        showSoftInput();
        builder.setPositiveButton(android.R.string.ok, null);
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                hideSoftInput(etPwd);
            }
        });
        final Dialog dialog = builder.setView(view).show();

        final Button positive = (Button) dialog.findViewById(android.R.id.button1);
        positive.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                hideSoftInput(etPwd);
                String Password = etPwd.getText().toString();
                if (!TextUtils.isEmpty(Password)) {
                    inputPwd = Password;
                    openNote(id);
                }
                dialog.dismiss();
            }
        });
        if (TextUtils.isEmpty(etPwd.getText())) {
            positive.setEnabled(false);
        }

        etPwd.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(etPwd.getText())) {
                    positive.setEnabled(false);
                } else {
                    positive.setEnabled(true);
                }
            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }
        });
    }

    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(this, NotesListActivity.class);
        startActivity(intent);
    }

    private void hideSoftInput(View view) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    private void showSoftInput() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }
    }
}