package com.aiosman.ravenow.data

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

data class Moment(
    @SerializedName("id")
    val id: Long,
    @SerializedName("textContent")
    val textContent: String,
    @SerializedName("url")
    val url: String? = null,
    @SerializedName("images")
    val images: List<Image>? = null,
    @SerializedName("videos")
    val videos: List<Video>? = null,
    @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("isCommented")
    val isCommented: Boolean,
    @SerializedName("commentCount")
    val commentCount: Long,
    @SerializedName("time")
    val time: String?,
    @SerializedName("isFollowed")
    val isFollowed: Boolean,
    // 新闻相关字段
    @SerializedName("isNews")
    val isNews: Boolean = false,
    @SerializedName("newsTitle")
    val newsTitle: String? = null,
    @SerializedName("newsUrl")
    val newsUrl: String? = null,
    @SerializedName("newsSource")
    val newsSource: String? = null,
    @SerializedName("newsCategory")
    val newsCategory: String? = null,
    @SerializedName("newsLanguage")
    val newsLanguage: String? = null,
    @SerializedName("newsContent")
    val newsContent: String? = null,
    @SerializedName("hasFullText")
    val hasFullText: Boolean = false,
    @SerializedName("summary")
    val summary: String? = null,
    @SerializedName("publishedAt")
    val publishedAt: String? = null,
    @SerializedName("imageCached")
    val imageCached: Boolean = false
) {
    fun toMomentItem(): MomentEntity {
        return MomentEntity(
            id = id.toInt(),
            avatar = if (user.avatar != null && user.avatar.isNotEmpty()) {
                "${ApiClient.BASE_SERVER}${user.avatar}"
            } else {
                "" // 如果头像为空，使用空字符串
            },
            nickname = user.nickName ?: "未知用户", // 如果昵称为空，使用默认值
            location = "Worldwide",
            time = if (time != null && time.isNotEmpty()) {
                ApiClient.dateFromApiString(time)
            } else {
                java.util.Date() // 如果时间为空，使用当前时间作为默认值
            },
            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(
                    id = it.id,
                    url = "${ApiClient.BASE_SERVER}${it.url}",
                    originalUrl = it.originalUrl,
                    directUrl = it.directUrl,
                    thumbnail = "${ApiClient.BASE_SERVER}${it.thumbnail}",
                    thumbnailDirectUrl = it.thumbnailDirectUrl,
                    small = it.small,
                    smallDirectUrl = it.smallDirectUrl,
                    medium = it.medium,
                    mediumDirectUrl = it.mediumDirectUrl,
                    large = it.large,
                    largeDirectUrl = it.largeDirectUrl,
                    blurHash = it.blurHash,
                    width = it.width,
                    height = it.height
                )
            } ?: emptyList(),
            authorId = user.id.toInt(),
            liked = isLiked,
            isFavorite = isFavorite,
            url = url,
            videos = videos?.map {
                MomentVideoEntity(
                    id = it.id,
                    url = "${ApiClient.BASE_SERVER}${it.url}",
                    originalUrl = it.originalUrl,
                    directUrl = it.directUrl,
                    thumbnailUrl = it.thumbnailUrl?.let { thumb -> "${ApiClient.BASE_SERVER}$thumb" },
                    thumbnailDirectUrl = it.thumbnailDirectUrl,
                    duration = it.duration,
                    width = it.width,
                    height = it.height,
                    size = it.size,
                    format = it.format,
                    bitrate = it.bitrate,
                    frameRate = it.frameRate
                )
            },
            // 新闻相关字段
            isNews = isNews,
            newsTitle = newsTitle ?: "",
            newsUrl = newsUrl ?: "",
            newsSource = newsSource ?: "",
            newsCategory = newsCategory ?: "",
            newsLanguage = newsLanguage ?: "",
            newsContent = newsContent ?: "",
            hasFullText = hasFullText,
            summary = summary,
            publishedAt = publishedAt,
            imageCached = imageCached
        )
    }
}

data class Image(
    @SerializedName("id")
    val id: Long,
    @SerializedName("url")
    val url: String,
    @SerializedName("original_url")
    val originalUrl: String? = null,
    @SerializedName("directUrl")
    val directUrl: String? = null,
    @SerializedName("thumbnail")
    val thumbnail: String,
    @SerializedName("thumbnailDirectUrl")
    val thumbnailDirectUrl: String? = null,
    @SerializedName("small")
    val small: String? = null,
    @SerializedName("smallDirectUrl")
    val smallDirectUrl: String? = null,
    @SerializedName("medium")
    val medium: String? = null,
    @SerializedName("mediumDirectUrl")
    val mediumDirectUrl: String? = null,
    @SerializedName("large")
    val large: String? = null,
    @SerializedName("largeDirectUrl")
    val largeDirectUrl: String? = null,
    @SerializedName("blurHash")
    val blurHash: String?,
    @SerializedName("width")
    val width: Int?,
    @SerializedName("height")
    val height: Int?
)

data class Video(
    @SerializedName("id")
    val id: Long,
    @SerializedName("url")
    val url: String,
    @SerializedName("original_url")
    val originalUrl: String? = null,
    @SerializedName("directUrl")
    val directUrl: String? = null,
    @SerializedName("thumbnailUrl")
    val thumbnailUrl: String? = null,
    @SerializedName("thumbnailDirectUrl")
    val thumbnailDirectUrl: String? = null,
    @SerializedName("duration")
    val duration: Int? = null,
    @SerializedName("width")
    val width: Int? = null,
    @SerializedName("height")
    val height: Int? = null,
    @SerializedName("size")
    val size: Long? = null,
    @SerializedName("format")
    val format: String? = null,
    @SerializedName("bitrate")
    val bitrate: Int? = null,
    @SerializedName("frameRate")
    val frameRate: String? = null
)

data class User(
    @SerializedName("id")
    val id: Long,
    @SerializedName("nickName")
    val nickName: String?,
    @SerializedName("avatar")
    val avatar: String?,
    @SerializedName("avatarMedium")
    val avatarMedium: String? = null,
    @SerializedName("avatarLarge")
    val avatarLarge: String? = null,
    @SerializedName("originAvatar")
    val originAvatar: String? = null,
    @SerializedName("avatarDirectUrl")
    val avatarDirectUrl: String? = null,
    @SerializedName("avatarMediumDirectUrl")
    val avatarMediumDirectUrl: String? = null,
    @SerializedName("avatarLargeDirectUrl")
    val avatarLargeDirectUrl: String? = null,
    @SerializedName("aiAccount")
    val aiAccount: Boolean = false,
    @SerializedName("aiRoleAvatar")
    val aiRoleAvatar: String? = null,
    @SerializedName("aiRoleAvatarMedium")
    val aiRoleAvatarMedium: String? = null,
    @SerializedName("aiRoleAvatarLarge")
    val aiRoleAvatarLarge: String? = null,
    @SerializedName("aiRoleAvatarDirectUrl")
    val aiRoleAvatarDirectUrl: String? = null,
    @SerializedName("aiRoleAvatarMediumDirectUrl")
    val aiRoleAvatarMediumDirectUrl: String? = null,
    @SerializedName("aiRoleAvatarLargeDirectUrl")
    val aiRoleAvatarLargeDirectUrl: String? = null
)

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 是否趋势动态
     * @param explore 是否探索动态
     * @return 动态列表
     */
    suspend fun getMoments(
        pageNumber: Int,
        author: Int? = null,
        timelineId: Int? = null,
        contentSearch: String? = null,
        trend: Boolean? = false,
        explore: 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

    suspend fun agentMoment(
        content: String,
    ): String

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

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

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


