package com.adealink.weparty.playmate.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.CommonDataNullError
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.App
import com.adealink.weparty.module.playmate.data.PlaymateBusinessData
import com.adealink.weparty.module.playmate.data.PlaymateCategoryConditionData
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.module.playmate.data.PlaymateDetailData
import com.adealink.weparty.module.playmate.data.SkillSettingData
import com.adealink.weparty.module.playmate.data.UserPlaymateCategoryData
import com.adealink.weparty.module.playmate.viewmodel.IPlaymateViewModel
import com.adealink.weparty.playmate.data.GetSkillSwitchReq
import com.adealink.weparty.playmate.data.PlaymateDetailReq
import com.adealink.weparty.playmate.data.SetupBusinessTimeReq
import com.adealink.weparty.playmate.data.SkillSettingListReq
import com.adealink.weparty.playmate.data.SkillSwitchData
import com.adealink.weparty.playmate.data.SwitchBusinessReq
import com.adealink.weparty.playmate.data.SwitchSkillReq
import com.adealink.weparty.playmate.datasource.remote.PlaymateHttpService
import com.adealink.weparty.playmate.manager.playmateManager
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class PlaymateViewModel : BaseViewModel(), IPlaymateViewModel {

    override val allCategoryLD = MutableLiveData<List<PlaymateCategoryData>>()
    val allCategoryRltLD = ExtMutableLiveData<Rlt<Any>>()

    override val allCategoryConditionLD =
        MutableLiveData<Map<String, PlaymateCategoryConditionData>>()

    private val playmateHttpService by lazy {
        App.instance.networkService.getHttpService(PlaymateHttpService::class.java)
    }


    override fun pullAllCategory() {
        viewModelScope.launch {
            val rlt = playmateManager.pullAllCategory()
            when (rlt) {
                is Rlt.Failed -> {
                    allCategoryLD.send(emptyList())
                }

                is Rlt.Success -> {
                    allCategoryLD.send(rlt.data)
                }
            }
            allCategoryRltLD.send(rlt)
        }
    }

    override fun pullAllCategoryCondition() {
        viewModelScope.launch {
            val categoryDef = async { playmateManager.pullAllCategory() }
            val conditionDef = async { playmateManager.pullAllCategoryCondition() }
            val categoryListRlt = categoryDef.await()
            val conditionMap = conditionDef.await()
            allCategoryRltLD.send(categoryListRlt)
            allCategoryLD.send((categoryListRlt as? Rlt.Success)?.data ?: emptyList())
            allCategoryConditionLD.send(conditionMap)
        }
    }

    override fun getUserPlaymateCategory(
        uid: String,
        cache: Boolean
    ): LiveData<Rlt<List<UserPlaymateCategoryData>>> {
        val liveData = OnceMutableLiveData<Rlt<List<UserPlaymateCategoryData>>>()
        viewModelScope.launch {
            val rlt = playmateManager.getUserPlaymateCategory(uid, cache)
            liveData.send(rlt)
        }
        return liveData
    }

    override fun loadPlaymateDetail(id: String?): LiveData<Rlt<PlaymateDetailData>> {
        val liveData = OnceMutableLiveData<Rlt<PlaymateDetailData>>()
        viewModelScope.launch {
            if (id.isNullOrEmpty()) {
                liveData.send(Rlt.Failed(CommonDataNullError()))
                return@launch
            }
            val rlt = playmateHttpService.getPlaymateDetail(PlaymateDetailReq(id))
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    if (data == null) {
                        liveData.send(Rlt.Failed(CommonDataNullError()))
                    } else {
                        liveData.send(Rlt.Success(data))
                    }
                }
            }
        }
        return liveData
    }

    override fun openBusiness(open: Boolean): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            liveData.send(playmateHttpService.switchBusiness(SwitchBusinessReq(open)))
        }
        return liveData
    }

    val skillSwitchLD = ExtMutableLiveData<SkillSwitchData?>()
    fun getBusinessOpenData(playmateId: String): LiveData<Rlt<SkillSwitchData?>> {
        val liveData = OnceMutableLiveData<Rlt<SkillSwitchData?>>()
        viewModelScope.launch {
            val rlt = playmateHttpService.getSkillSwitch(
                GetSkillSwitchReq(
                    playmateId
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    skillSwitchLD.send(null)
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    skillSwitchLD.send(data)
                    liveData.send(Rlt.Success(data))
                }
            }
        }
        return liveData
    }

    fun openBusiness(playmateId: String, open: Boolean, mianSkill: Boolean): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            liveData.send(
                playmateHttpService.switchSkill(
                    SwitchSkillReq(
                        playmateId, open, mianSkill
                    )
                )
            )
        }
        return liveData
    }

    override fun getSkillSettingList(queryOpen: Boolean?): LiveData<Rlt<List<SkillSettingData>>> {
        val liveData = OnceMutableLiveData<Rlt<List<SkillSettingData>>>()
        viewModelScope.launch {
            val rlt = playmateHttpService.getSkillSettingList(
                SkillSettingListReq(queryOpen)
            )
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    liveData.send(
                        Rlt.Success(
                            rlt.data.data?.list ?: emptyList()
                        )
                    )
                }
            }
        }
        return liveData
    }

    override fun setBusinessTime(
        fromTime: String,
        toTime: String,
        weekDays: List<Int>
    ): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            liveData.send(
                playmateHttpService.setupBusinessTime(
                    SetupBusinessTimeReq(
                        timeRange = "${fromTime}-${toTime}",
                        weekDays = weekDays
                    )
                )
            )
        }
        return liveData
    }

    override fun getBusinessInfo(): LiveData<Rlt<PlaymateBusinessData>> {
        val liveData = OnceMutableLiveData<Rlt<PlaymateBusinessData>>()
        viewModelScope.launch {
            val rlt = playmateHttpService.getPlaymateBusinessInfo()
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    if (data == null) {
                        liveData.send(Rlt.Failed(CommonDataNullError()))
                    } else {
                        liveData.send(Rlt.Success(data))
                    }
                }
            }
        }
        return liveData
    }

}