package com.cooleshow.teacher.ui.mine;

import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.PermissionUtils;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.widgets.CommonDialog;
import com.cooleshow.base.widgets.CommonTipDialog;
import com.cooleshow.base.widgets.DialogUtil;
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.constants.TeacherInfoConstants;
import com.cooleshow.teacher.contract.MyBankCardContract;
import com.cooleshow.teacher.databinding.ActivityMyBankCardBinding;
import com.cooleshow.teacher.presenter.mine.MyBankCardPresenter;

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

import io.rong.imkit.utils.StatusBarUtil;

/**
 * 创建日期：2022/5/18 16:57
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.MineCenter.MINE_MY_BANKCARD)
public class MyBankCardActivity extends BaseMVPActivity<ActivityMyBankCardBinding, MyBankCardPresenter> implements MyBankCardContract.MyBankCardView, View.OnClickListener {

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_right_text:
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_BIND_BANKCARD)
                        .navigation();
                break;
            case R.id.tv_unbind:
                if (null == rowsBean) {
                    ToastUtil.getInstance().showShort("还未绑定银行卡");
                    break;
                }
                CommonTipDialog.getInstance(this)
                        .setTitle("解绑银行卡")
                        .setContent("解绑后将无法将收入提现，请确认是否继续解绑")
                        .setCancel(getString(R.string.cancel), v -> {
                            CommonTipDialog.dismissDialog();
                        })
                        .setConfirm(getString(R.string.sure), v -> {
                            CommonTipDialog.dismissDialog();
                            ARouter.getInstance().build(RouterPath.MineCenter.MINE_UNBIND_BANKCARD)
                                    .withString(TeacherConfig.BANK_CARD_ID, String.valueOf(rowsBean.id))
                                    .withString(TeacherConfig.BANK_NAME, rowsBean.bankName)
                                    .withString(TeacherConfig.BANK_CARD, rowsBean.bankCard)
                                    .navigation();
                        })
                        .show();
                break;
            case R.id.tv_bind_card:
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_BIND_BANKCARD)
                        .navigation();
                break;
        }
    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this, true);
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "我的银行卡");
        viewBinding.toolbarInclude.tvRightText.setTextColor(getResources().getColor(com.cooleshow.base.R.color.common_black));
        viewBinding.toolbarInclude.tvRightText.setVisibility(View.GONE);
        viewBinding.toolbarInclude.tvRightText.setText("更换绑定卡");
        viewBinding.toolbarInclude.tvRightText.setOnClickListener(this);
        viewBinding.tvUnbind.setOnClickListener(this);
        viewBinding.includeEmpty.tvBindCard.setOnClickListener(this);
    }

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

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

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

    private ResponseBankCardBean.RowsBean rowsBean;

    @Override
    public void querySuccess(int page, ResponseBankCardBean data) {
        if (null != data && null != data.rows && data.rows.size() > 0) {
            rowsBean = data.rows.get(0);
            viewBinding.includeEmpty.getRoot().setVisibility(View.GONE);
            viewBinding.clContent.setVisibility(View.VISIBLE);
            viewBinding.tvBankName.setText(rowsBean.bankName);
            String bankCard = rowsBean.bankCard;
            String temCard = bankCard.substring(0, 4) + "**** ****" + bankCard.substring(bankCard.length() - 4);
            viewBinding.tvCardNum.setText(temCard);
            viewBinding.toolbarInclude.tvRightText.setVisibility(View.VISIBLE);
        } else {
            viewBinding.includeEmpty.getRoot().setVisibility(View.VISIBLE);
            viewBinding.clContent.setVisibility(View.GONE);
            viewBinding.toolbarInclude.tvRightText.setVisibility(View.GONE);
        }
    }

    @Override
    public void unbindBankCard(boolean isSuccess) {

    }

    @Override
    public void unbindBankCardError(Throwable throwable) {

    }
}