package com.cooleshow.teacher.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.ResponseBindCardBean;
import com.cooleshow.teacher.bean.ResponseUserBankCardBean;
import com.cooleshow.teacher.contract.BindBankCardContract;
import com.cooleshow.teacher.databinding.ActivityBindBankcardBinding;
import com.cooleshow.teacher.presenter.mine.BindBankCardPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期：2022/5/19 9:26
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.MineCenter.MINE_BIND_BANKCARD)
public class BindBankCardActivity extends BaseMVPActivity<ActivityBindBankcardBinding, BindBankCardPresenter> implements BindBankCardContract.BindBankCardView, View.OnClickListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_bind:
                String realName = viewBinding.etRealName.getText().toString().trim();
                if (TextUtils.isEmpty(realName)) {
                    ToastUtil.getInstance().showShort("请输入您的真实姓名");
                    break;
                }
                String idCardNo = viewBinding.etRealIdcard.getText().toString().trim();
                if (TextUtils.isEmpty(idCardNo)) {
                    ToastUtil.getInstance().showShort("请输入您的身份证号");
                    break;
                }
                if (TextUtils.isEmpty(selectBankName)) {
                    ToastUtil.getInstance().showShort("请选择开户行");
                    break;
                }
                String bankNum = viewBinding.etBankNum.getText().toString().trim();
                if (TextUtils.isEmpty(bankNum)) {
                    ToastUtil.getInstance().showShort("请输入银行卡号");
                    break;
                }
                String bankPhone = viewBinding.etBankPhone.getText().toString().trim();
                if (TextUtils.isEmpty(bankPhone)) {
                    ToastUtil.getInstance().showShort("请输入手机号");
                    break;
                }
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.putOpt("bankCard", bankNum);
                    jsonObject.putOpt("idCardNo", idCardNo);
                    jsonObject.putOpt("bankName", selectBankName);
                    jsonObject.putOpt("bankCode", selectBankCode);
                    jsonObject.putOpt("name", realName);
                    jsonObject.putOpt("phone", bankPhone);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

              /*  ARouter.getInstance().build(RouterPath.MineCenter.MINE_INPUT_BANK_VCODE)
                        .withString("jsonString", jsonObject.toString())
                        .withString("phone", bankPhone)
                        .navigation(this, 1001);*/


                presenter.bindBankCard(jsonObject.toString());
                break;
            case R.id.cl_bankname:
                if (bankList.size() > 0) {
                    selectBank();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1001) {
                finish();
            }
        }
    }

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "绑定银行卡");
        viewBinding.tvBind.setOnClickListener(this);
        viewBinding.clBankname.setOnClickListener(this);


    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.userBankCard();
    }

    @Override
    protected ActivityBindBankcardBinding getLayoutView() {
        return ActivityBindBankcardBinding.inflate(getLayoutInflater());
    }

    @Override
    protected BindBankCardPresenter createPresenter() {
        return new BindBankCardPresenter();
    }

    @Override
    public void bindBankCardSuccess(ResponseBindCardBean data) {
        ToastUtil.getInstance().showShort("绑卡成功");
        ARouter.getInstance().build(RouterPath.MineCenter.MINE_BIND_CARD_SUCCESS)
                .navigation();
        setResult(RESULT_OK);
        finish();
    }

    private List<ResponseUserBankCardBean> bankList = new ArrayList<>();

    @Override
    public void userBankCardSuccess(List<ResponseUserBankCardBean> data) {
        if (null != data) {
            bankList.clear();
            bankList.addAll(data);
        }
    }

    private OptionsPickerView pvOptions;
    private String selectBankName = "";
    private String selectBankCode = "";

    private void selectBank() {
        pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String opt1tx = bankList.size() > 0 ?
                        bankList.get(options1).paramValue : "";
                viewBinding.tvSelectBankname.setText(opt1tx);
                selectBankName = opt1tx;
                selectBankCode = bankList.size() > 0 ?
                        bankList.get(options1).paramName : "";
                viewBinding.tvSelectBankname.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_1a1a1a));

            }
        })
                .setLayoutRes(R.layout.pickerview_address_layout, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        //自定义布局中的控件初始化及事件处理
                        final TextView tvSubmit = (TextView) v.findViewById(com.cooleshow.base.R.id.tv_finish);
                        TextView ivCancel = (TextView) v.findViewById(com.cooleshow.base.R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvOptions.returnData();
                                pvOptions.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvOptions.dismiss();
                            }
                        });

                    }
                })
                .isDialog(false)
                .build();
        pvOptions.setPicker(bankList);
        pvOptions.show();

    }
}
