package com.adealink.weparty.playmate.detail.viewmodel

import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.CommonDataNullError
import com.adealink.frame.base.Rlt
import com.adealink.frame.log.Log
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.network.data.PageReq
import com.adealink.weparty.App
import com.adealink.weparty.module.playmate.data.TAG_PLAYMATE_DETAIL
import com.adealink.weparty.playmate.comment.data.PlaymateCommentData
import com.adealink.weparty.playmate.comment.data.PlaymateCommentReq
import com.adealink.weparty.playmate.data.PlaymateDetailReq
import com.adealink.weparty.playmate.data.PlaymateDetailRes
import com.adealink.weparty.playmate.datasource.remote.PlaymateHttpService
import com.adealink.weparty.playmate.detail.data.BasePlaymateDetailItem
import com.adealink.weparty.playmate.detail.data.PlaymateDetailCommentItemData
import com.adealink.weparty.playmate.detail.data.PlaymateDetailCommentTitleItemData
import com.adealink.weparty.playmate.detail.data.PlaymateDetailItemData
import kotlinx.coroutines.launch

class PlaymateDetailViewModel : BaseViewModel() {

    private var playmateID: String? = null
    private var playmateName: String? = null

    private var detailRes: PlaymateDetailRes? = null
    private var commonList: List<PlaymateCommentData>? = null

    fun setPlaymate(id: String?, name: String?) {
        this.playmateID = id
        this.playmateName = name
    }

    val detailRltLD = MutableLiveData<Rlt<Any>>()
    val detailLD = MutableLiveData<PlaymateDetailRes?>()

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

    fun loadPlaymateDetail(id: String?) {
        if (id.isNullOrEmpty()) {
            detailRltLD.send(Rlt.Failed(CommonDataNullError()))
            detailLD.send(null)
            return
        }
        viewModelScope.launch {
            val rlt = playmateHttpService.getPlaymateDetail(PlaymateDetailReq(id))
            when (rlt) {
                is Rlt.Failed -> {
                    detailRltLD.send(rlt)
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    detailRes = data
                    if (data == null) {
                        detailRltLD.send(Rlt.Failed(CommonDataNullError()))
                    } else {
                        detailLD.send(data)
                        detailRltLD.send(Rlt.Success(data))
                    }
                    notifyDetailItemListChanged()
                    loadPlaymateComment()
                }
            }
        }
    }

    private suspend fun loadPlaymateComment() {
        val playmateId = playmateID
        if (playmateId.isNullOrEmpty()) {
            commonList = null
            notifyDetailItemListChanged()
            return
        }
        val rlt = playmateHttpService.getPlaymateComment(
            PlaymateCommentReq(
                playmateId = playmateId,
                page = PageReq(size = 3, next = null)
            )
        )
        when (rlt) {
            is Rlt.Failed -> {
                Log.e(TAG_PLAYMATE_DETAIL, "loadPlaymateComment fail, for $rlt")
                commonList = null
            }

            is Rlt.Success -> {
                commonList = rlt.data.data?.list
                notifyDetailItemListChanged()
            }
        }
    }

    var detailItemList = listOf<BasePlaymateDetailItem>()
    val detailItemListLD = MutableLiveData<List<BasePlaymateDetailItem>>()

    private fun notifyDetailItemListChanged() {
        val data: PlaymateDetailRes? = detailRes
        if (data == null) {
            detailItemList = emptyList()
            detailItemListLD.send(detailItemList)
            return
        }
        val itemList = mutableListOf<BasePlaymateDetailItem>()
        //详情内容
        itemList.add(PlaymateDetailItemData(data))

        //评价标题
        itemList.add(
            PlaymateDetailCommentTitleItemData(
                data.star,
                data.commentCount,
            )
        )
        //评价标签
//        itemList.add(
//            PlaymateDetailCommentLabelItemData(
//                listOf(
//                    PlaymateCommentLabel("牛逼"),
//                    PlaymateCommentLabel("123123123"),
//                    PlaymateCommentLabel("123123123"),
//                    PlaymateCommentLabel("123123123"),
//                    PlaymateCommentLabel("123123123"),
//                    PlaymateCommentLabel("123123123"),
//                    PlaymateCommentLabel("123123123"),
//                )
//            )
//        )
        //评价详情
        itemList.addAll(
            commonList?.map {
                PlaymateDetailCommentItemData(it)
            } ?: emptyList()
        )
        detailItemList = itemList
        detailItemListLD.send(itemList)
    }

}