package com.dahe.gldriver.utils

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import com.dahe.gldriver.bean.OcrPersonBean
import com.dahe.gldriver.callback.OnOcrPicResultListener
import com.dahe.gldriver.callback.PicPath
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.SingletonNoPHolder
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.LoadingUtils
import com.dahe.mylibrary.utils.PopsUtils
import com.dahe.mylibrary.utils.ToastUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName OcrUtils
 * @Author john
 * @Date 2024/2/28 17:11
 * @Description TODO
 */
class OcrUtils private constructor() {

    companion object : SingletonNoPHolder<OcrUtils>(::OcrUtils)


    /**
     * 身份证识别
     * */
    fun ocrPerson(context: Context, activity: AppCompatActivity, listener: OnOcrPicResultListener) {
        PopsUtils.getInstance().showSimSelPic(context) { result ->
            LoadingUtils.instance.showLoading(activity)
            //上传oss得到图片地址
            OssServiceUtil.getInstance().run {
                asyncPutImage(result[0].realPath, null)
                setResultCallBack { data, oldPath ->
                    DataManager.getInstance().recognizeIdcard(oldPath)
                        .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                        .subscribe(BaseObserver(context, object : RxHttpCallBack<OcrPersonBean>() {
                            override fun onSuccess(t: CommonResponseBean<OcrPersonBean>) {
                                super.onSuccess(t)
                                LoadingUtils.instance.dissLoading()
                                t.data?.let {
                                    listener.onSuccResult(
                                        PicPath(result[0].realPath, oldPath), it
                                    )
                                }
                            }

                            override fun onCodeError(
                                mContext: Context?,
                                t: CommonResponseBean<OcrPersonBean>
                            ) {
//                                super.onCodeError(mContext, t)
                                LoadingUtils.instance.dissLoading()
                                if (t.code == 500) {
                                    if (t.msg.contains("code: 400, The image type does not match the API operation")) {
                                        ToastUtils.showToast(mContext, "图片识别失败，请重新上传")
                                    } else {
                                        ToastUtils.showToast(mContext, t.msg)
                                    }
                                }
                            }
                        }))
                }
            }
        }
    }

    /**
     * 驾驶证ocr识别
     * */
    fun ocrDriver(context: Context, activity: AppCompatActivity, listener: OnOcrPicResultListener) {
        PopsUtils.getInstance().showSimSelPic(context) { result ->
            LoadingUtils.instance.showLoading(activity)
            //上传oss得到图片地址
            OssServiceUtil.getInstance().run {
                asyncPutImage(result[0].realPath, null)
                setResultCallBack { data, oldPath ->
                    DataManager.getInstance().recognizeDrivingLicense(oldPath)
                        .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                            BaseObserver(
                                context,
                                object : RxHttpCallBack<OcrPersonBean>() {
                                    override fun onSuccess(t: CommonResponseBean<OcrPersonBean>) {
                                        super.onSuccess(t)
                                        LoadingUtils.instance.dissLoading()
                                        t.data?.let {
                                            listener.onSuccResult(
                                                PicPath(
                                                    result[0].realPath,
                                                    oldPath
                                                ), it
                                            )
                                        }
                                    }

                                    override fun onCodeError(
                                        mContext: Context?,
                                        t: CommonResponseBean<OcrPersonBean>
                                    ) {
//                                        super.onCodeError(mContext, t)
                                        LoadingUtils.instance.dissLoading()
                                        if (t.code == 500) {
                                            if (t.msg.contains("code: 400, The image type does not match the API operation")) {
                                                ToastUtils.showToast(
                                                    mContext,
                                                    "图片识别失败，请重新上传"
                                                )
                                            } else {
                                                ToastUtils.showToast(mContext, t.msg)
                                            }
                                        }
                                    }
                                })
                        )
                }
            }
        }
    }


    /**
     * 银行卡ocr识别
     * */
    fun ocrBankCard(
        context: Context,
        activity: AppCompatActivity,
        listener: OnOcrPicResultListener
    ) {
        PopsUtils.getInstance().showSimSelPic(context) { result ->
            //上传oss得到图片地址
            OssServiceUtil.getInstance().run {
                asyncPutImage(result[0].realPath, activity)
                setResultCallBack { data, oldPath ->
                    DataManager.getInstance().recognizeBankCard(oldPath)
                        .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                            BaseObserver(
                                context,
                                object : RxHttpCallBack<OcrPersonBean>(activity) {
                                    override fun onSuccess(t: CommonResponseBean<OcrPersonBean>) {
                                        super.onSuccess(t)
                                        t.data?.let {
                                            listener.onSuccResult(
                                                PicPath(
                                                    result[0].realPath,
                                                    oldPath
                                                ), it
                                            )
                                        }
                                    }

                                    override fun onCodeError(
                                        mContext: Context?,
                                        t: CommonResponseBean<OcrPersonBean>
                                    ) {
//                                        super.onCodeError(mContext, t)
                                        LoadingUtils.instance.dissLoading()
                                        if (t.code == 500) {
                                            if (t.msg.contains("code: 400, The image type does not match the API operation")) {
                                                ToastUtils.showToast(
                                                    mContext,
                                                    "图片识别失败，请重新上传"
                                                )
                                            } else {
                                                ToastUtils.showToast(mContext, t.msg)
                                            }
                                        }
                                    }
                                })
                        )
                }
            }
        }
    }


    /**
     * 营业执照ocr识别
     * */
    fun ocrBusinessCard(
        context: Context,
        activity: AppCompatActivity,
        listener: OnOcrPicResultListener
    ) {
        PopsUtils.getInstance().showSimSelPic(context) { result ->
            //上传oss得到图片地址
            OssServiceUtil.getInstance().run {
                asyncPutImage(result[0].realPath, activity)
                setResultCallBack { data, oldPath ->
                    DataManager.getInstance().recognizeBusinessLicense(oldPath)
                        .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                            BaseObserver(
                                context,
                                object : RxHttpCallBack<OcrPersonBean>(activity) {
                                    override fun onSuccess(t: CommonResponseBean<OcrPersonBean>) {
                                        super.onSuccess(t)
                                        t.data?.let {
                                            listener.onSuccResult(
                                                PicPath(
                                                    result[0].realPath,
                                                    oldPath
                                                ), it
                                            )
                                        }
                                    }

                                    override fun onCodeError(
                                        mContext: Context?,
                                        t: CommonResponseBean<OcrPersonBean>
                                    ) {
//                                        super.onCodeError(mContext, t)
                                        LoadingUtils.instance.dissLoading()
                                        if (t.code == 500) {
                                            if (t.msg.contains("code: 400, The image type does not match the API operation")) {
                                                ToastUtils.showToast(
                                                    mContext,
                                                    "图片识别失败，请重新上传"
                                                )
                                            } else {
                                                ToastUtils.showToast(mContext, t.msg)
                                            }
                                        }
                                    }
                                })
                        )
                }
            }
        }
    }
}