package com.aiosman.riderpro.data

import com.aiosman.riderpro.R
import com.aiosman.riderpro.data.api.ApiClient
import com.aiosman.riderpro.entity.MomentEntity
import com.aiosman.riderpro.entity.MomentImageEntity
import com.google.gson.annotations.SerializedName
import java.io.File

data class Moment(
    @SerializedName("id")
    val id: Long,
    @SerializedName("textContent")
    val textContent: String,
    @SerializedName("images")
    val images: List<Image>,
    @SerializedName("user")
    val user: User,
    @SerializedName("likeCount")
    val likeCount: Long,
    @SerializedName("isLiked")
    val isLiked: Boolean,
    @SerializedName("favoriteCount")
    val favoriteCount: Long,
    @SerializedName("isFavorite")
    val isFavorite: Boolean,
    @SerializedName("shareCount")
    val isCommented: Boolean,
    @SerializedName("commentCount")
    val commentCount: Long,
    @SerializedName("time")
    val time: String,
    @SerializedName("isFollowed")
    val isFollowed: Boolean,
) {
    fun toMomentItem(): MomentEntity {
        return MomentEntity(
            id = id.toInt(),
            avatar = "${ApiClient.BASE_SERVER}${user.avatar}",
            nickname = user.nickName,
            location = "Worldwide",
            time = ApiClient.dateFromApiString(time),
            followStatus = isFollowed,
            momentTextContent = textContent,
            momentPicture = R.drawable.default_moment_img,
            likeCount = likeCount.toInt(),
            commentCount = commentCount.toInt(),
            shareCount = 0,
            favoriteCount = favoriteCount.toInt(),
            images = images.map {
                MomentImageEntity(
                    url = "${ApiClient.BASE_SERVER}${it.url}",
                    thumbnail = "${ApiClient.BASE_SERVER}${it.thumbnail}",
                    id = it.id,
                    blurHash = it.blurHash
                )
            },
            authorId = user.id.toInt(),
            liked = isLiked,
            isFavorite = isFavorite
        )
    }
}

data class Image(
    @SerializedName("id")
    val id: Long,
    @SerializedName("url")
    val url: String,
    @SerializedName("thumbnail")
    val thumbnail: String,
    @SerializedName("blurHash")
    val blurHash: String?
)

data class User(
    @SerializedName("id")
    val id: Long,
    @SerializedName("nickName")
    val nickName: String,
    @SerializedName("avatar")
    val avatar: String
)

data class UploadImage(
    val file: File,
    val filename: String,
    val url: String,
    val ext: String
)

interface MomentService {
    /**
     * 获取动态详情
     * @param id 动态ID
     */
    suspend fun getMomentById(id: Int): MomentEntity

    /**
     * 点赞动态
     * @param id 动态ID
     */
    suspend fun likeMoment(id: Int)

    /**
     * 取消点赞动态
     * @param id 动态ID
     */
    suspend fun dislikeMoment(id: Int)

    /**
     * 获取动态列表
     * @param pageNumber 页码
     * @param author 作者ID,过滤条件
     * @param timelineId 用户时间线ID,指定用户 ID 的时间线
     * @param contentSearch 内容搜索,过滤条件
     * @param trend 是否趋势动态
     * @return 动态列表
     */
    suspend fun getMoments(
        pageNumber: Int,
        author: Int? = null,
        timelineId: Int? = null,
        contentSearch: String? = null,
        trend: Boolean? = false,
        favoriteUserId: Int? = null
    ): ListContainer<MomentEntity>

    /**
     * 创建动态
     * @param content 动态内容
     * @param authorId 作者ID
     * @param images 图片列表
     * @param relPostId 关联动态ID
     */
    suspend fun createMoment(
        content: String,
        authorId: Int,
        images: List<UploadImage>,
        relPostId: Int? = null
    ): MomentEntity

    /**
     * 收藏动态
     * @param id 动态ID
     */
    suspend fun favoriteMoment(id: Int)

    /**
     *  取消收藏动态
     *  @param id 动态ID
     */
    suspend fun unfavoriteMoment(id: Int)

    /**
     * 删除动态
     */
    suspend fun deleteMoment(id: Int)
}


