package cn.yyxx.eyuancomm.impl.channel.huawei.floatview

import android.app.Activity
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import cn.yyxx.eyuancomm.comm.impl.CommSdkImpl

/**
 * @author #Suyghur.
 * Created on 2021/06/28
 */
class FloatCenterService : Service() {

    private var activity: Activity? = null
    private var floatBall: FloatBall? = null

    private lateinit var callback: FloatBall.FloatBallCallback


    fun initService(activity: Activity, callback: FloatBall.FloatBallCallback) {
        this.activity = activity
        this.callback = callback
    }

    fun attach() {
        if (activity==null){
            return
        }
        if (floatBall==null){
            floatBall = FloatBall(activity!!, CommSdkImpl.isLandscape, callback)
        }
        floatBall?.attach()
    }

    fun detach() {
        floatBall?.detach()
    }

    fun release() {
        floatBall?.release()
    }

    override fun onBind(intent: Intent?): IBinder {
        return FloatCenterServiceBinder()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }

    inner class FloatCenterServiceBinder : Binder() {
        val service: FloatCenterService = this@FloatCenterService
    }
}