package com.dafay.demo.aidl.proxy

import com.dafay.demo.aidl.callback.MethodCallback
import com.dafay.demo.aidl.inter.ReceiveListener
import com.dafay.demo.lib.base.utils.debug


object TestServiceProxy : BaseServiceConnect() {

    @Throws(RuntimeException::class)
    private fun checkServiceConnected() {
        if (!isServiceConnected()) {
            throw RuntimeException("service not connected")
        }
    }

    fun bindService() {
        bindToService()
    }

    @Throws(RuntimeException::class)
    fun add(value: Int) {
        checkServiceConnected()
        remoteServiceProxy!!.add(value, object : MethodCallback.Stub() {
            override fun onSuccess(resultJson: String?) {
                debug("add result:${resultJson}")
            }

            override fun onFailure(errorJson: String?) {
                debug("add result:${errorJson}")
            }
        })
    }

    @Throws(RuntimeException::class)
    fun send(msg: String): Int {
        checkServiceConnected()
        return remoteServiceProxy!!.send(msg)
    }

    fun registerReceiverListener(listener: ReceiveListener?) {
        if (!receiveListeners.contains(listener)) {
            receiveListeners.add(listener)
        }
    }
}
