package kr.co.hanwha.hifive.screen.intro;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.dialog.HFDefaultDialog;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.screen.intro.contract.AuthNumberContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.view.HFNumberKeypadView;
import kr.co.hanwha.hifive.view.HFPasswordEditTextView;

/**
 * 간편 로그인 설정
 *
 * @see AuthNumberPresenter
 * <p>
 * Created by sanghoonkwon on 2018. 2. 7..
 */

public class AuthNumberActivity extends BaseActivity implements AuthNumberContract.View {
    private AuthNumberPresenter mPresenter;

    private HFPasswordEditTextView mPasswordEdit;
    private HFNumberKeypadView mNumberKeypadView;
    private TextView mTextViewAlert;

    private boolean isRepeat = false;
    private String mPassword;

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_auth_number;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return null;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return BaseAnimationType.RIGHT;
    }


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

        mPasswordEdit = (HFPasswordEditTextView) findViewById(R.id.passwordEditText);
        mPasswordEdit.setOnInputPasswordListener(onInputPasswordListener);
//        mPasswordEdit.setFocus();

        mNumberKeypadView = (HFNumberKeypadView) findViewById(R.id.numberKeypadView);
        mNumberKeypadView.setOnNumberKeypadListener(onNumberKeypadListener);

        mTextViewAlert = (TextView) findViewById(R.id.alert_textView);

        isRepeat = getIntent().getBooleanExtra(HFActivityUtil.INTENT_AUTH_NUMBER_REPEAT, false);
        mPassword = getIntent().getStringExtra(HFActivityUtil.INTENT_AUTH_NUMBER);

        findViewById(R.id.back_imageButton).setOnClickListener(onClickListener);

        mPresenter = new AuthNumberPresenter(this);

        setLayout();
    }

    @Override
    public void showAlertMessage(String message) {
        if (TextUtils.isEmpty(message)) mTextViewAlert.setText("");
        else {
            mTextViewAlert.setText(message);
            mPasswordEdit.postDelayed(new Runnable() {//자연스럽게 보이게 하기 위해 딜레이 추가
                @Override
                public void run() {
                    mPasswordEdit.clearData();
                }
            }, 300);
        }
    }

    private void setLayout() {
        TextView textViewGuide = (TextView) findViewById(R.id.guide_textView);
        if (isRepeat) {
            textViewGuide.setText(getString(R.string.auth_number_guide_2));
            findViewById(R.id.back_imageButton).setVisibility(View.VISIBLE);
        } else {
            textViewGuide.setText(getString(R.string.auth_number_guide_1));
        }
    }

    @Override
    public void onBackPressed() {
        if (isRepeat == false) {
            showAlert(getString(R.string.alert_finish_title), getString(R.string.alert_finish_message),
                    new HFDefaultDialog.OnDialogCallback() {
                        @Override
                        public void onNegative() {

                        }

                        @Override
                        public void onPositive() {
                            HFActivityUtil.startRootActivity(AuthNumberActivity.this, true);
                            finish();
                        }
                    });
        } else {
            super.onBackPressed();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == HFActivityUtil.REQUEST_SET_EASYLOGIN_REPEAT) {
            if (resultCode == RESULT_OK) {
                //ADD MESSAGE - 간편 로그인 설정 완료시 메세지 필요

                showToastConfirm(R.string.auth_number_login_completed);
                HFActivityUtil.startMainActivity(this);
                finish();
            } else {
                mPasswordEdit.setFocus();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.back_imageButton:
                    finish();
                    break;
            }
        }
    };

    /**************************************************
     * NumberKeypadView.OnNumberKeypadListener
     **************************************************/
    HFNumberKeypadView.OnNumberKeypadListener onNumberKeypadListener = new HFNumberKeypadView.OnNumberKeypadListener() {
        @Override
        public void onNumberClicked(int num) {
            mPasswordEdit.setNum(num);
        }

        @Override
        public void onDeleteClicked() {
            mPasswordEdit.delNum();
        }

        @Override
        public void easterEgg() {
//            new PasswordDialog(AuthNumberActivity.this, new PasswordDialog.OnDialogCallback() {
//                @Override
//                public void onPositive(@NonNull String message) {
//                    GWActivityUtil.startSettingAdminActivity(AuthNumberActivity.this);
//                }
//            }).show();
        }
    };

    /**************************************************
     * PasswordEditTextView.OnInputPasswordListener
     **************************************************/
    HFPasswordEditTextView.OnInputPasswordListener onInputPasswordListener = new HFPasswordEditTextView.OnInputPasswordListener() {
        @Override
        public void onCompleteInput(String password) {
            if (isRepeat) {
                if (mPasswordEdit.getText().equals(mPassword)) {
                    mPresenter.doUpdateAuthcode(mPassword);
                } else {
                    showAlertMessage(getString(R.string.auth_number_not_match));
                }
            } else {
                HFActivityUtil.startSetEasyLoginRepeatActivity(AuthNumberActivity.this, mPasswordEdit.getText());
            }
        }

        @Override
        public void onErrorMessage(String message) {
            showToast(message);
        }

        @Override
        public void onTextChanged() {
            if (mPasswordEdit.getText().length() > 0)
                showAlertMessage("");
        }
    };
}


