package com.yidianling.ydlcommon.custom.utils

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.PixelFormat
import android.graphics.Point
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.yidianling.common.tools.RxImageTool
import com.yidianling.ydlcommon.custom.player.YDLMusicHelper
import com.yidianling.ydlcommon.custom.view.PlayerFloatView

/**
 * 音频播放悬浮窗 帮助类
 *
 * 需要在Activity onDestroy中调用removeView()方法移除
 * 避免内存泄露
 */
class PlayerFloatHelper {

    companion object {
        @SuppressLint("StaticFieldLeak")
        private var mPlayerFloatView: PlayerFloatView? = null
        //已添加悬浮窗页面全路径名
        private var showingPageName: String? = null
        private var wm: WindowManager? = null
        /**
         * 如果有音频正在播放则显示
         */
        fun showIfPlaying(context: Context) {
            if (YDLMusicHelper.isFMPlaying()|| YDLMusicHelper.isConfidePlaying()  || YDLMusicHelper.isCoursePlaying() || YDLMusicHelper.isVideoPlaying) {
                show(context)
                mPlayerFloatView?.updatePlayState()
            } else {
                hide()
            }
        }

        /**
         * 显示悬浮控件
         */
        fun show(context: Context) {
            if (mPlayerFloatView == null) {
                mPlayerFloatView =
                    PlayerFloatView(context)
            }

            if (showingPageName != context::class.qualifiedName) {
                mPlayerFloatView?.resetWm(context)
                addFloatToWm(context)
            }

            mPlayerFloatView?.resetView()

            mPlayerFloatView?.visibility = View.VISIBLE
            mPlayerFloatView?.setPlayingState()
        }

        fun hide() {
            mPlayerFloatView?.visibility = View.GONE
            mPlayerFloatView?.stopVideo()
        }

        fun addClickListener(listener: PlayerFloatView.FloatViewPlayListener) {
            mPlayerFloatView?.addFloatClickListener(listener)
        }

        fun removeClickListener(listener: PlayerFloatView.FloatViewPlayListener) {
            mPlayerFloatView?.removeFloatClickListener(listener)
        }

        fun isShow(context: Context): Boolean {
            return !TextUtils.isEmpty(showingPageName) && showingPageName == context::class.qualifiedName && mPlayerFloatView?.visibility == View.VISIBLE
        }

        fun removeView(context: Context) {
            if (TextUtils.isEmpty(showingPageName) || showingPageName != context::class.qualifiedName) {
                return
            }
            mPlayerFloatView?.stopVideo()
            mPlayerFloatView?.visibility = View.GONE
            wm?.removeViewImmediate(mPlayerFloatView)
            showingPageName = ""
            wm = null
        }

        fun onDestroy() {
            if (mPlayerFloatView != null) {
                if (!TextUtils.isEmpty(showingPageName)) {
                    wm?.removeViewImmediate(mPlayerFloatView)
                    showingPageName = ""
                }

                mPlayerFloatView?.onDestroy()
                mPlayerFloatView?.removeAllViews()

            }
        }

        fun setPlayingState(context: Context) {
            if (YDLMusicHelper.playType == 1) {
                hide()
                YDLMusicHelper.playType = 0
                show(context)
            }
            mPlayerFloatView?.setPlayingState()
        }

        fun updatePlayState() {
            mPlayerFloatView?.updatePlayState()
        }

        private fun addFloatToWm(context: Context) {
            if (wm != null && !TextUtils.isEmpty(showingPageName)) {
                wm?.removeViewImmediate(mPlayerFloatView)
                wm = null
            }
            //获取WindowManager
            wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            //设置LayoutParams(全局变量）相关参数
            val wmParams = mPlayerFloatView?.wmParams
            wmParams?.type = WindowManager.LayoutParams.TYPE_APPLICATION   //设置window type
            wmParams?.format = PixelFormat.RGBA_8888   //设置图片格式，效果为背景透明
            //设置Window flag
            wmParams?.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            wmParams?.gravity = Gravity.LEFT or Gravity.TOP   //调整悬浮窗口至左上角
            //以屏幕左上角为原点，设置x、y初始值
            val size = Point()
            wm?.defaultDisplay?.getSize(size)
            if (YDLMusicHelper.playType == 0) {//音频
                wmParams?.x = 0
                wmParams?.y = size.y * 5 / 6
                //设置悬浮窗口长宽数据
                wmParams?.width = WindowManager.LayoutParams.MATCH_PARENT
                wmParams?.height = RxImageTool.dp2px(56f)
            } else {
                wmParams?.x = size.x - RxImageTool.dp2px(220f)
                wmParams?.y = size.y * 3 / 4
                //设置悬浮窗口长宽数据
                wmParams?.width = RxImageTool.dp2px(220f)
                wmParams?.height = RxImageTool.dp2px(124f)
            }
            //显示myFloatView图像
            wm?.addView(mPlayerFloatView, wmParams)
            showingPageName = context::class.qualifiedName!!
        }
    }

}
