package com.android.launcher3.function.pwd;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.launcher3.BaseActivity;
import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.secspace.lib.common.utils.Prefs;
import com.secspace.lib.common.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

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;
    EditText mEditPwd;
    CheckBox mCbShowPwd;
    Button mBtSure;
    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);
        mCbShowPwd = findViewById(R.id.cb_show_pwd);
        mBtSure = findViewById(R.id.btn_sure);
        mTvtip = findViewById(R.id.tv_tip);
        mBtSure.setOnClickListener(this);
        mCbShowPwd.setOnCheckedChangeListener(this);
    }

    private void startLauncher() {
        Intent intent = new Intent();
        intent.setAction("android.start_launcher");
        intent.setPackage(this.getPackageName());
        intent.setClass(this, Launcher.class);
        startActivity(intent);
        finish();
    }

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sure:
                String pwd = String.valueOf(mEditPwd.getText());
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showShort(this, "密码不能为空");
                    return;
                }
                if (pwd.length() < PWD_TINY_LENGTH) {
                    ToastUtil.showShort(this, "密码至少为6位");
                    return;
                }
//                if (pwd.length() > PWD_MAX_LENGTH) {
//                    ToastUtil.showShort(this, "密码不能超过16位");
//                    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) {
        if (isChecked) {
            Log.i(TAG, "onCheckedChanged: TYPE_TEXT_VARIATION_PASSWORD");
            mEditPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {
            mEditPwd.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }
}
