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.WorkToolBarActivity;
import com.wd.workoffice.bean.workEnum.CodeType;
import com.wd.workoffice.contract.ForgetOneContract;
import com.wd.workoffice.presenter.ForgetOnePresenter;
import com.wd.workoffice.util.WorkUtils;

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

/*
 * 忘记密码第一步
 * */
public class ForgetOneActivity extends WorkToolBarActivity implements ForgetOneContract.View {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.btn_register)
    Button btnRegister;
    private ForgetOnePresenter registerFirstPresenter;


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


    @Override
    protected void initView() {
        ButterKnife.bind(this);
        registerFirstPresenter = new ForgetOnePresenter();
        registerFirstPresenter.onAttach(this);
        btnRegister.setEnabled(false);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        etPhone.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(etCode.getText().toString())) {
                    btnRegister.setEnabled(true);
                } else {
                    btnRegister.setEnabled(false);
                }
            }
        });

        etCode.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(etPhone.getText().toString())) {
                    btnRegister.setEnabled(true);
                } else {
                    btnRegister.setEnabled(false);
                }
            }
        });
    }


    @OnClick({R.id.tv_code, R.id.btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_code:
                String phone = etPhone.getText().toString();
                if (TextUtils.isEmpty(phone) || !RegexUtils.isMobileSimple(phone)) {
                    toast("请输入正确的手机号");
                    return;
                }
                registerFirstPresenter.getCode(phone, CodeType.ALTERPWD.getName());
                break;
            case R.id.btn_register:
                String code = etCode.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    toast("请输入验证码");
                    return;
                }
                registerFirstPresenter.verifyCode(etPhone.getText().toString(), code, CodeType.ALTERPWD.getName());
                break;
        }
    }

    @Override
    public void onError(String message) {
        toast(message);
    }

    @Override
    public void codeSuccess() {
        WorkUtils.timerSeckill(tvCode, 60 * 1000, 1000, 0);
        ToastUtil.showLong("验证码发送成功，请注意查收！");
    }

    @Override
    public void verifySuccess() {
        startActivity(ForgetTwoActivity.class, "phone", etPhone.getText().toString(), "code",
                etCode.getText().toString());
    }
}
