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.UpDriverInfoBean
import com.dahe.gldriver.databinding.ActivityAuthDriverBinding
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.gldriver.utils.OcrUtils
import com.dahe.mylibrary.utils.PickerUtils
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 com.dahe.mylibrary.utils.TimeUtil
import com.dahe.mylibrary.utils.ToastUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName AuthRoleActivity
 * @Author 用户
 * @Date 2024/1/25 11:15
 * @Description 驾驶证认证
 */
class AuthDriverActivity : BaseActivity<ActivityAuthDriverBinding>() {

    var ossServiceUtil: OssServiceUtil = OssServiceUtil.getInstance()

    var driverInfoBean: UpDriverInfoBean = UpDriverInfoBean()

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

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

            tvStart.run {
                setOnClickListener {
                    PickerUtils.getInstance()
                        .showDate(this@AuthDriverActivity, "有效起始日期") { date ->
                            this.text = date
                        }
                }
            }
            tvEnd.run {
                setOnClickListener {
                    PickerUtils.getInstance()
                        .showDate(this@AuthDriverActivity, "有效结束日期") { date ->
                            this.text = date
                        }
                }
            }
            tvCarType.setOnClickListener {
                PickerUtils.getInstance()
                    .showSelectCarType(this@AuthDriverActivity, "选择准驾车型") { _, item ->
                        tvCarType.text = item.toString()
                    }
            }

            ivFront.setOnClickListener {
                selFrontPic()
            }

            ivBack.setOnClickListener {
                selBackPic()
            }

        }
    }

    override fun initDate() {
    }

    private fun subMit() {
        binding.run {
            driverInfoBean.run {
                licenseNo = etId.text.toString()
                licenseType = tvCarType.text.toString()
                licenseEffectiveStart = tvStart.text.toString()
                licenseEffectiveEnd = tvEnd.text.toString()
            }
        }
        if (driverInfoBean.licenseNo.isNullOrBlank() || driverInfoBean.licensePhotoBackUrl.isNullOrBlank()) {
            ToastUtils.showToast(mContext, "请完善信息")
            return
        }



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

    }

    private fun selFrontPic() {

        OcrUtils.getInstance().ocrDriver(mContext, this) { picPath, result ->
            result?.data?.face?.data.let {
                ImageLoader.getInstance()
                    .loadRoundImage(mContext, picPath.locPic, 12, binding.ivFront)
                driverInfoBean.licensePhotoFaceUrl = picPath.picPath
                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 ""
                        }
                    }
                }
            }
        }


    }

    private fun selBackPic() {

        OcrUtils.getInstance().ocrDriver(mContext, this) { picPath, result ->
            result?.data?.back?.data.let {
                ImageLoader.getInstance()
                    .loadRoundImage(mContext, picPath.locPic, 12, binding.ivBack)
                driverInfoBean.licensePhotoBackUrl = picPath.picPath
                ToastUtils.showToast(mContext, "驾驶证副页上传成功")
            }
        }

    }
}