package com.aiosman.riderpro.ui.post

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.aiosman.riderpro.data.AccountService
import com.aiosman.riderpro.data.AccountServiceImpl
import com.aiosman.riderpro.data.MomentService
import com.aiosman.riderpro.data.UserService
import com.aiosman.riderpro.data.UserServiceImpl
import com.aiosman.riderpro.entity.AccountProfileEntity
import com.aiosman.riderpro.entity.MomentEntity
import com.aiosman.riderpro.entity.MomentServiceImpl
import com.aiosman.riderpro.ui.index.tabs.moment.MomentViewModel
import kotlinx.coroutines.launch


class PostViewModel(
    val postId: String
) : ViewModel() {
    var service: MomentService = MomentServiceImpl()
    var userService: UserService = UserServiceImpl()


    var accountProfileEntity by mutableStateOf<AccountProfileEntity?>(null)
    var moment by mutableStateOf<MomentEntity?>(null)
    var accountService: AccountService = AccountServiceImpl()
    var commentsViewModel: CommentsViewModel = CommentsViewModel(postId)
    var isError by mutableStateOf(false)
    var isFirstLoad by mutableStateOf(true)

    fun reloadComment() {
        commentsViewModel.reloadComment()
    }

    suspend fun initData(highlightCommentId: Int? = null) {
        if (!isFirstLoad)  {
            return
        }
        isFirstLoad = false
        try {
            moment = service.getMomentById(postId.toInt())
        } catch (e: Exception) {
            isError = true
            return
        }
        highlightCommentId?.let {
            commentsViewModel.highlightComment(it)
        }
        commentsViewModel.reloadComment()

    }

    suspend fun likeComment(commentId: Int) {
        commentsViewModel.likeComment(commentId)
    }

    suspend fun unlikeComment(commentId: Int) {
        commentsViewModel.unlikeComment(commentId)
    }

    suspend fun createComment(
        content: String,
        parentCommentId: Int? = null,
        replyUserId: Int? = null,
        replyCommentId: Int? = null
    ) {
        commentsViewModel.createComment(
            content = content,
            parentCommentId = parentCommentId,
            replyUserId = replyUserId,
            replyCommentId = replyCommentId
        )
        moment = moment?.copy(commentCount = moment?.commentCount?.plus(1) ?: 0)
    }

    suspend fun likeMoment() {
        moment?.let {
            service.likeMoment(it.id)
            moment = moment?.copy(likeCount = moment?.likeCount?.plus(1) ?: 0, liked = true)
            MomentViewModel.updateLikeCount(it.id)
        }
    }

    suspend fun dislikeMoment() {
        moment?.let {
            service.dislikeMoment(it.id)
            moment = moment?.copy(likeCount = moment?.likeCount?.minus(1) ?: 0, liked = false)
            // update home list
            MomentViewModel.updateDislikeMomentById(it.id)
        }
    }

    suspend fun favoriteMoment() {
        moment?.let {
            service.favoriteMoment(it.id)
            moment =
                moment?.copy(favoriteCount = moment?.favoriteCount?.plus(1) ?: 0, isFavorite = true)
        }
    }

    suspend fun unfavoriteMoment() {
        moment?.let {
            service.unfavoriteMoment(it.id)
            moment = moment?.copy(
                favoriteCount = moment?.favoriteCount?.minus(1) ?: 0, isFavorite = false
            )
        }
    }

    suspend fun followUser() {
        moment?.let {
            userService.followUser(it.authorId.toString())
            moment = moment?.copy(followStatus = true)
            // 更新我的关注页面的关注数
        }
    }

    suspend fun unfollowUser() {
        moment?.let {
            userService.unFollowUser(it.authorId.toString())
            moment = moment?.copy(followStatus = false)
            // 更新我的关注页面的关注数
        }
    }

    fun deleteComment(commentId: Int) {
        commentsViewModel.deleteComment(commentId)
        moment = moment?.copy(commentCount = moment?.commentCount?.minus(1) ?: 0)
        moment?.let {
            MomentViewModel.updateMomentCommentCount(it.id, -1)
        }
    }

    var avatar: String? = null
        get() {
            accountProfileEntity?.avatar?.let {
                return it
            }
            moment?.avatar?.let {
                return it
            }
            return field
        }
    var nickname: String? = null
        get() {
            accountProfileEntity?.nickName?.let {
                return it
            }
            moment?.nickname?.let {
                return it
            }
            return field
        }

    fun deleteMoment(callback: () -> Unit) {
        viewModelScope.launch {
            moment?.let {
                service.deleteMoment(it.id)
                MomentViewModel.deleteMoment(it.id)
            }
            callback()
        }
    }

    fun loadMoreSubComments(commentId: Int) {
        commentsViewModel.loadMoreSubComments(commentId)
    }

}