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

import android.os.Bundle
import com.dahe.gldriver.R
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.DiCarColor
import com.dahe.gldriver.bean.UpDrivingInfoBean
import com.dahe.gldriver.databinding.ActivityAuthTrailerBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.ui.account.AuthSuccActivity
import com.dahe.gldriver.utils.DicUtils
import com.dahe.gldriver.utils.OcrUtils
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.PickerUtils
import com.dahe.mylibrary.utils.TimeUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName AuthTrailerActivity
 * @Author john
 * @Date 2024/3/11 10:00
 * @Description TODO
 */
class AuthTrailerActivity : BaseActivity<ActivityAuthTrailerBinding>() {

    var carId :String = ""
    var upDrivingInfoBean = UpDrivingInfoBean()
    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("实名认证", true)
        carId = intent.extras?.getString(AppConfig.CAR_ID,"").toString()

        binding.run {
            ivFront.setOnClickListener {
                selFrontPic()
            }

            ivBack.setOnClickListener {
                selBackPic()
            }
            tvCarType.setOnClickListener {
                //车辆类型
                DicUtils.getInstance()
                    .selectDicCarType(mContext, this@AuthTrailerActivity,"重型自卸半挂车",true) { position, item ->
                        var data = item as DiCarColor
                        tvCarType.text = data.dictLabel
                        upDrivingInfoBean.run {
                            vehicleTypeCode = data.dictValue
                            vehicleType = data.dictLabel
                        }
                    }
            }

            tvCarColor.setOnClickListener {
                DicUtils.getInstance()
                    .getCarColor(mContext, this@AuthTrailerActivity,true) { position, item ->
                        var data = item as DiCarColor
                        tvCarColor.text = data.dictLabel
                        upDrivingInfoBean.run {
                            trailerPlateColorCode = data.dictValue
                            trailerPlateColor = data.dictLabel
                        }
                    }
            }

            tvEnd.setOnClickListener {
                PickerUtils.getInstance().showDateSim(this@AuthTrailerActivity) {
                    tvEnd.text = it
                    upDrivingInfoBean.licenseValidity = it
                }
            }

            btnOk.setOnClickListener {
                subMint()
            }

        }
    }

    override fun initDate() {

    }

    /**
     * 行驶证正面
     */
    private fun selFrontPic() {
        OcrUtils.getInstance().ocrDriveing(mContext, this) { picPath, result ->
            ImageLoader.getInstance()
                .loadRoundImage(mContext, picPath.locPic, 12, binding.ivFront)
            upDrivingInfoBean.licenseFaceUrl = picPath.picPath//行驶证照片（正面）

            result?.data?.face?.data?.let {

                //识别车辆类型后，调用接口匹配code值！！！？？？
                DicUtils.getInstance()
                    .getCarTypeByValue(mContext, it.vehicleType) { position, item ->
                        if (item != null) {
                            var data = item as DiCarColor
                            upDrivingInfoBean.run {
                                vehicleTypeCode = data.dictValue
                                vehicleType = data.dictLabel
                            }
                            binding.tvCarType.text = data.dictLabel
                        }
                    }

                binding.run {
                    etCarNum.setText(it.licensePlateNumber)
                    etUseNature.setText(it.useNature)
                }
            }
        }
    }

    private fun selBackPic() {

        OcrUtils.getInstance().ocrDriveing(mContext, this) { picPath, result ->
            ImageLoader.getInstance()
                .loadRoundImage(mContext, picPath.locPic, 12, binding.ivBack)
            upDrivingInfoBean.licenseBackUrl = picPath.picPath//行驶证照片（反面）

            result?.data?.back?.data?.let {
                if (it.inspectionRecord.length > 19) {
                    //行驶证到期时间
                    val changeDateNoneFormat = TimeUtil.changeDateNoneFormat(
                        it.inspectionRecord.substring(12, 19),
                        "yyyy年MM月"
                    )
                    if (!changeDateNoneFormat.isNullOrEmpty()) {
                        upDrivingInfoBean.licenseValidity = changeDateNoneFormat
                        binding.tvEnd.text = changeDateNoneFormat
                    }
                }
            }
        }
    }


    private fun subMint() {
        if (binding.tvCarType.text.isNullOrEmpty() ||
            binding.etCarNum.text.isNullOrEmpty() ||
            binding.tvCarColor.text.isNullOrEmpty() ||
            upDrivingInfoBean.licenseFaceUrl.isNullOrEmpty() ||
            upDrivingInfoBean.licenseBackUrl.isNullOrEmpty() ||
            binding.tvEnd.text.isNullOrEmpty()
        ) {
            showToast("请完善证件信息")
            return
        }


        upDrivingInfoBean.run {
            vehicleNum = binding.etCarNum.text.trim().toString()
            vehicleType = binding.tvCarType.text.trim().toString()//车辆类型
            licenseValidity = binding.tvEnd.text.trim().toString()
            trailerPlateColor = binding.tvCarColor.text.trim().toString()
            carId = this@AuthTrailerActivity.carId
        }

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