package com.gh.gamecenter.common.entity

import android.os.Parcel
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize

/**
 * Created by khy on 24/05/17.
 */

class ToolBoxEntity : Parcelable {

    @SerializedName(value = "_id", alternate = arrayOf("toolkit_id"))
    var id: String? = null

    var icon: String? = null

    var name: String? = null

    var des: String? = null

    var url: String? = null

    var time: Long = 0

    // 用户上次打开使用时间（仅用于历史记录）
    var lastOpenTime: Long = 0

    @SerializedName("me")
    var me: ToolboxMeEntity? = null


    override fun describeContents(): Int {
        return 0
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeString(this.id)
        dest.writeString(this.icon)
        dest.writeString(this.name)
        dest.writeString(this.des)
        dest.writeString(this.url)
        dest.writeLong(this.time)
        dest.writeParcelable(this.me, flags)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as ToolBoxEntity

        if (id != other.id) return false
        if (icon != other.icon) return false
        if (name != other.name) return false
        if (des != other.des) return false
        if (url != other.url) return false
        if (time != other.time) return false
        if (me != other.me) return false

        return true
    }

    override fun hashCode(): Int {
        var result = id?.hashCode() ?: 0
        result = 31 * result + (icon?.hashCode() ?: 0)
        result = 31 * result + (name?.hashCode() ?: 0)
        result = 31 * result + (des?.hashCode() ?: 0)
        result = 31 * result + (url?.hashCode() ?: 0)
        result = 31 * result + time.hashCode()
        result = 31 * result + (me?.hashCode() ?: 0)
        return result
    }

    constructor()

    protected constructor(`in`: Parcel) {
        this.id = `in`.readString()
        this.icon = `in`.readString()
        this.name = `in`.readString()
        this.des = `in`.readString()
        this.url = `in`.readString()
        this.time = `in`.readLong()
        this.me = `in`.readParcelable<ToolboxMeEntity>(ToolboxMeEntity::class.java.classLoader)
    }

    @Parcelize
    class ToolboxMeEntity(
        @SerializedName("is_toolkit_favorite")
        var isToolkitFavorite: Boolean = false
    ) : Parcelable

    companion object {

        const val TAG: String = "ToolBoxEntity"

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

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