package com.android.launcher3.function.pwd;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.android.launcher3.BaseActivity;
import com.android.launcher3.R;
import com.android.launcher3.settings.BlankActivity;
import com.secspace.lib.common.dialog.other.DialogUIUtils;
import com.secspace.lib.common.utils.Prefs;

import java.util.regex.Pattern;

/**
 * @author huangzhencheng
 */
public class PwdPinSetPwdActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private static final String TAG = PwdPinSetPwdActivity.class.getSimpleName();
    /**
     * 字符密码正则
     */
    private Pattern datePatt = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)(?!([^(0-9a-zA-Z)]|[\\(\\)])+$)([^(0-9a-zA-Z)]|[\\(\\)]|[a-zA-Z]|[0-9]){8,16}$");
    /**
     * 数字密码正则
     */
    Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
    /**
     * 密码最大程度
     */
    public static final int PWD_MAX_LENGTH = 16;
    /**
     * 密码短程度
     */
    public static final int PWD_TINY_LENGTH = 6;
    private EditText mEditPwd;
    private CheckBox mCbShowPwd;
    private EditText mEditPwdAgain;
    private CheckBox mCbShowPwdAgain;
    private Button mBtSure;
    private TextView mTvtip;

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

    private void initView() {
        mEditPwd = findViewById(R.id.edit_pwd);
        mEditPwdAgain = findViewById(R.id.edit_pwd_again);
        mCbShowPwd = findViewById(R.id.cb_show_pwd);
        mCbShowPwdAgain = findViewById(R.id.cb_show_pwd_again);
        mBtSure = findViewById(R.id.btn_sure);
        mTvtip = findViewById(R.id.tv_tip);
        mBtSure.setOnClickListener(this);
        mCbShowPwd.setOnCheckedChangeListener(this);
        mCbShowPwdAgain.setOnCheckedChangeListener(this);
    }

    private void startLauncher() {
        Intent intent = new Intent();
        intent.setPackage(this.getPackageName());
        intent.setClass(this, BlankActivity.class);
        startActivity(intent);
        finish();
    }

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sure:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);

                String pwd = String.valueOf(mEditPwd.getText());
                String pwdAgain = String.valueOf(mEditPwdAgain.getText());
                if (TextUtils.isEmpty(pwd) || (pwd.length() < PWD_TINY_LENGTH)) {
                    DialogUIUtils.showToastCenter("密码为空或长度小于6位");
                    return;
                }
                if (TextUtils.isEmpty(pwdAgain) || (pwdAgain.length() < PWD_TINY_LENGTH)) {
                    DialogUIUtils.showToastCenter("确认密码为空或长度小于6位");
                    return;
                }
                if (!TextUtils.equals(pwd, pwdAgain)) {
                    DialogUIUtils.showToastCenter("两次输入密码不同");
                    return;
                }
                Log.i(TAG, "matches: " + datePatt.matcher(pwd).matches());
//                if (!datePatt.matcher(pwd).matches()) {
//                    ToastUtil.showShort(this, "新密码必须为数字、字母或常用符号的8-16位组合");
//                    return;
//                }
                Prefs.setLoginPwd(this, pwd);
                startLauncher();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_show_pwd:
                if (isChecked) {
                    mEditPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    mEditPwd.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
                break;
            case R.id.cb_show_pwd_again:
                if (isChecked) {
                    mEditPwdAgain.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    mEditPwdAgain.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
                break;

        }

    }
}
