package cn.yyxx.eyuangame.core.impl.floatball

import android.app.Activity
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.widget.ImageView
import cn.yyxx.eyuangame.core.entity.FloatFeature
import cn.yyxx.eyuangame.core.impl.SdkBridgeImpl
import cn.yyxx.eyuangame.core.impl.center.HybridActivity
import cn.yyxx.eyuangame.core.impl.center.MemberActivity
import cn.yyxx.eyuangame.core.ui.floatview.FloatingBall
import cn.yyxx.eyuangame.core.ui.floatview.FloatingBallMenu
import cn.yyxx.support.ResUtils

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

    private var floatingBall: FloatingBall? = null
    private var mActivity: Activity? = null

    private val callback = object : FloatingBall.FloatingBallCallback {
        override fun onUpdateBallView(ballView: ImageView, isLeftLocation: Boolean, isHide: Boolean) {
            val resId = if (isHide) {
                ballView.alpha = 0.8f
                if (isLeftLocation) {
                    ResUtils.getResId(mActivity, "yyxx_float_logo_left_img", "drawable")
                } else {
                    ResUtils.getResId(mActivity, "yyxx_float_logo_right_img", "drawable")
                }
            } else {
                ballView.alpha = 1.0f
                ResUtils.getResId(mActivity, "yyxx_float_logo_img", "drawable")
            }
            ballView.setBackgroundResource(resId)
        }

        override fun onInitMenuData(): MutableList<FloatingBallMenu.FloatingBallMenuItem> {
            val items = mutableListOf<FloatingBallMenu.FloatingBallMenuItem>()
            if (SdkBridgeImpl.initBean.floatCfg.memberCfg.switch == 1) {
                items.add(FloatingBallMenu.FloatingBallMenuItem(FloatFeature.FEATURE_MEMBER, "", ""))
            }
            if (SdkBridgeImpl.initBean.floatCfg.gifCfg.switch == 1) {
                items.add(FloatingBallMenu.FloatingBallMenuItem(FloatFeature.FEATURE_GIF, "", ""))
            }
            if (SdkBridgeImpl.initBean.floatCfg.gmCfg.switch == 1) {
                items.add(FloatingBallMenu.FloatingBallMenuItem(FloatFeature.FEATURE_GM, "", ""))
            }
            if (SdkBridgeImpl.initBean.floatCfg.chargeCfg.switch == 1) {
                items.add(FloatingBallMenu.FloatingBallMenuItem(FloatFeature.FEATURE_CHARGE, "", ""))
            }
            if (SdkBridgeImpl.initBean.floatCfg.inviteCfg.switch == 1) {
                items.add(FloatingBallMenu.FloatingBallMenuItem(FloatFeature.FEATURE_INVITE, "", ""))
            }
            return items
        }

        override fun onMenuItemClick(item: FloatingBallMenu.FloatingBallMenuItem, pos: Int) {
            when (item.type) {
                FloatFeature.FEATURE_MEMBER -> MemberActivity.start(mActivity!!)
                FloatFeature.FEATURE_GIF -> HybridActivity.start(mActivity!!, SdkBridgeImpl.initBean.floatCfg.gifCfg.url)
                FloatFeature.FEATURE_GM -> HybridActivity.start(mActivity!!, SdkBridgeImpl.initBean.floatCfg.gmCfg.url)
                FloatFeature.FEATURE_CHARGE -> HybridActivity.start(mActivity!!, SdkBridgeImpl.initBean.floatCfg.chargeCfg.url)
                FloatFeature.FEATURE_INVITE -> HybridActivity.start(mActivity!!, SdkBridgeImpl.initBean.floatCfg.inviteCfg.url)
            }
        }

        override fun onExpandMenu(hasRedDot: Boolean) {
        }

        override fun onDismissMenu() {
            floatingBall?.dismissMenu()
        }
    }

    override fun onCreate() {
        super.onCreate()
    }

    fun initService(activity: Activity) {
        this.mActivity = activity
    }

    fun attach() {
        if (mActivity == null) {
            return
        }

        if (floatingBall == null) {
            floatingBall = FloatingBall(mActivity!!, SdkBridgeImpl.isLand, callback)
        }
        floatingBall?.attach()
    }

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

    fun release() {
        floatingBall?.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
    }
}