package com.yidianling.ydlcommon.custom.player

import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import com.yidianling.ydlcommon.custom.player.player.NotifyClickListener
import com.yidianling.ydlcommon.custom.player.service.HMusicPlayService

/**
 * 音频播放器辅助类
 * Created by hgw on 2018/3/31.
 */
object MusicPlayerHelper {
    var listener: NotifyClickListener? = null

    fun bindService(context: Context, serviceConnection: ServiceConnection): Boolean {
        var intent = Intent(context, HMusicPlayService::class.java)
        return context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
    }

    fun unBinderService(activity: Context?, serviceConnection: ServiceConnection) {
        activity?.unbindService(serviceConnection)
    }

    fun start(context: Context) {
        var intent = Intent(context, HMusicPlayService::class.java)
        var bundle = Bundle()
        bundle.putString(HMusicPlayService.ACTION, HMusicPlayService.ONSTART)
        intent.putExtra("bundle", bundle)
        context.startService(intent)
    }

    fun start(context: Context, index: Int) {
        var intent = Intent(context, HMusicPlayService::class.java)
        var bundle = Bundle()
        bundle.putString(HMusicPlayService.ACTION, HMusicPlayService.ONSTART_NEW)
        bundle.putInt(HMusicPlayService.INDEX, index)
        intent.putExtra("bundle", bundle)
        context.startService(intent)
    }

    fun pause(context: Context) {
        var intent = Intent(context, HMusicPlayService::class.java)
        var bundle = Bundle()
        bundle.putString(HMusicPlayService.ACTION, HMusicPlayService.ACTION_PAUSE)
        intent.putExtra("bundle", bundle)
        context.startService(intent)
    }

    fun pauseService(context: Context) {
        //暂停播放
        var intent = Intent(context, HMusicPlayService::class.java)
        var bundle = Bundle()
        bundle.putString(HMusicPlayService.ACTION, HMusicPlayService.ACTION_STOP)
        intent.putExtra("bundle", bundle)
        context.startService(intent)
    }

    fun stopService(context: Context) {
        //停止服务
        listener = null
        var intent = Intent(context, HMusicPlayService::class.java)
        context.stopService(intent)
    }

    /**
     * 注册通知栏操作监听器监听器
     */
    fun registerListener(listener: NotifyClickListener) {
        MusicPlayerHelper.listener = listener
    }

    /**
     * 播放通知栏详情点击
     */
    fun notoifyDetailClick() {
        listener?.notifyDetailClick()
    }
}