package com.aiosman.ravenow.entity

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.aiosman.ravenow.data.AccountFollow
import com.aiosman.ravenow.data.AccountService
import com.aiosman.ravenow.data.Image
import com.aiosman.ravenow.data.api.ApiClient
import java.io.IOException
import java.util.Date

/**
 * 用户点赞
 */
data class AccountLikeEntity(
    // 动态
    val post: NoticePostEntity?,
    // 回复评论
    val comment: NoticeCommentEntity?,
    // 点赞用户
    val user: NoticeUserEntity,
    // 点赞时间
    val likeTime: Date,
    // 动态ID
    val postId: Int
)

/**
 * 用户收藏
 */
data class AccountFavouriteEntity(
    // 动态
    val post: NoticePostEntity,
    // 收藏用户
    val user: NoticeUserEntity,
    // 收藏时间
    val favoriteTime: Date,
)

/**
 * 用户信息
 */
data class AccountProfileEntity(
    // 用户ID
    val id: Int,
    // 粉丝数
    val followerCount: Int,
    // 关注数
    val followingCount: Int,
    // 昵称
    val nickName: String,
    // 头像
    val avatar: String,
    // 个人简介
    val bio: String,
    // 国家
    val country: String,
    // 是否关注,针对当前登录用户
    val isFollowing: Boolean,
    // 主页背景图
    val banner: String?,
    // trtcUserId
    val trtcUserId: String,
    val chatToken: String?,

    val aiAccount: Boolean,
    val rawAvatar: String,

    val chatAIId: String,
)

/**
 * 消息关联的动态
 */
data class NoticePostEntity(
    // 动态ID
    val id: Int,
    // 动态内容
    val textContent: String,
    // 动态图片
    val images: List<Image>,
    // 时间
    val time: Date,
)

data class NoticeCommentEntity(
    // 评论ID
    val id: Int,
    // 评论内容
    val content: String,
    // 评论时间
    val time: Date,
    // 引用评论
    val replyComment: NoticeCommentEntity?,
    // 动态
    val postId: Int,
    // 动态
    val post : NoticePostEntity?,
)

/**
 * 消息关联的用户
 */
data class NoticeUserEntity(
    // 用户ID
    val id: Int,
    // 昵称
    val nickName: String,
    // 头像
    val avatar: String,
)

/**
 * 用户点赞消息分页数据加载器
 */
class LikeItemPagingSource(
    private val accountService: AccountService,
) : PagingSource<Int, AccountLikeEntity>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AccountLikeEntity> {
        return try {
            val currentPage = params.key ?: 1
            val likes = accountService.getMyLikeNotice(
                page = currentPage,
                pageSize = 20,
            )

            LoadResult.Page(
                data = likes.list.map {
                    it.toAccountLikeEntity()
                },
                prevKey = if (currentPage == 1) null else currentPage - 1,
                nextKey = if (likes.list.isEmpty()) null else likes.page + 1
            )
        } catch (exception: IOException) {
            return LoadResult.Error(exception)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, AccountLikeEntity>): Int? {
        return state.anchorPosition
    }
}

/**
 * 用户收藏消息分页数据加载器
 */
class FavoriteItemPagingSource(
    private val accountService: AccountService,
) : PagingSource<Int, AccountFavouriteEntity>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AccountFavouriteEntity> {
        return try {
            val currentPage = params.key ?: 1
            val favouriteListContainer = accountService.getMyFavouriteNotice(
                page = currentPage,
                pageSize = 20,
            )
            LoadResult.Page(
                data = favouriteListContainer.list.map {
                    it.toAccountFavouriteEntity()
                },
                prevKey = if (currentPage == 1) null else currentPage - 1,
                nextKey = if (favouriteListContainer.list.isEmpty()) null else favouriteListContainer.page + 1
            )
        } catch (exception: IOException) {
            return LoadResult.Error(exception)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, AccountFavouriteEntity>): Int? {
        return state.anchorPosition
    }
}

/**
 * 用户关注消息分页数据加载器
 */
class FollowItemPagingSource(
    private val accountService: AccountService,
) : PagingSource<Int, AccountFollow>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AccountFollow> {
        return try {
            val currentPage = params.key ?: 1
            val followListContainer = accountService.getMyFollowNotice(
                page = currentPage,
                pageSize = 20,
            )

            LoadResult.Page(
                data = followListContainer.list.map {
                    it.copy(
                        avatar = "${ApiClient.BASE_SERVER}${it.avatar}",
                    )
                },
                prevKey = if (currentPage == 1) null else currentPage - 1,
                nextKey = if (followListContainer.list.isEmpty()) null else followListContainer.page + 1
            )
        } catch (exception: IOException) {
            return LoadResult.Error(exception)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, AccountFollow>): Int? {
        return state.anchorPosition
    }
}