package com.adealink.weparty.profile.dynamic.data

import android.os.Parcelable
import com.adealink.frame.network.data.PageReq
import com.adealink.weparty.commonui.recycleview.diffutil.BaseListItemData
import com.adealink.weparty.module.image.data.PhotoData
import com.google.gson.annotations.GsonNullable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize


@Parcelize
data class DynamicDetailData(
    @SerializedName("id") val id: String,
    @SerializedName("userNo") val uid: String,
    @SerializedName("nickname") val nickname: String,
    @SerializedName("avatar") val avatar: String,
    @SerializedName("createdAt") val createdAt: Long, //创建时间
    @SerializedName("updatedAt") val updatedAt: Long, //更新时间
    @SerializedName("medias") val medias: List<DynamicMediaData>,
    @GsonNullable
    @SerializedName("textContent") val textContent: String?,

    @SerializedName("liked") var liked: Boolean, //我是否点赞
    @SerializedName("likeCount") var likeCount: Int,

    @SerializedName("commentCount") var commentCount: Int, //评论数量
) : Parcelable {
    fun isVideo(): Boolean {
        return medias.find {
            it.type == 1
        } != null
    }

    fun getVideoMedia(): DynamicMediaData? {
        return medias.find {
            it.type == 1
        }
    }

    fun isVideoMediaAvailable(): Boolean {
        val media = getVideoMedia()
        return media != null && !media.isNull()
    }

    fun getPhotoMedia(): List<DynamicMediaData>? {
        return medias.filter { it.type == 0 }
    }
}

enum class MediaType(val type: Int) {
    PHOTO(0),
    VIDEO(1)
}

@Parcelize
data class DynamicMediaData(
    @SerializedName("url") val url: String,
    @SerializedName("type") val type: Int, //0:图片, 1:视频
    @GsonNullable
    @SerializedName("videoCover") val videoCover: String?,
) : Parcelable {

    fun isNull(): Boolean {
        return url.isNullOrEmpty()
    }

    @IgnoredOnParcel
    @GsonNullable
    private var photoData: PhotoData? = null
    fun getPhotoData(): PhotoData {
        var data = photoData
        if (data == null) {
            data = PhotoData(url, null, null)
            photoData = data
        }
        return data
    }

    @IgnoredOnParcel
    @GsonNullable
    private var videoCoverData: PhotoData? = null
    fun getVideoCoverData(): PhotoData {
        var data = videoCoverData
        if (data == null) {
            data = PhotoData(videoCover, null, null)
            videoCoverData = data
        }
        return data
    }

}

data class DynamicListReq(
    @SerializedName("userNo") val uid: String,
    @SerializedName("page") val page: PageReq,
)

data class DynamicListRes(
    @SerializedName("list") val list: List<DynamicDetailData>,
    @GsonNullable
    @SerializedName("next") val next: String?,
)


data class DynamicItemData(
    val data: DynamicDetailData
) : BaseListItemData {
    override fun areContentsTheSame(newItem: Any): Boolean {
        val new = newItem as? DynamicItemData ?: return false
        return data.id == new.data.id
                && data.createdAt == new.data.createdAt
                && data.updatedAt == new.data.updatedAt
                && data.likeCount == new.data.likeCount
                && data.medias.size == new.data.medias.size
    }
}


data class DynamicDetailReq(
    @SerializedName("id") val id: String,
)

data class DynamicCommentListReq(
    @SerializedName("trendId") val id: String,
    @SerializedName("page") val page: PageReq,
)

data class DynamicCommentListRes(
    @SerializedName("list") val list: List<DynamicCommentData>,
    @GsonNullable
    @SerializedName("next") val next: String?,
    @SerializedName("total") val total: Long,
)

data class DynamicCommentData(
    @SerializedName("id") val id: String, //评论ID
//    @SerializedName("commentPid") val pid: String, //父级评论ID
//    @SerializedName("trendId") val trendId: Long, //动态ID
    @SerializedName("createdAt") val createdAt: Long,
    @SerializedName("updatedAt") val updatedAt: Long,

    @SerializedName("userNo") val uid: String,
    @SerializedName("avatar") val avatar: String,
    @SerializedName("nickname") val nickname: String,

    @SerializedName("textContent") val comment: String,
    @SerializedName("likeCount") val likeCount: Int,
)


data class PublishDynamicReq(
    //动态文案
    @SerializedName("content") val content: String?,
    //动态媒体
    @SerializedName("medias") val medias: List<DynamicMediaData>?,
)

data class DynamicLikeReq(
    @SerializedName("id") val id: String
)

data class DynamicCommentReq(
    @SerializedName("trendId") val id: String,
    @SerializedName("content") val content: String,
)


data class DynamicDeleteReq(
    @SerializedName("id") val id: String
)