package com.zj365.health.model

import android.os.Parcel
import android.os.Parcelable
import android.text.TextUtils
import com.lifesense.android.ble.core.application.model.enums.ConnectionState
import com.lifesense.android.ble.core.valueobject.DeviceInfo

class LSEDeviceInfoApp() : Parcelable {

    private var mLSEDeviceInfo: DeviceInfo? = null

    private var deviceId: String? = null
    private var deviceName: String? = null
    private var macAddress: String? = null
    private var rssi = 0
    private var connectionState: ConnectionState = ConnectionState.DISCONNECTED

    constructor(parcel: Parcel) : this() {
        deviceId = parcel.readString()
        deviceName = parcel.readString()
        macAddress = parcel.readString()
        rssi = parcel.readInt()
    }

   fun setConnectionState(state:ConnectionState){
       this.connectionState = state
   }

    fun getConnectionState():ConnectionState{
        return connectionState
    }

    fun getLSEDeviceInfo(): DeviceInfo? {
        return mLSEDeviceInfo
    }

    fun setLSEDeviceInfo(LSEDeviceInfo: DeviceInfo?) {
        mLSEDeviceInfo = LSEDeviceInfo
    }


    fun getDeviceId(): String? {
        return deviceId
    }

    fun setDeviceId(deviceId: String?) {
        this.deviceId = deviceId
    }

    fun getRssi(): Int {
        return rssi
    }

    fun setRssi(rssi: Int) {
        this.rssi = rssi
    }

    fun getDeviceName(): String? {
        if (!TextUtils.isEmpty(macAddress)) {
            val replacedMac = macAddress!!.replace(":", "")
            return deviceName + replacedMac.substring(replacedMac.length - 4)
        }
        return deviceName
    }

    fun setDeviceName(deviceName: String?) {
        this.deviceName = deviceName
    }

    fun getMacAddress(): String? {
        return macAddress
    }

    fun setMacAddress(macAddress: String?) {
        this.macAddress = macAddress
    }

    override fun equals(o: Any?): Boolean {
        if (o == null || macAddress == null) {
            return false
        }
        if (o is LSEDeviceInfoApp) {
            return macAddress.equals(o.getMacAddress(), ignoreCase = true)
        }
        return false
    }

    operator fun compareTo(o: Any): Int {
        val lse = o as LSEDeviceInfoApp
        return if (lse.getRssi() > rssi) {
            1
        } else {
            -1
        }
    }


    override fun toString(): String {
        return "LSEDeviceInfoApp{" +
                "mLSEDeviceInfo=" + mLSEDeviceInfo +
                ", deviceId='" + deviceId + '\'' +
                ", deviceName='" + deviceName + '\'' +
                ", macAddress='" + macAddress + '\'' +
                ", rssi=" + rssi +
                '}'
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(deviceId)
        parcel.writeString(deviceName)
        parcel.writeString(macAddress)
        parcel.writeInt(rssi)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<LSEDeviceInfoApp> {
        override fun createFromParcel(parcel: Parcel): LSEDeviceInfoApp {
            return LSEDeviceInfoApp(parcel)
        }

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