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.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.App
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.playmate.data.PlaymateCommentData
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.PlaymateCommentLabel
import com.adealink.weparty.playmate.detail.data.PlaymateDetailCommentItemData
import com.adealink.weparty.playmate.detail.data.PlaymateDetailCommentLabelItemData
import com.adealink.weparty.playmate.detail.data.PlaymateDetailCommentTitleItemData
import com.adealink.weparty.playmate.detail.data.PlaymateDetailItemData
import kotlinx.coroutines.launch
import kotlin.random.Random

class PlaymateDetailViewModel : BaseViewModel() {

    private var playmateID: String? = null
    private var playmateName: String? = 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
                    if (data == null) {
                        detailRltLD.send(Rlt.Failed(CommonDataNullError()))
                    } else {
                        detailLD.send(data)
                        detailRltLD.send(Rlt.Success(data))
                    }
                    wrapDetailItemList(data)
                }
            }
        }
    }

    fun getPlaymateDetail(): PlaymateDetailRes? {
        return detailLD.value
    }


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

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

        //评价标题
        itemList.add(
            PlaymateDetailCommentTitleItemData(
                data.star,
                Random.nextInt(999),
            )
        )
        //评价标签
        itemList.add(
            PlaymateDetailCommentLabelItemData(
                listOf(
                    PlaymateCommentLabel("牛逼"),
                    PlaymateCommentLabel("123123123"),
                    PlaymateCommentLabel("123123123"),
                    PlaymateCommentLabel("123123123"),
                    PlaymateCommentLabel("123123123"),
                    PlaymateCommentLabel("123123123"),
                    PlaymateCommentLabel("123123123"),
                )
            )
        )
        //评价详情
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "短文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )

        // TODO: 测试代码
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )
        itemList.add(
            PlaymateDetailCommentItemData(
                PlaymateCommentData(
                    ProfileModule.getMyUserInfo() ?: UserInfo.emptyUserInfo("test", ""),
                    Random.nextFloat() * 5,
                    "长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案长文案",
                    Random.nextLong(0, System.currentTimeMillis())
                )
            )
        )

        detailItemList = itemList
        detailItemListLD.send(itemList)
    }

}