package com.dahe.gldriver.ui.account.authteam

import android.os.Bundle
import android.view.View
import com.dahe.gldriver.R
import com.dahe.gldriver.bean.AuthTeamBean
import com.dahe.gldriver.bean.OcrPersonBean
import com.dahe.gldriver.bean.UpPersonInfoBean
import com.dahe.gldriver.callback.PicPath
import com.dahe.gldriver.databinding.ActivityAuthTeamBinding
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.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ImageLoader
import com.dahe.mylibrary.utils.LoadingUtils
import com.dahe.mylibrary.utils.LoadingUtils3
import com.dahe.mylibrary.utils.PickerUtils
import com.dahe.mylibrary.utils.PopsUtils
import com.dahe.mylibrary.utils.TimeUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * @ClassName AuthTeamActivity
 * @Author john
 * @Date 2024/1/25 16:44
 * @Description 个人车队认证
 */
class AuthTeamActivity : BaseActivity<ActivityAuthTeamBinding>(), View.OnClickListener {
    private var checkTeamType = 1  //1代表运输公司，0代表个人车队

    var fff : MutableList<String> = mutableListOf("sdfsdf","asdfasdf")

    var ossServiceUtil: OssServiceUtil = OssServiceUtil.getInstance()

    var upComInfoBean: AuthTeamBean = AuthTeamBean("1")//公司车队
    var upPerInfoBean: AuthTeamBean = AuthTeamBean("0")//个人车队

    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("身份认证", true)
        binding.run {
            rgTeam.setOnCheckedChangeListener { _, i ->
                changeTeamType(if (i == binding.rbCom.id) 1 else 0)
            }
            //营业执照选择。ocr识别
            ivBusiness.setOnClickListener {
                OcrUtils.getInstance()
                    .ocrBusinessCard(mContext, this@AuthTeamActivity) { picPath, result ->
                        upComInfoBean.businessLicensePhoneUrl = picPath.picPath

                        result.data?.run {
                            upComInfoBean.businessLicenseCreditCode = creditCode
                            upComInfoBean.businessLicenseEffectiveStart= TimeUtil.changeDateNoneFormat(validFromDate)
                            upComInfoBean.businessLicenseEffectiveEnd=TimeUtil.changeDateNoneFormat(validToDate)
                        }
                        ImageLoader.getInstance()
                            .loadRoundImage(mContext, picPath.locPic, 12, ivBusiness)
                    }
            }

            //运输公司  本人身份证
            ivComFront.setOnClickListener {
                OcrUtils.getInstance()
                    .ocrPerson(mContext, this@AuthTeamActivity) { picPath, result ->
                        ImageLoader.getInstance()
                            .loadRoundImage(mContext, picPath.locPic, 12, ivComFront)
                        result.data.face?.data?.run {
                            etName.setText(name)
                            etId.setText(idNumber)
                            upComInfoBean.run {
                                idcardNo = idNumber
                                idcardName = name
                                idcardSex = if (sex == "男") "1" else "2"
                                idcardEthnicity = ethnicity
                                idcardFrontUrl = picPath.picPath
                                idcardAddress = address
                            }
                        }
                    }
            }

            ivComBack.setOnClickListener {
                OcrUtils.getInstance()
                    .ocrPerson(mContext, this@AuthTeamActivity) { picPath, result ->
                        ImageLoader.getInstance()
                            .loadRoundImage(mContext, picPath.locPic, 12, ivComBack)
                        result.data.back?.data?.run {
                            if (validPeriod.contains("-")) {
                                val split = validPeriod.split("-")
                                tvStart.text = TimeUtil.changeDateFormat(split[0])
                                tvEnd.text = if (split.size == 2) TimeUtil.changeDateFormat(split[1]) else ""
                                upComInfoBean.run {
//                                    idcardEffectiveStart = TimeUtil.changeDateFormat(split[0])
//                                    idcardEffectiveEnd = if (split.size == 2) TimeUtil.changeDateFormat(split[1]) else ""
                                    idcardBackUrl = picPath.picPath
                                }
                            }
                        }
                    }
            }
            //个人车队 本人身份证
            ivFront.setOnClickListener {
                OcrUtils.getInstance()
                    .ocrPerson(mContext, this@AuthTeamActivity) { picPath, result ->
                        ImageLoader.getInstance()
                            .loadRoundImage(mContext, picPath.locPic, 12, ivFront)
                        result.data.face?.data?.run {
                            etPerName.setText(name)
                            etPerId.setText(idNumber)

                            upPerInfoBean.run {
                                idcardNo = idNumber
                                idcardName = name
                                idcardSex = if (sex == "男") "1" else "2"
                                idcardEthnicity = ethnicity
                                idcardFrontUrl = picPath.picPath
                                idcardAddress = address
                            }
                        }
                    }
            }

            //个人车队 本人身份证
            ivBack.setOnClickListener {
                OcrUtils.getInstance()
                    .ocrPerson(mContext, this@AuthTeamActivity) { picPath, result ->
                        ImageLoader.getInstance()
                            .loadRoundImage(mContext, picPath.locPic, 12, ivBack)
                        result.data.back?.data?.run {
                            if (validPeriod.contains("-")) {
                                val split = validPeriod.split("-")
                                tvPerStart.text = TimeUtil.changeDateFormat(split[0])
                                tvPerEnd.text = if (split.size == 2) TimeUtil.changeDateFormat(split[1]) else ""

                                upPerInfoBean.run {
                                    idcardEffectiveStart = tvPerStart.text.toString()
                                    idcardEffectiveEnd = tvPerEnd.text.toString()
                                    idcardBackUrl = picPath.picPath
                                }
                            }
                        }
                    }
            }

            /**
             * 个人银行卡
             */
            ivBank.setOnClickListener {
                OcrUtils.getInstance()
                    .ocrBankCard(mContext, this@AuthTeamActivity) { picPath, result ->
                        ImageLoader.getInstance()
                            .loadRoundImage(mContext, picPath.locPic, 12, ivBank)
                        result.data.run {
                            etPerBankId.setText(cardNumber)
                            tvPerBankName.setText(bankName)
                            upPerInfoBean.run {
                                captainBankcardNumber = cardNumber
                                captainBankcardPhotoUrl = picPath.picPath
                            }
                        }
                    }
            }
            //道路运输许可证
            ivRoad.setOnClickListener {
                PopsUtils.getInstance().showSimSelPic(mContext) {
//                    upComInfoBean.roadLicensePhotoUrl = it[0].realPath
                    ImageLoader.getInstance().loadRoundImage(mContext, it[0].realPath, 12, ivRoad)
                }
            }

            //公司银行卡
            ivComBank.setOnClickListener {
                OcrUtils.getInstance()
                    .ocrBankCard(mContext, this@AuthTeamActivity) { picPath, result ->
                        ImageLoader.getInstance()
                            .loadRoundImage(mContext, picPath.locPic, 12, ivComBank)
                        result.data?.run {
                            binding.let {
                                it.etComBankId.setText(cardNumber)
                                it.etComBankName.setText(bankName)
                            }
                            upComInfoBean.run {
                                captainBankcardNumber = cardNumber
                                captainBankcardPhotoUrl = picPath.picPath
                            }
                        }
                    }
            }

            tvComDate.setOnClickListener {
                PickerUtils.getInstance()
                    .showDate(this@AuthTeamActivity, "请选择有效期至") { date->
                        tvComDate.text = date
                    }
            }

            tvPerStart.setOnClickListener {
                PickerUtils.getInstance()
                    .showDate(this@AuthTeamActivity, "请选择有效期至") { date ->
                        tvPerStart.text = date
                    }
            }

            btnOk.setOnClickListener(this@AuthTeamActivity)
        }
    }

    override fun initDate() {
    }

    private fun changeTeamType(type: Int) {
        checkTeamType = type
        if (1 == checkTeamType) {
            binding.llCom.visibility = View.VISIBLE
            binding.llPerson.visibility = View.GONE
        } else {
            binding.llCom.visibility = View.GONE
            binding.llPerson.visibility = View.VISIBLE
        }
    }

    override fun onClick(view: View?) {
        super.onClick(view)
        when (view?.id) {
            binding.btnOk.id -> {
                DataManager.getInstance()
                    .authenticationCaptain(if (checkTeamType == 1) upComInfoBean else upPerInfoBean)
                    .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(BaseObserver(mContext, object : RxHttpCallBack<String>(this) {
                        override fun onSuccess(t: CommonResponseBean<String>) {
                            super.onSuccess(t)
                        }
                    }))
            }

            binding.ivBusiness.id -> {}
            binding.ivComFront.id -> {}
            binding.ivComBack.id -> {}
            binding.ivComBank.id -> {}
            binding.tvComDate.id -> {}
            binding.tvPerStart.id -> {}
            binding.ivFront.id -> {}
            binding.ivBack.id -> {}
            binding.ivBank.id -> {}
            binding.ivRoad.id -> {}

        }
    }

    private fun subMit() {

    }


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

                                            binding.let {
                                                it.etComBankId.setText(cardNumber)
                                                it.etComBankName.setText(bankName)
                                            }
                                        }

                                    }
                                })
                        )

                }
            }
            ImageLoader.getInstance()
                .loadRoundImage(mContext, it[0].realPath, 12, binding.ivComBank)
        }
    }

    private fun selPerPic() {
        PopsUtils.getInstance().showSimSelPic(mContext) {

            ImageLoader.getInstance().loadRoundImage(mContext, it[0].realPath, 12, binding.ivFront)

            ossServiceUtil.run {
                asyncPutImage(it[0].realPath, this@AuthTeamActivity)
                setResultCallBack { data, oldPath ->
                    DataManager.getInstance().recognizeIdcard(oldPath)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                            BaseObserver(mContext,
                                object : RxHttpCallBack<OcrPersonBean>(this@AuthTeamActivity) {
                                    override fun onSuccess(t: CommonResponseBean<OcrPersonBean>) {
                                        super.onSuccess(t)
                                        t.data.data.face?.data.let {
                                            it?.run {
//                                                perInfoBean.run {
//                                                    idcardNo = idNumber
//                                                    idcardName = name
//                                                    idcardSex = if (sex == "男") "1" else "2"
//                                                }
                                            }
                                            binding.run {
                                                etName.setText(it?.idNumber)
                                                etId.setText(it?.name)
                                            }
                                        }

                                    }
                                })
                        )
                }
            }
        }
    }
}

