package com.gh.gamecenter.entity

import android.os.Parcel
import android.os.Parcelable
import com.gh.gamecenter.common.annotation.SyncPage
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.common.syncpage.SyncFieldConstants
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.qa.entity.AnswerEntity
import com.gh.gamecenter.qa.entity.CommunityVideoEntity
import com.gh.gamecenter.qa.entity.ImageInfo
import com.gh.gamecenter.qa.entity.Questions
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parceler
import kotlinx.parcelize.Parcelize

@Parcelize
data class PersonalHistoryEntity(
    @SerializedName("_id")
    val id: String = "",
    val type: String = "",
    var question: Question = Question(),
    var brief: String = "",
    var des: String = "",
    var url: String = "",
    var status: String? = "",
    @SerializedName("video_info")
    var videoInfo: VideoInfo = VideoInfo(),
    var poster: String = "",
    var length: Long = 0,
    var count: Count = Count(),
    val time: Long = 0,
    var title: String = "",
    var description: String = "",
    val community: CommunityEntity = CommunityEntity(),
    var videos: List<CommunityVideoEntity> = ArrayList(),
    var user: PersonalEntity? = null,
    @SerializedName("fold_users")
    var foldUsers: List<UserEntity>? = null,
    val images: List<String> = ArrayList(),
    @SerializedName("images_info")
    var imagesInfo: List<ImageInfo> = ArrayList(),
    val me: MeEntity = MeEntity(),
    var comment: Comment = Comment(),
    var commentable: Boolean = true,
    @SerializedName("is_edit")
    var isEdit: Boolean = false
) : Parcelable {

    fun getPassVideos(): List<CommunityVideoEntity> {
        val passVideos = arrayListOf<CommunityVideoEntity>()
        for (video in videos) {
            if (video.status == "pass") passVideos.add(video)
        }
        return passVideos
    }


    @Parcelize
    data class Question(
        @SerializedName("_id")
        val id: String = "",
        var title: String = ""
    ) : Parcelable

    @Parcelize
    data class Count(
        @SyncPage(syncNames = [SyncFieldConstants.ANSWER_COMMENT_COUNT, SyncFieldConstants.ARTICLE_COMMENT_COUNT])
        var comment: Int = 0,
        @SyncPage(syncNames = [SyncFieldConstants.ANSWER_VOTE_COUNT, SyncFieldConstants.ARTICLE_VOTE_COUNT])
        var vote: Int = 0,
        var reply: Int = 0
    ) : Parcelable {

        @SyncPage(syncNames = [SyncFieldConstants.ANSWER_COUNT])
        var answer: Int = 0
            get() = field + reply

        fun transformForumCount(type: String): com.gh.gamecenter.qa.entity.Count {
            val count = com.gh.gamecenter.qa.entity.Count()
            count.comment = comment
            count.vote = vote
            count.answer = answer
            count.reply = if (type == "answer") comment else reply
            return count
        }

        companion object : Parceler<Count> {
            override fun Count.write(parcel: Parcel, flags: Int) {
                parcel.writeInt(comment)
                parcel.writeInt(vote)
                parcel.writeInt(reply)
                parcel.writeInt(answer)
            }

            override fun create(parcel: Parcel): Count {
                return Count().apply {
                    comment = parcel.readInt()
                    vote = parcel.readInt()
                    reply = parcel.readInt()
                    answer = parcel.readInt() - reply
                }
            }
        }
    }

    @Parcelize
    data class Comment(
        @SerializedName("_id")
        val id: String = "",
        var star: Int = 0,
        var content: String = "",
        val game: Game = Game()
    ) : Parcelable

    @Parcelize
    data class Game(
        @SerializedName("_id")
        val id: String = "",
        @SerializedName("name")
        private var mName: String = "",
        @SerializedName("name_suffix")
        val nameSuffix: String = "",

        val icon: String = "",
        @SerializedName(value = "new_tag_style")
        val tag: ArrayList<TagStyleEntity> = ArrayList(),
        val star: Float = 0F,
        val active: Boolean = true, //游戏是否被隐藏 false被隐藏

        @SerializedName("ori_icon")
        var rawIcon: String? = null,
        @SerializedName("icon_subscript")
        var iconSubscript: String? = null,

        @SerializedName("played_time")
        val playedTime: Long = 0
    ) : Parcelable {
        @IgnoredOnParcel
        var name: String
            get() = mName.removeSuffix(".")
            set(value) {
                mName = value
            }

        fun getRawIconIfExisted(): String {
            return rawIcon ?: icon
        }
    }


    // 因为内容是文章和回答混合的,所以返回的有可能是文章的实体
    fun transformAnswerEntity(): AnswerEntity {
        val question = if (type == "answer") {
            Questions(id = question.id, title = question.title)
        } else {
            Questions(title = title)
        }
        val answer = AnswerEntity()
        answer.id = id
        answer.questions = question
        answer.brief = brief
        answer.images = images
        answer.videos = videos
        answer.time = time
        answer.count = count.transformForumCount(type)
        answer.me = me
        answer.commentable = commentable
        answer.articleCommunityId = community.id
        answer.communityId = community.id
        answer.communityName = community.name
        answer.type = type
        answer.status = status ?: "pending"
        return answer
    }

    fun transformQuestionEntity(): Questions {
        val question = Questions(title = title)
        question.answerCount = count.answer
        question.me = me
        question.id = id
        question.images = images as ArrayList<String>
        question.communityName = community.name
        return question
    }

    fun transformForumVideoEntity(): ForumVideoEntity {
        val forumVideoEntity = ForumVideoEntity()
        if (type == "video") {
            forumVideoEntity.id = id
            forumVideoEntity.title = title
            forumVideoEntity.des = des
            forumVideoEntity.url = url
            forumVideoEntity.status = status ?: "pending"
            forumVideoEntity.poster = poster
            forumVideoEntity.length = length
            forumVideoEntity.duration = TimeUtils.formatVideoDuration(length)
            forumVideoEntity.videoInfo = videoInfo
            forumVideoEntity.videoIsMuted = SPUtils.getBoolean(Constants.SP_VIDEO_PLAY_MUTE, true)
        } else {
            forumVideoEntity.title = title
            forumVideoEntity.des = brief
            forumVideoEntity.videoIsMuted = SPUtils.getBoolean(Constants.SP_VIDEO_PLAY_MUTE, true)
            val newVideos = if (user?.id == UserManager.getInstance().userId) videos else getPassVideos()
            if (newVideos.isNotEmpty()) {
                forumVideoEntity.id = newVideos[0].id
                forumVideoEntity.url = newVideos[0].url
                forumVideoEntity.poster = newVideos[0].poster
                forumVideoEntity.status = status ?: "pending"
                forumVideoEntity.duration = newVideos[0].duration
                forumVideoEntity.videoInfo = VideoInfo(width = newVideos[0].width, height = newVideos[0].height)
            }
        }
        return forumVideoEntity
    }
}