package com.adealink.weparty.playmate.list.viewmodel

import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.network.data.PageReq
import com.adealink.weparty.App
import com.adealink.weparty.network.data.NoMoreDataError
import com.adealink.weparty.playmate.data.PlaymateListData
import com.adealink.weparty.playmate.data.PlaymateListPriceSort
import com.adealink.weparty.playmate.data.PlaymateListReq
import com.adealink.weparty.playmate.data.PlaymateListStarSort
import com.adealink.weparty.playmate.datasource.remote.PlaymateHttpService
import com.adealink.weparty.util.PageHandler
import kotlinx.coroutines.launch


class GuestPlaymateListViewModel : BaseViewModel() {

    companion object {
        /**
         * 利用缓存数据,提升路畅
         */
        private val playmateList = mutableListOf<PlaymateListData>()
        private val pageHandler = PageHandler()
    }

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

    val listLD = MutableLiveData<List<PlaymateListData>>()
    val listRltLD = ExtMutableLiveData<Rlt<Any>>()

    private var categoryCode: String? = null
    val starRankLD = ExtMutableLiveData<PlaymateListStarSort>()

    val priceRankLD = ExtMutableLiveData<PlaymateListPriceSort>()

    fun setCategoryCode(code: String?) {
        categoryCode = code
        pageHandler.reset()
        playmateList.clear()
    }

    fun changeStarSort() {
        val current = starRankLD.value ?: PlaymateListStarSort.DEFAULT
        val next = when (current) {
            PlaymateListStarSort.ASC -> {
                PlaymateListStarSort.DESC
            }

            PlaymateListStarSort.DESC -> {
                PlaymateListStarSort.DEFAULT
            }

            PlaymateListStarSort.DEFAULT -> {
                PlaymateListStarSort.ASC
            }
        }
        this.starRankLD.send(next)
    }

    fun changePriceSort() {
        val current = priceRankLD.value ?: PlaymateListPriceSort.DEFAULT
        val next = when (current) {
            PlaymateListPriceSort.ASC -> {
                PlaymateListPriceSort.DESC
            }

            PlaymateListPriceSort.DESC -> {
                PlaymateListPriceSort.DEFAULT
            }

            PlaymateListPriceSort.DEFAULT -> {
                PlaymateListPriceSort.ASC
            }
        }
        this.priceRankLD.send(next)
    }

    fun refreshList(cache: Boolean) {
        viewModelScope.launch {
            if(cache){
                if(playmateList.isNotEmpty()){
                    listRltLD.send(Rlt.Success(Unit))
                    listLD.send(playmateList)
                    return@launch
                }
            }
            pageHandler.reset()
            playmateList.clear()
            pullList()
        }
    }

    fun loadMoreList() {
        viewModelScope.launch {
            pullList()
        }
    }

    private suspend fun pullList() {
        if (pageHandler.isEnd) {
            listRltLD.send(Rlt.Failed(NoMoreDataError()))
            return
        }
        val rlt = playmateHttpService.getPlaymateList(
            PlaymateListReq(
                code = categoryCode,

                sortByStar = starRankLD.value?.sort ?: PlaymateListStarSort.DEFAULT.sort,
                sortByPrice = priceRankLD.value?.sort ?: PlaymateListPriceSort.DEFAULT.sort,

                page = PageReq(size = pageHandler.pageSize, next = pageHandler.currentPage)
            )
        )
        when (rlt) {
            is Rlt.Failed -> {
                listRltLD.send(rlt)
            }

            is Rlt.Success -> {
                pageHandler.nextPage(rlt.data.data?.next)
                playmateList.addAll(rlt.data.data?.list ?: emptyList())
                listRltLD.send(rlt)
                listLD.send(playmateList)
            }
        }
    }

}