package com.dahe.gldriver.utils

import android.content.Context
import android.os.Bundle
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.CarBean
import com.dahe.gldriver.callback.OnResultListener
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.ui.waybill.activity.WaybillLoadActivity
import com.dahe.mylibrary.base.SingletonNoPHolder
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ActivityUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName OrderUtils
 * @Author john
 * @Date 2024/3/4 14:48
 * @Description 订单状态转换
 */
class OrderUtils private constructor() {

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


    fun getOrderStatu(statu: String): String {
        return when (statu) {
            "102010" -> "草稿箱"
            "102020" -> "待调度"
            "102030" -> "待接单"
            "102040" -> "待装货"
            "102050" -> "待卸货"
            "102060" -> "待签收"
            "102070" -> "待结算"
            "102090" -> "已完成"
            "103000" -> "已取消"
            else -> {
                ""
            }
        }
    }


    fun getNextByStatu(statu: String): String {
        return when (statu) {
            "102010" -> "草稿箱"
            "102020" -> "待调度"
            "102030" -> "去接单"
            "102040" -> "去装货"
            "102050" -> "去卸货"
            "102060" -> "待签收"
            "102070" -> "去评价"
            "102090" -> "去评价"
            "103000" -> "已取消"
            else -> {
                ""
            }
        }
    }

    fun getStatuByString(statu: String): String {
        return when (statu) {
            "草稿箱" -> "102010"
            "待调度" -> "102020"
            "待接单" -> "102030"
            "待装货" -> "102040"
            "待卸货" -> "102050"
            "待签收" -> "102060"
            "待结算" -> "102070"
            "待评价" -> "102080"
            "已取消" -> "103000"
            else -> {
                ""
            }
        }
    }


    /**
     *
     * 接单（包含选择车辆）->装货
     * */
    fun goReceWaybill(context: Context, orderId: String,listener:OnResultListener) {
        //选择车辆
        DataManager.getInstance().carList()
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                BaseObserver(context, object : RxHttpCallBack<MutableList<CarBean>>() {
                    override fun onSuccess(t: CommonResponseBean<MutableList<CarBean>>) {
                        super.onSuccess(t)
                        if (t.data.size > 0) {//多辆车需选择车辆,单个车辆直接接单
                            CommonPopUtils.getInstance()
                                .showCarList(context, t.data) {
                                    //接单接口
                                    receivingOrders(context, orderId, it,listener)
                                }
                        } else {
                            //单量车，直接默认当前车辆 ，接单接口
                            receivingOrders(context, orderId, t.data[0], listener)
                        }
                    }
                })
            )
    }

    /**
     *
     *调用接单接口
     * */
    private fun receivingOrders(
        context: Context,
        orderId: String,
        car: CarBean,
        listener: OnResultListener
    ) {
        DataManager.getInstance()
            .receivingOrders(orderId, car.carId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                BaseObserver(
                    context,
                    object : RxHttpCallBack<Any>() {
                        override fun onSuccess(t: CommonResponseBean<Any>) {
                            super.onSuccess(t)
                            listener.onResult(true)
                            //接单成功，弹窗是否去装货
                            CommonPopUtils.getInstance()
                                .showLoadPop(context, car) {
                                    ActivityUtils.startActivity(context,
                                        WaybillLoadActivity::class.java,
                                        Bundle().apply {
                                            putString(AppConfig.ORDER_ID, orderId)
                                        }
                                    )
                                }
                        }
                    })
            )
    }
}