package com.yidianling.ydlcommon.custom.course

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import com.alibaba.android.arouter.launcher.ARouter
import com.yidianling.ydlcommon.core.base.BaseApplication
import com.yidianling.ydlcommon.custom.player.MusicPlayerHelper
import com.yidianling.ydlcommon.custom.player.player.NotifyClickListener
import com.yidianling.ydlcommon.custom.player.service.HMusicPlayService

@SuppressLint("StaticFieldLeak")
/**
 * 课程播放控制类
 * Created by harvie on 2015/4/28.
 */
object CoursePlayer {
    val TAG = "CoursePlayer"

    init {
        MusicPlayerHelper.listener = object :
            NotifyClickListener {
            override fun notifyDetailClick() {
                //点击通知栏详情
                startCoursePlayActivity(
                    mContext,
                    1
                )
            }
        }
    }

    var mContext = BaseApplication.getApp()

    var course_id: Int = 0//课程id

    var headUrl: String? = null //专家头像

    var playType = 0//播放类型 1.课程播放 2.课程试听

    //播放控制参数
    var musicControl: HMusicPlayService.MusicController? = null


    fun isPlaying(): Boolean {
        return musicControl?.isPlaying() ?: false
    }

    fun pause() {
        MusicPlayerHelper.pause(mContext)
    }

    fun play() {
        MusicPlayerHelper.start(mContext)
    }

    /**
     * 跳转音频课程播放,仅通过快捷方式进入课程时使用
     * from 1表示通知栏点进来，2表示悬浮图标点进来
     */
    fun startCoursePlayActivity(context: Context?, from: Int) {
        if (context != null && course_id > 0) {
//            val intent = Intent(context, CoursePlayActivity::class.java)
//            intent.putExtra("course_id", course_id)
//            intent.putExtra("course_type", playType)
//            intent.putExtra("from", from)
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//            context.startActivity(intent)
//            val it = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
//            context.sendBroadcast(it)
            ARouter.getInstance()
                    .build("/course/play")
                    .withInt("course_id", course_id)
                    .withInt("course_type", playType)
                    .withInt("from",from)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .navigation()
        }
    }

    fun onDestroy(context: Context) {
        course_id = 0
        headUrl = null
        musicControl?.pause()
        musicControl = null
        //销毁后台服务
        MusicPlayerHelper.stopService(context)
    }

}