package com.audio.record.screen.test.service

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.media.projection.MediaProjection
import android.os.IBinder
import androidx.core.content.ContextCompat
import com.audio.record.screen.test.tool.Common
import java.lang.ref.WeakReference

object FloatingWindowBridge {


    //activity 向service发送
    val COMMEND_show_camera = "show_camera"  //显示前置摄像头
    val COMMEND_hide_camera = "hide_camera"

    val COMMEND_show_screenshot = "show_screenshot" //显示截屏按钮
    val COMMEND_hide_screenshot = "hide_screenshot"

    val COMMEND_show_ball = "show_ball"  //显示录制球
    val COMMEND_hide_ball = "hide_ball"

    val COMMEND_show_countdown = "show_countdown" //显示倒计时
    val COMMEND_pause_record = "pause_record"
    val COMMEND_resume_record = "resume_record"
    val COMMEND_stop_record = "stop_record"

    val COMMEND_sync_view_status = "sync_view_status"  //请求同步当前三个悬浮按钮状态

    val COMMEND_sync_recording_status = "sync_recording_status"  //请求同步当前录制状态
    //service 向activity反馈
    val CALL_start_recording = "start_recording"

    @SuppressLint("StaticFieldLeak")
    private var service: ScreenRecordService? = null
    private var isBound = false

    private val listeners = mutableSetOf<WeakReference<ConnectionListener>>()

    private var mBinder: ScreenRecordService.FloatingBinder? = null
    private val connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
            Common.showLog("Service-=======onServiceConnected")
            isBound = true
            mBinder = binder as? ScreenRecordService.FloatingBinder
            service = mBinder?.getService()
            listeners.forEach { it.get()?.onServiceConnected() }

        }

        override fun onServiceDisconnected(name: ComponentName?) {
            //----------------异常断连
            Common.showLog("Service-=======onServiceDisconnected")
            service = null
            isBound = false
        }
    }

    fun addListener(listener: ConnectionListener) {
        listeners.add(WeakReference(listener))
    }

    fun removeListener(listener: ConnectionListener) {
        listeners.removeAll { it.get() == null || it.get() == listener }
    }

    fun startAndBindService(context: Context) {
        val intent = Intent(context, ScreenRecordService::class.java)
        //不受进程存在与否限制
        ContextCompat.startForegroundService(context, intent)
        if (!isBound) {
            //通信
            context.bindService(intent, connection, Context.BIND_AUTO_CREATE)

        }
    }


    fun updateAudio(audio: Boolean) {
        service?.setIsAudio(audio)
    }

    fun sendCommand(command: String, countdownValues: Array<String>? = null) {
        Common.showLog("-------------command=${command}")
        when (command) {
            COMMEND_hide_camera -> service?.hideFrontCamera()
            COMMEND_show_camera -> service?.showCameraView()
            COMMEND_hide_ball -> service?.hideBall()
            COMMEND_show_ball -> service?.showBall()
            COMMEND_hide_screenshot -> service?.hideScreenshot()
            COMMEND_show_screenshot -> service?.showScreenshot()
            COMMEND_show_countdown -> countdownValues?.let { service?.showCountDownView(it) }
            COMMEND_pause_record -> service?.pauseRecording()
            COMMEND_resume_record -> service?.resumeRecording()
            COMMEND_stop_record -> service?.stopRecording()
            COMMEND_sync_view_status -> {
                service?.getViewStatus()
            }
            COMMEND_sync_recording_status->{
              service?.getRecordingStatus()
            }

            else -> {

            }
        }
    }

    fun updateMediaProjection(code: Int, mIntent: Intent) {
        service?.createMediaProjection(code, mIntent)

    }

    fun getMediaProjection() = service?.mIntent

    fun registerCallback(callback: FloatingCallback) {
        mBinder?.registerCallback(callback)
    }

    fun unregisterCallback(callback: FloatingCallback) {
        mBinder?.unregisterCallback(callback)
    }

    fun unbind(context: Context) {
        Common.showLog("Service-=======unbindService")
        if (isBound) {
            Common.showLog("Service-=======unbindService 1111")
            context.unbindService(connection)
//            service = null
//            mBinder = null
            isBound = false
        }
    }
}
