package im.angry.openeuicc.core

import im.angry.openeuicc.util.*
import net.typeblog.lpac_jni.LocalProfileAssistant

class EuiccChannelWrapper(orig: EuiccChannel) : EuiccChannel {
    private var _inner: EuiccChannel? = orig

    private val channel: EuiccChannel
        get() {
            if (_inner == null) {
                throw IllegalStateException("This wrapper has been invalidated")
            }

            return _inner!!
        }

    override val type: String
        get() = channel.type
    override val port: UiccPortInfoCompat
        get() = channel.port
    override val slotId: Int
        get() = channel.slotId
    override val logicalSlotId: Int
        get() = channel.logicalSlotId
    override val portId: Int
        get() = channel.portId
    private val lpaDelegate = lazy {
        LocalProfileAssistantWrapper(channel.lpa)
    }
    override val lpa: LocalProfileAssistant by lpaDelegate
    override val valid: Boolean
        get() = channel.valid

    override fun close() = channel.close()

    fun invalidateWrapper() {
        _inner = null

        if (lpaDelegate.isInitialized()) {
            (lpa as LocalProfileAssistantWrapper).invalidateWrapper()
        }
    }
}