package com.adealink.weparty.profile.ui.about.viewmodel

import androidx.lifecycle.MutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.profile.dynamic.data.DynamicDetailData
import com.adealink.weparty.profile.dynamic.data.LikeDynamicEvent
import com.adealink.weparty.profile.ui.about.data.BaseProfileData
import com.adealink.weparty.profile.ui.about.data.DynamicItemData
import com.adealink.weparty.profile.ui.about.data.DynamicTitleItemData
import com.adealink.weparty.profile.ui.about.data.EmptyItemData
import com.adealink.weparty.profile.ui.about.data.InterestsItemData
import com.adealink.weparty.profile.ui.about.data.IntroduceItemData
import com.adealink.weparty.profile.ui.about.data.SkillsItemData
import kotlinx.coroutines.launch

class ProfilePageViewModel : BaseViewModel() {

    private var userInfo: UserInfo? = null
    private var dynamicList: List<DynamicDetailData>? = null

    private var itemList = listOf<BaseProfileData>()

    val profileItemLD = MutableLiveData<List<BaseProfileData>>()

    fun setUserInfo(userInfo: UserInfo?) {
        this.userInfo = userInfo
        notifyProfileItemChanged()
    }

    fun setDynamicList(dynamicList: List<DynamicDetailData>) {
        this.dynamicList = dynamicList
        notifyProfileItemChanged()
    }

    private fun notifyProfileItemChanged() {
        val itemList = mutableListOf<BaseProfileData>()

        //用户个人
        val userInfo = this@ProfilePageViewModel.userInfo
        //动态
        val dynamicList = dynamicList

        if (userInfo != null) {
            itemList.add(IntroduceItemData(userInfo))

            if (!userInfo.interests.isNullOrEmpty()) {
                itemList.add(InterestsItemData(userInfo.interests ?: emptyList()))
            }

            if (userInfo.isPlaymate() && !userInfo.skills.isNullOrEmpty()) {
                itemList.add(SkillsItemData(userInfo.skills ?: emptyList()))
            }
        }
        if (dynamicList.isNullOrEmpty()) {
            itemList.add(EmptyItemData)
        } else {
            itemList.add(DynamicTitleItemData)
            itemList.addAll(
                dynamicList.map {
                    DynamicItemData(it, userInfo)
                }
            )
        }

        this@ProfilePageViewModel.itemList = itemList
        profileItemLD.send(itemList)
    }

    fun onDynamicNewComment(
        dynamicId: String,
        notifyDataChanged: (position: Int) -> Unit
    ) {
        viewModelScope.launch {
            val itemList = itemList
            itemList.indexOfFirst { it is DynamicItemData && it.dynamic.id == dynamicId }
                .let { index ->
                    if (index >= 0) {
                        val itemData = itemList[index] as? DynamicItemData ?: return@launch
                        itemData.dynamic.commentCount++
                        notifyDataChanged.invoke(index)
                    }
                }
        }
    }

    fun onDynamicLiked(
        likeEvent: LikeDynamicEvent,
        notifyDataChanged: (position: Int) -> Unit
    ) {
        viewModelScope.launch {
            val itemList = itemList
            itemList.indexOfFirst { it is DynamicItemData && it.dynamic.id == likeEvent.dynamicId }
                .let { index ->
                    if (index >= 0) {
                        val itemData = itemList[index] as? DynamicItemData ?: return@launch
                        itemData.dynamic.liked = likeEvent.liked
                        itemData.dynamic.likeCount = likeEvent.likeCount
                        notifyDataChanged.invoke(index)
                    }
                }
        }
    }

}