package com.gh.gamecenter.entity

import android.os.Parcelable
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.User
import com.gh.gamecenter.feature.entity.UserEntity
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
import java.util.Objects

@Parcelize
data class FollowUserEntity(
    @SerializedName("_id")
    private val _id: String? = null,
    @SerializedName("type")
    private val _type: String? = null, // user：用户、bbs：论坛
    @SerializedName("user")
    private val _user: UserEntity? = null,
    @SerializedName("bbs")
    private var _bbs: Bbs? = null,
    @SerializedName("is_top")
    private val _isTop: Int? = null,
    @SerializedName("is_show_tip")
    private val _isShowTip: Int? = null,
) : Parcelable {

    val id: String
        get() = _id ?: ""

    val type: String
        get() = _type ?: ""

    val user: UserEntity
        get() = _user ?: UserEntity()

    val isTop: Boolean
        get() = _isTop == 1

    val isShowTip: Boolean
        get() = _isShowTip == 1

    val bbs: Bbs
        get() = _bbs ?: Bbs()

    val isUser: Boolean
        get() = type == TYPE_USER

    val name: String
        get() = if (isUser) {
            user.name ?: ""
        } else {
            bbs.name
        }

    val icon: String
        get() = when {
            type == TYPE_USER -> user.icon ?: ""
            bbs.type == BBS_TYPE_GAME -> bbs.game.icon ?: ""
            else -> bbs.icon
        }

    override fun equals(other: Any?): Boolean {
        return other is FollowUserEntity
                && id == other.id
                && type == other.type
                && user.id == other.user.id
                && user.name == other.user.name
                && bbs.id == other.bbs.id
                && bbs.name == other.bbs.name
                && bbs.type == other.bbs.type
                && bbs.icon == other.bbs.icon
                && bbs.game.icon == other.bbs.game.icon
                && isTop == other.isTop
                && isShowTip == other.isShowTip
                && icon == other.icon
    }

    override fun hashCode(): Int {
        return Objects.hash(
            _id,
            type,
            user.id,
            user.name,
            bbs.id,
            bbs.name,
            bbs.type,
            bbs.icon,
            bbs.game.icon,
            isTop,
            isShowTip,
            icon
        )
    }

    companion object {
        private const val TYPE_USER = "user"
        private const val BBS_TYPE_GAME = "game_bbs"
    }

    @Parcelize
    data class Bbs(
        @SerializedName("_id")
        private val _id: String? = null,
        @SerializedName("name")
        private val _name: String? = null,
        @SerializedName("type")
        private val _type: String? = null,
        @SerializedName("icon")
        private val _icon: String? = null,
        @SerializedName("game")
        private val _game: GameEntity? = null
    ) : Parcelable {

        val id: String
            get() = _id ?: ""

        val name: String
            get() = _name ?: ""

        val type: String
            get() = _type ?: ""

        val icon: String
            get() = _icon ?: ""

        val game: GameEntity
            get() = _game ?: GameEntity()
    }

    @Parcelize
    data class Game(
        @SerializedName("_id")
        private val _id: String? = null,
        @SerializedName("name")
        private val _name: String? = null,
        @SerializedName("icon")
        private val _icon: String? = null,
        @SerializedName("ori_icon")
        private val _oriIcon: String? = null,
        @SerializedName("active")
        private val _active: Boolean? = null
    ) : Parcelable {

        val id: String
            get() = _id ?: ""

        val name: String
            get() = _name ?: ""

        val icon: String
            get() = _icon ?: ""

        val oriIcon: String
            get() = _oriIcon ?: ""

        val active: Boolean
            get() = _active ?: false
    }
}