package com.cooleshow.teacher.ui.mine;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.annotation.Nullable;

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.widgets.VerifyEditText;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.ResponseBindCardBean;
import com.cooleshow.teacher.contract.InputBankVCodeContract;
import com.cooleshow.teacher.databinding.ActivityInputBankVcodeBinding;
import com.cooleshow.teacher.presenter.mine.InputBankVCodePresenter;
import com.cooleshow.usercenter.bean.SetDetailBean;

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

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import io.rong.imkit.utils.StatusBarUtil;

/**
 * 创建日期：2022/5/19 14:11
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.MineCenter.MINE_INPUT_BANK_VCODE)
public class InputBankVCodeActivity extends BaseMVPActivity<ActivityInputBankVcodeBinding, InputBankVCodePresenter> implements InputBankVCodeContract.InputBankVCodeView, View.OnClickListener {

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_resend_code:
                //跳绑卡成功
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_BIND_CARD_SUCCESS)
                        .navigation();
                break;
        }
    }

    @Override
    public void initData() {
        super.initData();
        sendVcode();
    }

    private void sendVcode() {
        Map<String,String> jsonObject = new HashMap<>();
        jsonObject.put("mobile", phone);
        jsonObject.put("type", "BANK");
        presenter.sendSmsCode(jsonObject);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarDarkTheme(this, true);
    }

    private String jsonString;
    private String phone;

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "");
        viewBinding.tvResendCode.setOnClickListener(this);
        jsonString = getIntent().getStringExtra("jsonString");
        phone = getIntent().getStringExtra("phone");
        viewBinding.tvPhoneNum.setText(phone);
        viewBinding.editCode.addInputCompleteListener(new VerifyEditText.InputCompleteListener() {
            @Override
            public void complete(String content) {
                //输入完成
                presenter.verifySmsCode(phone, "BANK", content);
            }
        });
    }


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

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

    @Override
    public void sendSmsCodeSuccess(SetDetailBean data) {
        startTimer();
    }

    @Override
    public void verifySmsCodeSuccess(Object o) {
        presenter.bindBankCard(jsonString);
    }

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


    /**
     * 执行完成
     */
    public static final int EXECUTE_FINISH = 0X11000;
    /**
     * 执行中
     */
    public static final int EXECUTE_LOADING = 0X4000;
    private int mTimerId = 60;

    private TimerTask timerTask;

    private Timer timer;

    /**
     * 开始倒计时
     */
    private void startTimer() {

        if (timerTask == null) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    Message msg = new Message();
                    msg.what = EXECUTE_LOADING;
                    msg.arg1 = --mTimerId;
                    handler.sendMessage(msg);
                }
            };
            timer = new Timer();
            timer.schedule(timerTask, 100, 1000);
        }
    }

    /**
     * 结束计时
     */
    private void closeTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask = null;
        }
        mTimerId = 60;
        handler.sendEmptyMessage(EXECUTE_FINISH);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EXECUTE_LOADING:
                    viewBinding.tvResendCode.setText(msg.arg1 + "s");
                    viewBinding.tvResendCode.setClickable(false);
                    viewBinding.tvResendCode.setBackgroundResource(com.cooleshow.base.R.drawable.shape_login_bt_bg_unselect);
                    if (msg.arg1 == 0) {
                        closeTimer();
                        viewBinding.tvResendCode.setClickable(true);
                    }
                    break;
                case EXECUTE_FINISH:
                    viewBinding.tvResendCode.setClickable(true);
                    viewBinding.tvResendCode.setText("重新发送");
                    viewBinding.tvResendCode.setBackgroundResource(R.drawable.shape_login_bt_bg_select);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }
}
