package com.cooleshow.teacher.ui.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.data.net.ApiException;
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.ResponseBankCardBean;
import com.cooleshow.teacher.bean.ResponseBindCardBean;
import com.cooleshow.teacher.constants.TeacherConfig;
import com.cooleshow.teacher.contract.MyBankCardContract;
import com.cooleshow.teacher.databinding.ActivityUnBindBankCardBinding;
import com.cooleshow.teacher.presenter.mine.MyBankCardPresenter;
import com.cooleshow.usercenter.constants.UserConstants;

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

import io.rong.imkit.utils.StatusBarUtil;

@Route(path = RouterPath.MineCenter.MINE_UNBIND_BANKCARD)
public class UnBindBankCardActivity extends BaseMVPActivity<ActivityUnBindBankCardBinding, MyBankCardPresenter> implements MyBankCardContract.MyBankCardView {
    private String bankCardId;

    @Override
    protected void initView() {
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        viewBinding.titleView.setLeftClickListener(v -> {
            finish();
        });
        viewBinding.unbindView.setOnClickListener(v -> {
            unbindBank();
        });

        Intent intent = getIntent();
        bankCardId = intent.getStringExtra(TeacherConfig.BANK_CARD_ID);
        String bankName = intent.getStringExtra(TeacherConfig.BANK_NAME);
        String bankCard = intent.getStringExtra(TeacherConfig.BANK_CARD);
        String temCard = bankCard.substring(0, 4) + "**** ****" + bankCard.substring(bankCard.length() - 4);
        String bankString = bankName + "（" + temCard + "）银行卡";
        viewBinding.bankText.setText(bankString);
    }


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

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

    /**
     * 解绑
     */
    private void unbindBank() {
        String realName = viewBinding.etRealName.getText().toString();
        String realIdCard = viewBinding.etRealIdcard.getText().toString();
        if (TextUtils.isEmpty(realName)) {
            ToastUtil.getInstance().show(this, "请输入您的真实姓名");
            return;
        }
        if (TextUtils.isEmpty(realIdCard)) {
            ToastUtil.getInstance().showShort("请输入您的身份证号");
            return;
        }
        presenter.unBindBankCard(bankCardId, realName, realIdCard);
    }

    @Override
    public void querySuccess(int page, ResponseBankCardBean data) {

    }

    /**
     * 解绑成功
     */
    @Override
    public void unbindBankCard(boolean isSuccess) {
        ARouter.getInstance().build(RouterPath.UserCenter.COMMON_EMPTY_ACTIVITY)
                .withString(UserConstants.PAGE_TITLE, isSuccess ? "解绑成功" : "解绑失败")
                .withInt(UserConstants.EMPTY_ICON_RESID, R.drawable.icon_bind_card_success)
                .withString(UserConstants.EMPTY_BTN_TEXT, isSuccess ? "我知道了" : "重新解绑")
                .navigation();
        if (isSuccess) finish();
    }

    @Override
    public void unbindBankCardError(Throwable throwable) {
        if (throwable instanceof ApiException) {
            ApiException apiException = (ApiException) throwable;
            ARouter.getInstance().build(RouterPath.UserCenter.COMMON_EMPTY_ACTIVITY)
                    .withString(UserConstants.PAGE_TITLE, "解绑失败")
                    .withString(UserConstants.PAGE_ERROR_TEXT, apiException.getErrmsg())
                    .withInt(UserConstants.EMPTY_ICON_RESID, R.drawable.icon_bind_card_error)
                    .withString(UserConstants.EMPTY_BTN_TEXT, "重新解绑")
                    .navigation();
        }
    }
}