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.databinding.ActivityAuthDrivingBinding
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 AuthRoleActivity
 * @Author 用户
 * @Date 2024/1/25 11:15
 * @Description 行驶证认证
 */
class AuthDrivingActivity : BaseActivity<ActivityAuthDrivingBinding>() {

    var ossServiceUtil: OssServiceUtil = OssServiceUtil.getInstance()
    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("实名认证", true)
        binding.btnOk.setOnClickListener {
            ActivityUtils.startActivity(mContext, AuthRoadActivity::class.java)
        }
        binding.run {
            ivFront.setOnClickListener {
                selFrontPic()
//                PopsUtils.getInstance().showSimSelPic(mContext) {
//                    ImageLoader.getInstance().loadRoundImage(mContext, it[0].path, 12, ivFront)
//                }
            }

            ivBack.setOnClickListener {
                selBackPic()
//                PopsUtils.getInstance().showSimSelPic(mContext) {
//                    ImageLoader.getInstance().loadRoundImage(mContext, it[0].path, 12, ivBack)
//                }
            }
            ivCar.setOnClickListener {
                PopsUtils.getInstance().showSimSelPic(mContext) {
                    ImageLoader.getInstance().loadRoundImage(mContext, it[0].path, 12, ivCar)
                }
            }
        }
    }

    override fun initDate() {
    }

    private fun selFrontPic() {
        PopsUtils.getInstance().showSimSelPic(mContext) {
            var data = it[0]
            ossServiceUtil.asyncPutImage(data.realPath, this@AuthDrivingActivity)
            ossServiceUtil.setResultCallBack { data, oldPath ->
                DataManager.getInstance().recognizeVehicleLicense(oldPath)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                        BaseObserver(mContext,
                            object : RxHttpCallBack<OcrPersonBean>() {
                                override fun onSuccess(t: CommonResponseBean<OcrPersonBean>) {
                                    super.onSuccess(t)
                                    t.data.data.face?.data?.let {
                                        binding.run {
                                            etCarNum.setText(it.licensePlateNumber)
                                            etOwner.setText(it.owner)
                                            etUseNature.setText(it.useNature)
                                            etCurbWeight.setText(it.curbWeight)
                                            etPermittedWeight.setText(it.permittedWeight)
                                            tvCarType.text = it.vehicleType
                                        }
                                    }
//                                    t.data.data.face?.data.let {
//                                        driverInfoBean.licensePhotoFaceUrl = oldPath
//                                        it?.run {
//                                            driverInfoBean.run {
//                                                licenseApproveOrganization =
//                                                    issueAuthority //发证机关
//                                            }
//
//
//                                            binding.run {
//                                                etName.setText(it.name)
//                                                etId.setText(it.licenseNumber)
//                                                tvCarType.text = it.approvedType
//                                                if (validPeriod.contains("至")) {
//                                                    val split = validPeriod.split("至")
//                                                    tvStart.text = split[0]
//                                                    tvEnd.text =
//                                                        if (split.size == 2) split[1] else ""
//                                                }
//                                            }
//                                        }
//                                    }
                                }
                            })
                    )

            }

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

    private fun selBackPic() {
        PopsUtils.getInstance().showSimSelPic(mContext) {
            var data = it[0]
            ossServiceUtil.asyncPutImage(data.realPath, this@AuthDrivingActivity)
            ossServiceUtil.setResultCallBack { data, oldPath ->
                run {
                    DataManager.getInstance().recognizeVehicleLicense(oldPath)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                            BaseObserver(mContext,
                                object : RxHttpCallBack<OcrPersonBean>() {
                                    override fun onSuccess(t: CommonResponseBean<OcrPersonBean>) {
                                        super.onSuccess(t)
//                                        t.data.data.back?.data.let {
//                                            driverInfoBean.licensePhotoBackUrl = oldPath
//                                        }
                                    }
                                })
                        )

                }
            }


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