package com.za.ui.order_report

import androidx.compose.ui.graphics.Color
import com.blankj.utilcode.util.ToastUtils
import com.za.base.BaseVm
import com.za.base.view.LoadingManager
import com.za.bean.ReportInfoRequest
import com.za.bean.ReportItem
import com.za.common.GlobalData
import com.za.net.BaseObserver
import com.za.net.RetrofitHelper
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class OrderReportVm : BaseVm<OrderReportVm.Action, OrderReportVm.UiState>() {
    private val _uiState = MutableStateFlow(UiState())
    val uiState get() = _uiState.asStateFlow()

    override fun updateState(uiState: UiState) {
        _uiState.value = uiState
    }

    override fun dispatch(action: Action) {
        when (action) {
            is Action.Init -> getReportList()
            is Action.SelectReportItem -> {
                updateState(uiState.value.copy(selectReportItem = action.item, reportContent = null))
            }

            is Action.Submit -> {
                submitReport()
            }
        }
    }

    private fun submitReport() {
        if (uiState.value.selectReportItem == null) {
            ToastUtils.showLong("请选择需要报备的类型")
            return
        }

        if (uiState.value.reportContent.isNullOrEmpty()) {
            ToastUtils.showLong("请输入报备内容")
            return
        }

        LoadingManager.showLoading()
        val reportInfoRequest = ReportInfoRequest(
            taskId = "${GlobalData.currentOrder?.taskId}",
            reportType = uiState.value.selectReportItem?.reportType ?: "",
            reportTemplate = "${uiState.value.selectReportItem?.reportTemplate?.replace("_", "")}${uiState.value.reportContent}")
        RetrofitHelper.getDefaultService().submitReport(reportInfoRequest)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BaseObserver<String>() {
                override fun doSuccess(it: String?) {
                    LoadingManager.hideLoading()
                    ToastUtils.showShort("提交成功")
                    updateState(uiState.value.copy(success = true))
                }

                override fun doFailure(code: Int, msg: String?) {
                    LoadingManager.hideLoading()
                }
            })
    }

    private fun getReportList() {
        LoadingManager.showLoading()
        RetrofitHelper.getDefaultService().getReportTemplates()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BaseObserver<List<ReportItem>>() {
                override fun doSuccess(it: List<ReportItem>?) {
                    LoadingManager.hideLoading()
                    updateState(uiState.value.copy(reportList = it))
                }

                override fun doFailure(code: Int, msg: String?) {
                    LoadingManager.hideLoading()
                }
            })
    }

    sealed class Action {
        data object Init : Action()
        data class SelectReportItem(val item: ReportItem) : Action()
        data object Submit : Action()
    }

    data class UiState(
        val itemColors: ArrayList<Color> = arrayListOf(Color(color = 0xFFFEE2E2),
            Color(color = 0xFFDBEAFE),
            Color(0xFFFCE7F3),
            Color(0xFFE0E7FF),
            Color(color = 0xFFDCFCE7),
            Color(color = 0xFFFEF9C3),
            Color(0xFFF3E8FF)),
        val reportList: List<ReportItem>? = null,
        val selectReportItem: ReportItem? = null,
        val reportContent: String? = null,
        val success: Boolean? = false
    )
}