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

import android.content.Intent;
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.DepBean;
import com.wd.workoffice.contract.UserRegisterThreeContract;
import com.wd.workoffice.presenter.UserRegisterThreePresenter;
import com.wd.workoffice.ui.activity.bat.store.StoreSaleClientActivity;
import com.wd.workoffice.util.UserKeeper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 * 完善信息
 * */
public class UserRegisterThreeActivity extends WorkToolBarActivity implements UserRegisterThreeContract.View {
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_product_content)
    TextView tvProductContent;
    @BindView(R.id.ll_product)
    LinearLayout llProduct;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    private UserRegisterThreePresenter userRegisterThreePresenter;
    private List<DepBean> depList;
    private List<String> depNameList = new ArrayList<>();
    private int chooseDep = -1;
    private AlertDialog.Builder builder;
    private Integer depId;
    private DepBean dep;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        userRegisterThreePresenter = new UserRegisterThreePresenter();
        userRegisterThreePresenter.onAttach(this);
        btnSubmit.setEnabled(false);
        tvPhone.setText(getIntent().getStringExtra("phone"));

    }

    @Override
    protected void initData() {
        userRegisterThreePresenter.depList();
    }

    @Override
    protected void initEvent() {
        addInputListener(etCode);
        addInputListener(tvProductContent);
        addInputListener(etEmail);
        addInputListener(etName);
    }

    /**
     * 添加监听事件
     *
     * @param et
     */
    private void addInputListener(TextView et) {
        et.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(etEmail.getText().toString()) &&
                        !TextUtils.isEmpty(etCode.getText().toString()) &&
                        !TextUtils.isEmpty(etName.getText().toString()) &&
                        !TextUtils.isEmpty(tvProductContent.getText().toString())) {
                    btnSubmit.setEnabled(true);
                } else {
                    btnSubmit.setEnabled(false);
                }
            }
        });
    }

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


    @OnClick({R.id.ll_product, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_product:
//                if (builder == null) {
//                    toast("正在加载部门，请稍等");
//                    return;
//                }
//                builder.show();
                startActivityForResult(UserRegisterDepActivity.class, 10001);
                break;
            case R.id.btn_submit:
                if (dep == null) {
                    toast("请选择部门");
                    return;
                }
                Map<String, Object> param = new HashMap<>();
                param.put("userName", getIntent().getStringExtra("name"));
                param.put("password", getIntent().getStringExtra("pwd"));
                param.put("nickName", etName.getText().toString());
                param.put("email", etEmail.getText().toString());
                param.put("phone", getIntent().getStringExtra("phone"));
//                param.put("erpId", "");
                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());
                }
                param.put("verifyCode", getIntent().getStringExtra("code"));
                userRegisterThreePresenter.register(param);
                break;
        }
    }

    private void initDialog() {
        builder = new AlertDialog.Builder(this);
        String[] stringArray = new String[depNameList.size()];
        builder.setItems(depNameList.toArray(stringArray), (dialog, which) -> {
            tvProductContent.setText(depNameList.get(which));
            chooseDep = which;
        });
//        normalDialog = builder.setView(dialogView).setPositiveButton("确定", (dialog, which) -> clientPresenter.changeClient(id, status + ""))
//                .setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).create();

    }

    @Override
    public void registerSuccess(String msg) {
        toast(msg);
        startActivity(LoginActivity.class);
    }

    @Override
    public void depSuccess(List<DepBean> dataList) {
        depList = dataList;
        for (DepBean dep : dataList) {
            depNameList.add(dep.getName());
        }
        initDialog();
    }


    @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);
            tvProductContent.setText(depName);
            dep = JSON.parseObject(data.getStringExtra("dep"), DepBean.class);
        }
    }
}
