package com.omixlab.lckcontrol.shared

import android.os.Parcel
import android.os.Parcelable

data class LinkedAccount(
    val id: String,
    val serviceId: String,
    val displayName: String,
    val accountId: String,
    val avatarUrl: String? = null,
    val isAuthenticated: Boolean = false,
    val isEnabled: Boolean = true,
    val rtmpUrl: String? = null,
    val streamKey: String? = null,
) : Parcelable {

    constructor(parcel: Parcel) : this(
        id = parcel.readString()!!,
        serviceId = parcel.readString()!!,
        displayName = parcel.readString()!!,
        accountId = parcel.readString()!!,
        avatarUrl = parcel.readString(),
        isAuthenticated = parcel.readInt() != 0,
        isEnabled = if (parcel.dataAvail() > 0) parcel.readInt() != 0 else true,
        rtmpUrl = if (parcel.dataAvail() > 0) parcel.readString() else null,
        streamKey = if (parcel.dataAvail() > 0) parcel.readString() else null,
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(id)
        parcel.writeString(serviceId)
        parcel.writeString(displayName)
        parcel.writeString(accountId)
        parcel.writeString(avatarUrl)
        parcel.writeInt(if (isAuthenticated) 1 else 0)
        parcel.writeInt(if (isEnabled) 1 else 0)
        parcel.writeString(rtmpUrl)
        parcel.writeString(streamKey)
    }

    override fun describeContents(): Int = 0

    companion object CREATOR : Parcelable.Creator<LinkedAccount> {
        override fun createFromParcel(parcel: Parcel) = LinkedAccount(parcel)
        override fun newArray(size: Int) = arrayOfNulls<LinkedAccount>(size)
    }
}
