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

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
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.CheckAuthNumberContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;
import kr.co.hanwha.hifive.view.HFNumberKeypadView;
import kr.co.hanwha.hifive.view.HFPasswordEditTextView;

/**
 * 간편 인증 - 로그인 완료후 백그라운드에서 진입시
 * Created by sanghoonkwon on 2018. 2. 13..
 */

public class CheckAuthNumberActivity extends BaseActivity implements CheckAuthNumberContract.View {
    private CheckAuthNumberPresenter mPresenter;

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

    private String mPassword;

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

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

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return null;
    }

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

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

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

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

        mPassword = getIntent().getStringExtra(HFActivityUtil.INTENT_AUTH_NUMBER);

        mPresenter = new CheckAuthNumberPresenter(this);

        TextView textViewGuide = (TextView) findViewById(R.id.guide_textView);
        textViewGuide.setText(getString(R.string.auth_number_guide_login));

        ((TextView) findViewById(R.id.simpleDepartment_textView)).setText(HFInfoUtil.getDepartmentText(this));
    }

    @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);
        }
    }

    @Override
    public void onBackPressed() {
        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(CheckAuthNumberActivity.this, true);
                        finish();
                    }
                });
//            super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            finish();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**************************************************
     * 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) {
            mPresenter.doSimpleLogin(password);
        }

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

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