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

import android.content.Intent;
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.BaseBean;
import com.wd.workoffice.app.Config;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.ActivationUserBean;
import com.wd.workoffice.bean.DepBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.user.UserServerActivity;
import com.wd.workoffice.util.WorkUtils;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import flexible.xd.android_base.utils.RegexUtils;
import io.reactivex.disposables.Disposable;

/*
 *设置账号密码激活
 * */
public class ErpActivationTwoActivity 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_dep)
    TextView tvDep;
    @BindView(R.id.ll_dep)
    LinearLayout llDep;
    @BindView(R.id.tv_go_protocol)
    TextView tvGoProtocol;
    private Map<String, Object> param;
    private Integer depId;
    private DepBean dep;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        btnNext.setEnabled(false);
        param = new HashMap<>();
    }

    @Override
    protected void initData() {
        param.put("verifyCode", getIntent().getStringExtra("code"));
        param.put("phone", getIntent().getStringExtra("phone"));
        ActivationUserBean info = JSON.parseObject(getIntent().getStringExtra("info"), ActivationUserBean.class);
        param.put("email", info.getEmail());
        param.put("nickName", info.getNickName());
    }

    @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_erp_activation_two;
    }


    private void submit() {
        RtfUtils.getRtf().activation(WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                finish();
                toast(data.getMessage());
                startActivity(LoginActivity.class);
            }
        });
    }


    @OnClick({R.id.ll_dep, R.id.btn_next, R.id.tv_go_protocol})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_dep:
                startActivityForResult(UserRegisterDepActivity.class, 10001);
                break;
            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;
                }
                if (dep == null) {
                    toast("请选择部门");
                    return;
                }

                param.put("userName", name);
                param.put("password", pwd);
                param.put("ofRegisterNewDept", dep.getOfRegisterNewDept());
                if (dep.getOfRegisterNewDept() == 1) {
                    param.put("newDeptName", dep.getName());
                    param.put("newDeptErpId", dep.getCode());
                    param.put("newDeptType", dep.getType());
                } else {
                    param.put("deptId", dep.getId());
                }
                submit();
                break;
            case R.id.tv_go_protocol:
                startActivity(UserServerActivity.class);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == 10001) {
            String depName = data.getStringExtra("depName");
            depId = data.getIntExtra("id", 0);
            tvDep.setText(depName);
            dep = JSON.parseObject(data.getStringExtra("dep"), DepBean.class);
        }
    }
}
