package com.yidianling.ydlcommon.custom.player.notifycation

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.yidianling.ydlcommon.custom.player.MusicPlayerHelper
import com.yidianling.ydlcommon.custom.player.service.HMusicPlayService
import android.os.Build
import java.lang.reflect.Method


/**
 * 播放通知栏状态变化接收器
 */
class MusicPlayerReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            HMusicPlayService.ACTION_PLAY -> {

                MusicPlayerHelper.start(context)
                collapseStatusBar(context)

            }
            HMusicPlayService.ACTION_PAUSE -> {
                MusicPlayerHelper.pause(context)
                collapseStatusBar(context)
            }

            HMusicPlayService.ACTION_DETAIL -> {
                MusicPlayerHelper.notoifyDetailClick()
                collapseStatusBar(context)
            }
            HMusicPlayService.ACTION_STOP -> {
                MusicPlayerHelper.pauseService(context)
                collapseStatusBar(context)
            }
        }
    }

    fun collapseStatusBar(context: Context) {
        try {
            val statusBarManager = context.getSystemService("statusbar")
            var collapse: Method
            if (Build.VERSION.SDK_INT <= 16) {
                collapse = statusBarManager!!.javaClass.getMethod("collapse")
            } else {
                collapse = statusBarManager!!.javaClass.getMethod("collapsePanels")
            }
            collapse.invoke(statusBarManager)
        } catch (localException: Exception) {
            localException.printStackTrace()
        }

    }
}
