package com.gh.gamecenter.qa.entity

import android.os.Parcel
import android.os.Parcelable
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import com.gh.gamecenter.common.annotation.SyncIgnore
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.entity.*
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.room.converter.*
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parceler
import kotlinx.parcelize.Parcelize

@Entity
@Parcelize
data class ArticleEntity(
    @PrimaryKey
    @SerializedName("_id")
    var id: String = "",
    var title: String = "",
    var brief: String = "",
    var active: Boolean = true,
    var orderTag: Long = 0,
    @TypeConverters(ListStringConverter::class)
    var images: List<String> = ArrayList(),
    @TypeConverters(ImageInfoConverter::class)
    @SerializedName("images_info")
    var imagesInfo: List<ImageInfo> = ArrayList(),
    @TypeConverters(CommunityVideoConverter::class)
    var videos: List<CommunityVideoEntity> = ArrayList(),
    var count: Count = Count(),
    var community: CommunityEntity = CommunityEntity(),
    var time: TimeEntity? = TimeEntity(),
    var user: UserEntity = UserEntity(),
    @Ignore
    var read: Boolean = true,
    @Ignore
    var me: MeEntity = MeEntity(),
    @Ignore
    var commentable: Boolean = true,
    @Ignore
    var bbs: CommunityEntity = CommunityEntity(),
    var des: String = "",
    var url: String = "",
    @TypeConverters(VideoInfoConverter::class)
    @SerializedName("video_info")
    var videoInfo: VideoInfo = VideoInfo(),
    var poster: String = "",
    var length: Long = 0,
    @Ignore
    var type: String = "",
    var status: String = "",
    var content: String = "",
    @SyncIgnore // questions里的vote当前entity的vote冲突
    @TypeConverters(QuestionsConverter::class)
    @SerializedName("question")
    var questions: Questions = Questions(),
    @Ignore
    @SerializedName("recommend_id")
    var recommendId: String = ""
) : Parcelable {

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

    fun transformAnswerEntity(): AnswerEntity {
        if (type != "answer") {
            questions = Questions(title = title, description = brief)
        }
        val answer = AnswerEntity()
        answer.id = id
        answer.articleTitle = title
        answer.questions = questions
        answer.brief = brief
        answer.images = images
        answer.videos = videos
        answer.user = user
        if (type.contains("video")) {
            answer.time = time?.upload
        } else {
            answer.time = time?.create
        }
        answer.vote = count.vote
        answer.active = active
        answer.commentCount = count.comment
        answer.answerCount = count.answer
        answer.orderTag = orderTag
        answer.read = read
        answer.me = me
        answer.commentable = commentable
        answer.communityId = community.id
        answer.communityName = community.name
        answer.bbs = bbs
        answer.type = type
        answer.imagesInfo = imagesInfo
        answer.des = des
        answer.url = url
        answer.videoInfo = videoInfo
        answer.poster = poster
        answer.length = length
        answer.count = count
        answer.status = status
        answer.content = content
        return answer
    }

    fun transformForumVideoEntity(): ForumVideoEntity {
        val forumVideoEntity = ForumVideoEntity()
        if (type.contains("video")) {
            forumVideoEntity.id = id ?: ""
            forumVideoEntity.title = title
            forumVideoEntity.des = des
            forumVideoEntity.url = url
            forumVideoEntity.status = status
            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
                forumVideoEntity.duration = newVideos[0].duration
                forumVideoEntity.videoInfo = VideoInfo(width = newVideos[0].width, height = newVideos[0].height)
            }
        }
        return forumVideoEntity
    }
}

@Parcelize
data class Count(
    @SyncPage(syncNames = [SyncFieldConstants.ARTICLE_COMMENT_COUNT, SyncFieldConstants.ANSWER_COMMENT_COUNT])
    var comment: Int = 0,
    @SyncPage(syncNames = [SyncFieldConstants.ARTICLE_VOTE_COUNT, SyncFieldConstants.ANSWER_VOTE_COUNT])
    var vote: Int = 0,
    var favorite: Int = 0,
    @SyncPage(syncNames = [SyncFieldConstants.ANSWER_COMMENT_REPLY_COUNT])
    var reply: Int = 0,
    var game: Int = 0,
    @SerializedName("game_played")
    var playedGame: Int = 0,
    var hot: Int = 0,
    var share: Int = 0
) : Parcelable {

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

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

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

@Parcelize
data class TimeEntity(
    var create: Long = 0,
    var update: Long = 0,
    var edit: Long = 0,
    var upload: Long = 0
) : Parcelable