package com.adealink.weparty.profile.dynamic.viewmodel

import androidx.lifecycle.LiveData
import com.adealink.frame.base.CommonDataNullError
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.App
import com.adealink.weparty.commonui.ext.isSuccess
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.profile.datasource.remote.DynamicHttpService
import com.adealink.weparty.profile.dynamic.data.DynamicCommentData
import com.adealink.weparty.profile.dynamic.data.DynamicCommentReq
import com.adealink.weparty.profile.dynamic.data.DynamicDeleteReq
import com.adealink.weparty.profile.dynamic.data.DynamicDetailData
import com.adealink.weparty.profile.dynamic.data.DynamicDetailReq
import com.adealink.weparty.profile.dynamic.data.DynamicLikeReq
import com.adealink.weparty.profile.dynamic.data.LikeDynamicEvent
import com.adealink.weparty.profile.dynamic.data.PublishNewCommentEvent
import com.adealink.weparty.profile.dynamic.data.deleteDynamicLD
import com.adealink.weparty.profile.dynamic.data.likeDynamicLD
import com.adealink.weparty.profile.dynamic.data.publishNewCommentLD
import kotlinx.coroutines.launch
import kotlin.math.max

class DynamicViewModel : BaseViewModel() {

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

    private var dynamicId: String? = null
    val dynamicDetailLD = ExtMutableLiveData<DynamicDetailData>()

    fun loadDynamic(id: String): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            this@DynamicViewModel.dynamicId = id
            val rlt = dynamicHttpService.dynamicDetail(DynamicDetailReq(id))
            when (rlt) {
                is Rlt.Failed -> {
                    //Ntd.
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    if (data == null) {
                        liveData.send(Rlt.Failed(CommonDataNullError()))
                    } else {
                        dynamicDetailLD.send(data)
                    }
                }
            }
            liveData.send(rlt)

        }
        return liveData
    }

    fun setDynamic(detail: DynamicDetailData) {
        viewModelScope.launch {
            dynamicDetailLD.send(detail)
        }
    }

    fun reloadDynamic() {
        val id = dynamicId ?: return
        loadDynamic(id)
    }

    fun like(date: DynamicDetailData): LiveData<Rlt<Any>> {
        val livedata = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = dynamicHttpService.dynamicLike(DynamicLikeReq(date.id))
            if (rlt.isSuccess) {
                val nextLike = !date.liked
                if (nextLike) {
                    date.liked = true
                    date.likeCount++
                } else {
                    date.liked = false
                    date.likeCount = max(date.likeCount - 1, 0)
                }
                likeDynamicLD.send(
                    LikeDynamicEvent(
                        date.id, date.liked, date.likeCount
                    )
                )
            }
            livedata.send(rlt)
        }
        return livedata
    }

    fun like(dynamicId: String): LiveData<Rlt<Any>> {
        val livedata = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = dynamicHttpService.dynamicLike(DynamicLikeReq(dynamicId))
            if (rlt.isSuccess) {
                dynamicDetailLD.value?.let { detailData ->
                    if (detailData.id == dynamicId) {
                        val nextLike = !detailData.liked
                        if (nextLike) {
                            detailData.liked = true
                            detailData.likeCount++
                        } else {
                            detailData.liked = false
                            detailData.likeCount = max(detailData.likeCount - 1, 0)
                        }
                        likeDynamicLD.send(
                            LikeDynamicEvent(
                                dynamicId, detailData.liked, detailData.likeCount
                            )
                        )
                        setDynamic(detailData)
                    }
                }
            }
            livedata.send(rlt)
        }
        return livedata
    }

    fun comment(dynamicId: String, comment: String): LiveData<Rlt<Any>> {
        val livedata = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = dynamicHttpService.dynamicComment(
                DynamicCommentReq(
                    dynamicId, comment
                )
            )
            if (rlt.isSuccess) {
                val nowTs = System.currentTimeMillis()
                val myUserInfo = ProfileModule.getMyUserInfo()
                publishNewCommentLD.send(
                    PublishNewCommentEvent(
                        dynamicId,
                        DynamicCommentData(
                            nowTs.toString(),
                            nowTs,
                            nowTs,
                            myUserInfo?.uid ?: "",
                            myUserInfo?.avatar ?: "",
                            myUserInfo?.nickName ?: "",
                            comment,
                            0
                        )
                    )
                )
            }
            livedata.send(rlt)
        }
        return livedata
    }

    fun delete(dynamicId: String): LiveData<Rlt<Any>> {
        val livedata = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = dynamicHttpService.delete(
                DynamicDeleteReq(dynamicId)
            )
            if (rlt.isSuccess) {
                deleteDynamicLD.send(dynamicId)
            }
            livedata.send(rlt)
        }
        return livedata
    }

    fun onNewComment() {
        viewModelScope.launch {
            val data = dynamicDetailLD.value ?: return@launch
            data.commentCount++
            setDynamic(data)
        }
    }

}