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.OssBean
import com.dahe.gldriver.bean.UpPersonInfoBean
import com.dahe.gldriver.databinding.ActivityAuthPersonBinding
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.glex.bean.UserBean
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.PopsUtils
import com.dahe.mylibrary.utils.TimeUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.functions.Function
import io.reactivex.rxjava3.schedulers.Schedulers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * @ClassName AuthRoleActivity
 * @Author 用户
 * @Date 2024/1/25 11:15
 * @Description 身份证认证
 */
class AuthPersonActivity : BaseActivity<ActivityAuthPersonBinding>() {


    lateinit var ossServiceUtil: OssServiceUtil

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

        ossServiceUtil = OssServiceUtil.getInstance()
        binding.run {
            var inBinding = this
            btnOk.setOnClickListener {

                perInfoBean.run {
                    idcardNo = inBinding.etId.text.toString()
                    idcardName = inBinding.etName.text.toString()
                    idcardEffectiveStart = inBinding.tvStart.text.toString()
                    idcardEffectiveEnd = inBinding.tvEnd.text.toString()
                }

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


            }
            tvStart.run {
                setOnClickListener {
                    PickerUtils.getInstance()
                        .showDate(this@AuthPersonActivity, "有效起始日期") { date ->
                            this.text = date
                        }
                }
            }
            tvEnd.run {
                setOnClickListener {
                    PickerUtils.getInstance()
                        .showDate(this@AuthPersonActivity, "有效结束日期") { date ->
                            this.text = date
                        }
                }
            }

            ivFront.setOnClickListener {
                OcrUtils.getInstance().ocrPerson(mContext,this@AuthPersonActivity){picPath,result->
                    ImageLoader.getInstance().loadRoundImage(mContext, picPath.locPic, 12, ivFront)
                    perInfoBean.idcardFrontUrl = picPath.picPath
                    result.data.face?.data.let {
                        it?.run {
                            perInfoBean.run {
                                idcardNo = idNumber
                                idcardName = name
                                idcardEthnicity = ethnicity
                                idcardSex = if (sex == "男") "0" else "1"
                            }
                        }
                        binding.run {
                            etName.setText(it?.name)
                            etId.setText(it?.idNumber)
                        }
                    }
                }
            }

            ivBack.setOnClickListener {
                PopsUtils.getInstance().showSimSelPic(mContext) {
                    var data = it[0]


                    val path: String
                    if (data.isCut && !data.isCompressed) {
                        // 裁剪过
                        path = data.cutPath
                    } else if (data.isCompressed || (data.isCut && data.isCompressed)) {
                        // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                        path = data.compressPath
                    } else {
                        // 原图
                        path = data.path
                    }
                    ImageLoader.getInstance().loadRoundImage(mContext, data.realPath, 12, ivBack)

                    val file = File(data.realPath)
                    val filePic: MultipartBody.Part = MultipartBody.Part.createFormData(
                        "file",
                        file.getName(),
                        file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                    )

                    DataManager.getInstance().uploadToOss(filePic).flatMap {
                        perInfoBean.idcardBackUrl = it.data.url
                        DataManager.getInstance().recognizeIdcard(it.data.url)
                    }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                            BaseObserver(
                                mContext,
                                object : RxHttpCallBack<OcrPersonBean>(this@AuthPersonActivity) {
                                    override fun onSuccess(t: CommonResponseBean<OcrPersonBean>) {
                                        super.onSuccess(t)
                                        t.data.data.back?.let {
                                            if (it.data.validPeriod?.contains("-") == true) {
                                                val split = it.data.validPeriod.split("-")
                                                binding.run {
                                                    tvStart.text = TimeUtil.changeDateFormat(split[0])
                                                    tvEnd.text =
                                                        if (split.size == 2) TimeUtil.changeDateFormat(split[1]) else ""
                                                }
                                            }
                                        }
                                    }
                                })
                        )
                }
            }
        }


    }

    override fun initDate() {
    }
}