package com.gh.gamecenter.feature.entity

import android.os.Parcel
import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName
import java.util.*

@Entity
class NewsEntity : Parcelable, Cloneable {

    @PrimaryKey
    @SerializedName(value = "_id", alternate = arrayOf("article_id"))
    var id: String = ""

    var type: String? = null

    var title: String? = null

    @SerializedName("time")
    var publishOn: Long = 0

    @SerializedName(value = "thumb", alternate = ["image"])
    var thumb: String? = null

    var thumbnail: ThumbnailEntity? = null

    @SerializedName(value = "intro", alternate = ["content"])
    var intro: String? = null

    var views: Int = 0

    @SerializedName(value = "link", alternate = ["url"])
    var link: String? = null

    @SerializedName("game_name")
    var gameName: String? = null

    var overtime: Long = 0

    var priority: Int = 0

    var active: Boolean = true

    var orderTag: Long = 0

    override fun clone(): Any {
        return super.clone()
    }


    override fun describeContents(): Int {
        return 0
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeString(this.id)
        dest.writeString(this.type)
        dest.writeString(this.title)
        dest.writeLong(this.publishOn)
        dest.writeString(this.thumb)
        dest.writeParcelable(this.thumbnail, flags)
        dest.writeString(this.intro)
        dest.writeInt(this.views)
        dest.writeString(this.link)
        dest.writeString(this.gameName)
        dest.writeLong(this.overtime)
        dest.writeInt(this.priority)
        dest.writeByte(if (active) 1 else 0)
    }

    constructor()

    protected constructor(`in`: Parcel) {
        this.id = `in`.readString() ?: ""
        this.type = `in`.readString()
        this.title = `in`.readString()
        this.publishOn = `in`.readLong()
        this.thumb = `in`.readString()
        this.thumbnail = `in`.readParcelable<ThumbnailEntity>(ThumbnailEntity::class.java.classLoader)
        this.intro = `in`.readString()
        this.views = `in`.readInt()
        this.link = `in`.readString()
        this.gameName = `in`.readString()
        this.overtime = `in`.readLong()
        this.priority = `in`.readInt()
        this.active = `in`.readByte() != 0.toByte()
    }

    companion object {

        const val TAG: String = "NewsEntity"

        fun deepCopy(list: List<NewsEntity>?): List<NewsEntity>? {
            if (list == null) return null
            val copy = ArrayList<NewsEntity>()
            for (t in list) {
                copy.add(t.clone() as NewsEntity)
            }
            return copy
        }

        @JvmField
        val CREATOR: Parcelable.Creator<NewsEntity> = object : Parcelable.Creator<NewsEntity> {
            override fun createFromParcel(source: Parcel): NewsEntity {
                return NewsEntity(source)
            }

            override fun newArray(size: Int): Array<NewsEntity?> {
                return arrayOfNulls(size)
            }
        }
    }
}
