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.PlaymateDetailData
import com.adealink.weparty.module.playmate.data.TAG_PLAYMATE_DETAIL
import com.adealink.weparty.module.playmate.viewmodel.IPlaymateDetailViewModel
import com.adealink.weparty.module.widget.form.WidgetType
import com.adealink.weparty.module.widget.form.data.StringStepField
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.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 com.adealink.weparty.playmate.detail.data.PlaymateExtraFieldItemData
import kotlinx.coroutines.launch

class PlaymateDetailViewModel : BaseViewModel(), IPlaymateDetailViewModel {

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

    private var detailRes: PlaymateDetailData? = null
    private var commentList: List<PlaymateCommentData>? = null
    private var totalComment: Long = 0

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

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

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

    fun loadPlaymateDetail(id: String?) {
        viewModelScope.launch {
            if (id.isNullOrEmpty()) {
                detailRltLD.send(Rlt.Failed(CommonDataNullError()))
                detailLD.send(null)
                return@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()) {
            commentList = null
            totalComment = 0
            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")
                commentList = null
                totalComment = 0
            }

            is Rlt.Success -> {
                commentList = rlt.data.data?.list
                totalComment = rlt.data.data?.total ?: 0
                notifyDetailItemListChanged()
            }
        }
    }

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

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

        //详情扩展内容
        //只展示单选,多选内容
        val extendFields = data.extendFields?.filter {
            it is StringStepField &&
                    (it.fieldType == WidgetType.SINGLE_CHOICE.type || it.fieldType == WidgetType.MULTIPLE_CHOICE.type)
        }
        if (!extendFields.isNullOrEmpty()) {
            itemList.addAll(
                extendFields.map {
                    PlaymateExtraFieldItemData(it)
                }
            )
        }

        //评价
        val commentList = commentList ?: emptyList()
        val commentItems = mutableListOf<PlaymateDetailCommentItemData>()
        for ((index, data) in commentList.withIndex()) {
            commentItems.add(
                PlaymateDetailCommentItemData(data, index < commentList.size - 1)
            )
        }
        if (commentItems.isNotEmpty()) {
            itemList.add(
                PlaymateDetailCommentTitleItemData(
                    data.star,
                    totalComment,
                )
            )
            //评价详情
            itemList.addAll(commentItems)
        }

        detailItemList = itemList
        detailItemListLD.send(itemList)
    }

}