package com.gh.gamecenter.feature.entity

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import android.os.Parcel
import android.os.Parcelable
import com.gh.gamecenter.feature.BR
import com.google.gson.annotations.SerializedName
import java.text.SimpleDateFormat
import java.util.*


class ServerCalendarEntity() : Parcelable, BaseObservable() {

    @SerializedName("_id")
    var id: String? = null

    private var note: String? = null

    private var firstName: CharSequence? = ""

    private var serverName: CharSequence? = ""

    private var time: Long = 0

    private var dataMark: Int = 0 // 判断字段是否符合条件 1:时间,2:名字,3:备注,4:时间&名字, 0: 正常

    var type: String? = null

    var remark: String = ""

    @SerializedName("notify_setting")
    var notifySetting: ServerCalendarNotifySetting? = null

    fun setNote(note: String?) {
        this.note = note
        notifyPropertyChanged(BR.note)
    }

    @Bindable
    fun getNote(): String? {
        return this.note
    }

    fun setFirstName(firstName: CharSequence?) {
        this.firstName = firstName
        notifyPropertyChanged(BR.firstName)
    }

    @Bindable
    fun getFirstName(): CharSequence? {
        return this.firstName
    }

    fun setServerName(serverName: CharSequence?) {
        this.serverName = serverName
        notifyPropertyChanged(BR.serverName)
    }

    @Bindable
    fun getServerName(): CharSequence? {
        return this.serverName
    }

    fun setTime(time: Long) {
        this.time = time
        notifyPropertyChanged(BR.time)
    }

    @Bindable
    fun getTime(): Long {
        return this.time
    }

    fun setDataMark(dataMark: Int) {
        this.dataMark = dataMark
        notifyPropertyChanged(BR.dataMark)
    }

    @Bindable
    fun getDataMark(): Int {
        return this.dataMark
    }

    override fun describeContents(): Int {
        return 0
    }

    fun getFormatTime(pattern: String): String? {
        if (time == 0L) return null
        val formatTime = SimpleDateFormat(pattern, Locale.CHINA)
        return formatTime.format(time * 1000)
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeString(this.id)
        dest.writeString(this.note)
        dest.writeLong(this.time)
        dest.writeString(this.type)
        dest.writeString(this.remark)
        dest.writeParcelable(this.notifySetting, flags)
    }

    constructor(source: Parcel) : this() {
        this.id = source.readString()
        this.note = source.readString()
        this.time = source.readLong()
        this.type = source.readString()
        this.remark = source.readString() ?: ""
        this.notifySetting = source.readParcelable(ServerCalendarNotifySetting::class.java.classLoader)
    }

    companion object {

        const val TAG = "ServerCalendarEntity"

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

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