package com.adealink.weparty.playmate.setting.viewmodel

import androidx.lifecycle.LiveData
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.playmate.data.SkillSettingListReq
import com.adealink.weparty.playmate.data.SwitchSkillReq
import com.adealink.weparty.playmate.datasource.remote.PlaymateHttpService
import com.adealink.weparty.playmate.setting.data.SkillSettingItemData
import kotlinx.coroutines.launch

class SkillSettingViewModel : BaseViewModel() {

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

    val settingListRltLD = ExtMutableLiveData<Rlt<Any>>()
    val settingListLD = ExtMutableLiveData<List<SkillSettingItemData>>()

    private val settingList = mutableListOf<SkillSettingItemData>()

    fun loadSettingList() {
        viewModelScope.launch {
            val rlt = playmateHttpService.getSkillSettingList(SkillSettingListReq(null))
            when (rlt) {
                is Rlt.Failed -> {
                    settingListRltLD.send(rlt)
                }

                is Rlt.Success -> {
                    settingListRltLD.send(rlt)
                    settingList.clear()
                    settingList.addAll(
                        rlt.data.data?.list?.map {
                            SkillSettingItemData(it)
                        } ?: emptyList()
                    )
                    settingListLD.send(settingList)
                }
            }
        }
    }

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


}