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.commonui.ext.isSuccess
import com.adealink.weparty.playmate.data.EnableAutoWelcomeReq
import com.adealink.weparty.playmate.data.SetupBusinessTimeReq
import com.adealink.weparty.playmate.datasource.remote.PlaymateHttpService
import com.adealink.weparty.util.TimeHour
import com.adealink.weparty.util.WeekDay
import kotlinx.coroutines.launch

class OrderSettingViewModel : BaseViewModel() {

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

    val timeRangeLD = ExtMutableLiveData<Pair<TimeHour, TimeHour>>()
    val weekDayLD = ExtMutableLiveData<List<WeekDay>>()

    val enableAutoWelcomeLD = ExtMutableLiveData<Boolean>()


    fun getOrderSetting(): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = playmateHttpService.getBusinessTime()
            liveData.send(rlt)
            when (rlt) {
                is Rlt.Failed -> {

                }

                is Rlt.Success -> {
                    val data = rlt.data.data ?: return@launch
                    data.getTimeFromTo()?.let {
                        timeRangeLD.send(it)
                    }
                    data.getWeekDay().let {
                        weekDayLD.send(it)
                    }
                    enableAutoWelcomeLD.send(data.autoWelcomeOpen)
                }
            }
        }
        return liveData
    }

    fun updateTimeRange(from: TimeHour, to: TimeHour): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = playmateHttpService.setupBusinessTime(
                SetupBusinessTimeReq(
                    timeRange = "${from.time()}-${to.time()}",
                    weekDays = weekDayLD.value?.map { it.day } ?: emptyList()
                )
            )
            if (rlt.isSuccess) {
                timeRangeLD.send(Pair(from, to))
            }
            liveData.send(rlt)
        }
        return liveData
    }

    fun updateWeekDay(weekDays: List<WeekDay>): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = playmateHttpService.setupBusinessTime(
                SetupBusinessTimeReq(
                    timeRange = "${timeRangeLD.value?.first?.time()}-${timeRangeLD.value?.second?.time()}",
                    weekDays = weekDays.map { it.day },
                )
            )
            if (rlt.isSuccess) {
                weekDayLD.send(weekDays)
            }
            liveData.send(rlt)
        }
        return liveData
    }


    fun updateAutoWelcome(enable: Boolean): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = playmateHttpService.enableAutoWelcome(
                EnableAutoWelcomeReq(enable)
            )
            if (rlt.isSuccess) {
                enableAutoWelcomeLD.send(enable)
            }
            liveData.send(rlt)
        }
        return liveData
    }
}