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

import android.os.Bundle;
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.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.ActivationUserBean;
import com.wd.workoffice.bean.workEnum.CodeType;
import com.wd.workoffice.contract.ErpActivationContract;
import com.wd.workoffice.presenter.ErpActivationPresenter;
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;

/*
 * ERP员工激活
 * */
public class ErpActivationOneActivity extends WorkToolBarActivity implements ErpActivationContract.View {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_discover)
    TextView tvDiscover;
    @BindView(R.id.et_staff_code)
    TextView etStaffCode;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.ll_dep)
    LinearLayout llDep;
    @BindView(R.id.et_name)
    TextView etName;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.btn_next)
    Button btnNext;
    private ErpActivationPresenter erpActivationPresenter;
    private ActivationUserBean infoData;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        erpActivationPresenter = new ErpActivationPresenter();
        erpActivationPresenter.onAttach(this);
        btnNext.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())) {
                    btnNext.setEnabled(true);
                } else {
                    btnNext.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())) {
                    btnNext.setEnabled(true);
                } else {
                    btnNext.setEnabled(false);
                }
            }
        });
    }

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

    @OnClick({R.id.tv_discover, R.id.tv_code, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_discover:
                String discover = etPhone.getText().toString();
                if (!RegexUtils.isMobileSimple(discover)) {
                    toast("请输入正确的手机号");
                    return;
                }
                erpActivationPresenter.getInfo(discover);
                break;
            case R.id.tv_code:
                String phone = etPhone.getText().toString();
                erpActivationPresenter.getCode(phone, CodeType.REGISTER.getName());
                break;
            case R.id.btn_next:
                if (infoData == null) {
                    toast("请检测用户信息");
                    return;
                }
                String code = etCode.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    toast("请输入验证码");
                    return;
                }
//                verifySuccess();
                erpActivationPresenter.verifyCode(etPhone.getText().toString(), code, CodeType.REGISTER.getName());
                break;
        }
    }

    @Override
    public void getInfo(ActivationUserBean data) {
        infoData = data;
        etName.setText(data.getNickName());
        etStaffCode.setText(data.getErpId());
        tvDep.setText(data.getDeptName());
    }

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

    @Override
    public void verifySuccess() {
        startActivity(ErpActivationTwoActivity.class,
                "phone", etPhone.getText().toString(), "code", etCode.getText().toString(),
                "info", JSON.toJSONString(infoData));
    }

}
