package com.android.launcher3.settings;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.launcher3.R;
import com.android.launcher3.function.setting.ChangePwdActivity;
import com.android.launcher3.function.setting.AdminSettingActivity;
import com.secspace.lib.common.utils.InputUtil;
import com.secspace.lib.common.utils.Prefs;
import com.secspace.lib.common.utils.ToastUtil;

import static com.android.launcher3.settings.Contants.FROM_WHERE;
import static com.android.launcher3.settings.Contants.IS_SHOW_ORIGINAL_PWD;

public class VerifyPwdProtectionActivity extends AppCompatActivity implements View.OnClickListener {
    TextView mTvQuestion1;
    TextView mTvQuestion2;
    EditText mEditAnswer1;
    EditText mEditAnswer2;
    Button mBtnSure;
    private Intent mIntent;
    private TextView mTvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_pwdprotection);
        initView();
        initData();
    }

    private void initData() {
        mTvQuestion1.setText("密保1： " + Prefs.getPwdProtectionQuestion1(this));
        mTvQuestion2.setText("密保2： " + Prefs.getPwdProtectionQuestion2(this));
        mIntent = getIntent();
    }

    private void initView() {
        mTvQuestion1 = findViewById(R.id.tv_question_1);
        mTvQuestion2 = findViewById(R.id.tv_question_2);
        mEditAnswer1 = findViewById(R.id.edit_answer_1);
        mEditAnswer2 = findViewById(R.id.edit_answer_2);
        mBtnSure = findViewById(R.id.btn_sure);
        findViewById(R.id.tv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputUtil.closeSoftInput(getApplicationContext(), mEditAnswer1);
                finish();
            }
        });
        mBtnSure.setOnClickListener(this);
        mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("验证密保");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onClick(View v) {
        String answer1 = mEditAnswer1.getText().toString();
        String answer2 = mEditAnswer2.getText().toString();
        if (!ErrorTimeHelper.getInstance(this).canShowDialog()){
            ToastUtil.showShort(this,"请在"+ ErrorTimeHelper.getInstance(this).getCountDownTime()+"秒后重试");
            return;
        }
        if (TextUtils.isEmpty(answer1) || TextUtils.isEmpty(answer2)) {
            ToastUtil.showShort(this, "密保问题不能为空");
            return;
        }
        if (!TextUtils.equals(answer1, Prefs.getPwdProtectionAnswer1(this)) && !TextUtils.equals(answer2, Prefs.getPwdProtectionAnswer2(this))) {
            ToastUtil.showShort(this, "密保问题错误");
            ErrorTimeHelper.getInstance(this).canInputPwd();
            return;
        }
        String fromWhere = mIntent.getStringExtra(FROM_WHERE);
        Intent intent;
        if (!TextUtils.isEmpty(fromWhere) && TextUtils.equals(fromWhere, AdminSettingActivity.class.getSimpleName())) {
            intent = new Intent(this, ProtectionPwdActivity.class);
        } else {
            intent = new Intent(this, ChangePwdActivity.class);
            intent.putExtra(IS_SHOW_ORIGINAL_PWD, false);
        }
        startActivity(intent);
        finish();
    }
}
