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.UserRegisterOneContract;
import com.wd.workoffice.presenter.UserRegisterOnePresenter;
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 UserRegisterOneActivity extends WorkToolBarActivity implements UserRegisterOneContract.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;
    @BindView(R.id.tv_go_protocol)
    TextView tvGoProtocol;
    private UserRegisterOnePresenter registerFirstPresenter;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        registerFirstPresenter = new UserRegisterOnePresenter();
        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);
                }
            }
        });
    }

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

    @OnClick({R.id.tv_code, R.id.btn_register, R.id.tv_go_protocol})
    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.REGISTER.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.REGISTER.getName());
                break;
            case R.id.tv_go_protocol:
                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(UserRegisterTwoActivity.class, "phone", etPhone.getText().toString(), "code",
                etCode.getText().toString());
    }
}
