package kr.co.hanwha.hifive.admin;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.util.HFCryptoUtil;

/**
 * Created by sanghoonkwon on 2018. 2. 13..
 */

public class AdminInputDialog extends Dialog {
    public enum ADMIN_TYPE {
        PASSWORD,
        URL,
        RESET_PASSWORD,
        TASK_ID
    }

    private ADMIN_TYPE mAdminType;
    private String mMessage;
    private EditText mEditTextMessage;

    private OnDialogCallback mCallback;

    public AdminInputDialog(@NonNull Context context) {
        super(context);
    }

    public AdminInputDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    protected AdminInputDialog(@NonNull Context context, boolean cancelable, @Nullable DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public AdminInputDialog(@NonNull Context context, @NonNull ADMIN_TYPE type, @Nullable OnDialogCallback callback) {
        super(context, R.style.DialogFadeAnim);
        setCancelable(false);
        this.mAdminType = type;
        this.mCallback = callback;
    }

    public AdminInputDialog(@NonNull Context context, @NonNull ADMIN_TYPE type, @Nullable String message, @Nullable OnDialogCallback callback) {
        super(context, R.style.DialogFadeAnim);
        setCancelable(false);
        this.mAdminType = type;
        this.mMessage = message;
        this.mCallback = callback;
    }

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

        setContentView(R.layout.dialog_admin_input);

        TextView textViewTitle = (TextView) findViewById(R.id.title_textView);
        TextView textViewPositive = (TextView) findViewById(R.id.positive_textView);
        mEditTextMessage = (EditText) findViewById(R.id.message_editText);
        mEditTextMessage.setOnEditorActionListener(onEditorActionListener);

        if (mAdminType == ADMIN_TYPE.PASSWORD) {
            textViewTitle.setText(getContext().getString(R.string.login_alert_empty_pw));
        } else if (mAdminType == ADMIN_TYPE.URL) {
            textViewTitle.setText("URL을 입력하세요");
        } else if (mAdminType == ADMIN_TYPE.RESET_PASSWORD) {
            textViewTitle.setText("변경할 비밀번호를 입력해 주세요.");
        } else if (mAdminType == ADMIN_TYPE.TASK_ID) {
            textViewTitle.setText("TASK ID 를 입력해 주세요.");
        }

        if (TextUtils.isEmpty(mMessage) == false) {
            mEditTextMessage.setText(mMessage);
            mEditTextMessage.setSelection(mMessage.length());
        }

        textViewPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClicked();
            }
        });

        findViewById(R.id.close_imageButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AdminInputDialog.this.dismiss();
            }
        });
    }

    private void onClicked() {
        if (mCallback != null) {
            if (mAdminType == ADMIN_TYPE.PASSWORD) {
                String message = mEditTextMessage.getText().toString();
                String enc = HFCryptoUtil.getSHA256EncryptionData(message);
                if ("da7f750650543630536afb5f3b33b205e6b43c9c5c1373c7b388a8e5612867b1".equals(enc)) {
                    mCallback.onPositive("SUPER");
                } else if ("67601989ca65ab53739888dbebc75effdd667ea6137e12bef95f34fc327fe56b".equals(enc)) {
                    mCallback.onPositive("ADMIN");
                } else if ("ab9e11e0f95358e962a50ca0f7703e75fc69e47d4ba486fb5f61c0fab27093f1".equals(enc)) {
                    mCallback.onPositive("DESIGN");
                } else {
                    mCallback.onWrongPassword();
                }
            } else if (mAdminType == ADMIN_TYPE.URL) {
                mCallback.onPositive(mEditTextMessage.getText().toString());
            } else if (mAdminType == ADMIN_TYPE.RESET_PASSWORD) {
                mCallback.onPositive(mEditTextMessage.getText().toString());
            } else if (mAdminType == ADMIN_TYPE.TASK_ID) {
                mCallback.onPositive(mEditTextMessage.getText().toString().trim());
            }

        }
        AdminInputDialog.this.dismiss();
    }

    /**************************************************
     * TextView.OnEditorActionListener
     **************************************************/
    TextView.OnEditorActionListener onEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                onClicked();
            }
            return false;
        }
    };

    /**************************************************
     * Callback
     **************************************************/
    public interface OnDialogCallback {
        void onPositive(@NonNull String message);

        void onWrongPassword();
    }
}