package com.dahe.gldriver.ui.account.authperson

import android.os.Bundle
import com.dahe.gldriver.R
import com.dahe.gldriver.bean.OcrPersonBean
import com.dahe.gldriver.bean.UpBankInfoBean
import com.dahe.gldriver.databinding.ActivityAuthBankcardBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.oss.OssServiceUtil
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.ImageLoader
import com.dahe.mylibrary.utils.PopsUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName AuthBankCardActivity
 * @Author john
 * @Date 2024/1/25 15:31
 * @Description TODO
 */
class AuthBankCardActivity : BaseActivity<ActivityAuthBankcardBinding>() {
    private var ossServiceUtil: OssServiceUtil = OssServiceUtil.getInstance()

    private var bankInfo = UpBankInfoBean()

    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("实名认证", true)

        binding.run {
            btnOk.setOnClickListener {
                subMit()
            }
            ivFront.setOnClickListener {
                selBankPic()
            }
        }
    }

    override fun initDate() {
    }


    private fun selBankPic() {
        PopsUtils.getInstance().showSimSelPic(mContext) { it ->
            var data = it[0]
            ossServiceUtil.asyncPutImage(data.realPath, this@AuthBankCardActivity)
            ossServiceUtil.setResultCallBack { data, oldPath ->
                DataManager.getInstance().recognizeBankCard(oldPath)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                        BaseObserver(mContext,
                            object : RxHttpCallBack<OcrPersonBean>(this) {
                                override fun onSuccess(t: CommonResponseBean<OcrPersonBean>) {
                                    super.onSuccess(t)
                                    t.data?.data?.run {
                                        bankInfo.driverBankcardPhotoUrl = oldPath

                                        binding.let {
                                            it.etId.setText(cardNumber)
                                            it.tvBankName.text = bankName
                                        }
                                    }

                                }
                            })
                    )

            }

            ImageLoader.getInstance().loadRoundImage(mContext, data.realPath, 12, binding.ivFront)
        }
    }

    private fun subMit() {
        bankInfo.run {

            bankInfo.driverBankcardNumber = binding.etId.text.toString()
            bankInfo.driverBankcardAddress = binding.tvBankName.text.toString()

            DataManager.getInstance().setUpDriverBankCard(this)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(BaseObserver(mContext, object : RxHttpCallBack<String>() {
                    override fun onSuccess(t: CommonResponseBean<String>) {
                        super.onSuccess(t)
                        ActivityUtils.startActivity(mContext, AuthFaceActivity::class.java)
                    }
                }))
        }
    }
}