package com.adealink.weparty.profile.search.viewmodel

import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.App
import com.adealink.weparty.commonui.recycleview.diffutil.BaseListItemData
import com.adealink.weparty.profile.datasource.remote.SearchHttpService
import com.adealink.weparty.profile.search.data.SearchAllReq
import com.adealink.weparty.profile.search.data.SearchAllRoomResultItemData
import com.adealink.weparty.profile.search.data.SearchAllUserResultItemData
import com.adealink.weparty.profile.search.data.SearchRoomResultItemData
import com.adealink.weparty.profile.search.data.SearchUserResultItemData
import kotlinx.coroutines.launch

class SearchAllViewModel : BaseViewModel() {

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

    private var playmateResult = listOf<SearchUserResultItemData>()
    private var roomResult = listOf<SearchRoomResultItemData>()

    val searchResultLD = ExtMutableLiveData<List<BaseListItemData>>()
    val searchRltLD = ExtMutableLiveData<Rlt<Any>>()
    private var keyword: String? = null
    fun search(keyword: String?) {
        this.keyword = keyword
        viewModelScope.launch {
            if (keyword.isNullOrEmpty()) {
                searchRltLD.send(Rlt.Success(Any()))
                searchResultLD.send(emptyList())
                return@launch
            }
            val rlt = searchHttpService.searchAll(SearchAllReq(keyword))
            when (rlt) {
                is Rlt.Failed -> {
                    if (keyword != this@SearchAllViewModel.keyword) {
                        return@launch
                    }
                    searchRltLD.send(rlt)
                }

                is Rlt.Success -> {
                    if (keyword != this@SearchAllViewModel.keyword) {
                        return@launch
                    }

                    playmateResult = rlt.data.data?.playmate?.map {
                        SearchUserResultItemData(it)
                    } ?: emptyList()
                    roomResult = rlt.data.data?.rooms?.map {
                        SearchRoomResultItemData(it)
                    } ?: emptyList()
                    notifyResultListChanged()
                    searchRltLD.send(rlt)
                }
            }
        }
    }

    fun reSearch() {
        search(keyword)
    }

    fun notifyResultListChanged() {
        val itemList = mutableListOf<BaseListItemData>()
        itemList.add(SearchAllUserResultItemData(playmateResult))
        itemList.add(SearchAllRoomResultItemData(roomResult))
        searchResultLD.send(itemList)
    }


    fun notifyFollowChanged(uid: String, follow: Boolean) {
        viewModelScope.launch {
            playmateResult.find { it.data.uid == uid }?.data?.follow = follow
            notifyResultListChanged()
        }
    }
}