package com.dahe.gldriver.utils

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import com.dahe.gldriver.bean.DiCarColor
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.mylibrary.base.SingletonNoPHolder
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.PickerUtils
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName DicUtils
 * @Author john
 * @Date 2024/3/11 09:32
 * @Description 字典工具类
 */
class DicUtils private constructor() {
    companion object : SingletonNoPHolder<DicUtils>(::DicUtils)


    /**
     * 选择车辆类型
     * @param context Context
     * @param activity AppCompatActivity
     * @param listener OnOptionPickedListener
     */
    fun selectDicCarType(
        context: Context,
        activity: AppCompatActivity,
        defaultCont: String = "重型半挂牵引车",
        isLoading: Boolean = false,
        listener: OnOptionPickedListener
    ) {
        //车辆类型
        DataManager.getInstance().getSysDictDataList("vehicle_type")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                BaseObserver(context, object : RxHttpCallBack<MutableList<DiCarColor>>(if (isLoading) activity else null) {
                    override fun onSuccess(t: CommonResponseBean<MutableList<DiCarColor>>) {
                        super.onSuccess(t)
                        PickerUtils.getInstance()
                            .showSelectCarColor(
                                activity,
                                "车辆类型",
                                "重型半挂牵引车",
                                t.data, listener
                            )
                    }
                })
            )
    }

    /**
     * 根据名称获取车辆类型
     * @param context Context
     * @param value String
     * @param listener OnOptionPickedListener
     */
    fun getCarTypeByValue(
        context: Context,
        value: String,
        listener: OnOptionPickedListener
    ) {
        //车辆类型
        DataManager.getInstance().getSysDictDataList("vehicle_type")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                BaseObserver(context, object : RxHttpCallBack<MutableList<DiCarColor>>() {
                    override fun onSuccess(t: CommonResponseBean<MutableList<DiCarColor>>) {
                        super.onSuccess(t)
                        var first = t.data?.firstOrNull { it.dictLabel == value }
                        listener.onOptionPicked(0, first)
                    }
                })
            )
    }


    /**
     * 设置车牌颜色
     * @param context Context
     * @param appCompatActivity AppCompatActivity
     * @param listener OnOptionPickedListener
     */
    fun getCarColor(
        context: Context, appCompatActivity: AppCompatActivity,
        isLoading : Boolean=false,
        listener: OnOptionPickedListener
    ) {
        DataManager.getInstance().getSysDictDataList("plate_color")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                BaseObserver(context, object : RxHttpCallBack<MutableList<DiCarColor>>(if (isLoading) appCompatActivity else null) {
                    override fun onSuccess(t: CommonResponseBean<MutableList<DiCarColor>>) {
                        super.onSuccess(t)
                        PickerUtils.getInstance()
                            .showSelectCarColor(
                                appCompatActivity,
                                "车牌颜色",
                                "黄色",
                                t.data, listener
                            )
                    }
                })
            )
    }
}