package com.wd.workoffice.ui.activity.login;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wd.workoffice.R;
import com.wd.workoffice.app.Config;
import com.wd.workoffice.app.WorkToolBarActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.utils.RegexUtils;

/*
 * 账号密码确认密码
 * */
public class UserRegisterTwoActivity extends WorkToolBarActivity {

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.tv_go_protocol)
    TextView tvGoProtocol;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        btnNext.setEnabled(false);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
        etConfirmPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(etName.getText().toString()) &&
                        !TextUtils.isEmpty(etPwd.getText().toString())) {
                    btnNext.setEnabled(true);
                } else {
                    btnNext.setEnabled(false);
                }
            }
        });
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(etConfirmPwd.getText().toString()) &&
                        !TextUtils.isEmpty(etPwd.getText().toString())) {
                    btnNext.setEnabled(true);
                } else {
                    btnNext.setEnabled(false);
                }
            }
        });
        etPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(etName.getText().toString()) &&
                        !TextUtils.isEmpty(etConfirmPwd.getText().toString())) {
                    btnNext.setEnabled(true);
                } else {
                    btnNext.setEnabled(false);
                }
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_user_register_two;
    }

    @OnClick({R.id.btn_next, R.id.tv_go_protocol})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_next:
                String name = etName.getText().toString();
                String pwd = etPwd.getText().toString();
                String confirmPwd = etConfirmPwd.getText().toString();
                if(!RegexUtils.isMatch(Config.REGEX_USERNAME,name)){
                    toast("账号名称数字和字母４-16位");
                    return;
                }
                if(!RegexUtils.isMatch(Config.REGEX_PASSWORD,pwd)){
                    toast("设置密码数字或字母６-16位");
                    return;
                }
                if (TextUtils.equals(pwd, "123456")) {
                    toast("密码不能为123456");
                    return;
                }
                if (!TextUtils.equals(pwd, confirmPwd)) {
                    toast("两次输入的密码不一致");
                    return;
                }
                startActivity(UserRegisterThreeActivity.class,
                        "phone",getIntent().getStringExtra("phone"),
                        "code",getIntent().getStringExtra("code")
                ,"name",name,"pwd",pwd);
                break;
            case R.id.tv_go_protocol:
                break;
        }
    }
}
