package com.yidianling.ydlcommon.custom.player

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.launcher.ARouter

/**
 * 音乐播放器辅助类
 * 兼容原播放逻辑
 */
object YDLMusicHelper {
    //公共参数
    var title: String? = null
    var cover: String? = null
    var name: String? = null

    //FM跳转参数
    var fmId: Int = 0

    //课程跳转参数
    var course_id: Int = 0//课程id
    //播放类型  1：视频  0：音频
    var playType = 0
    //课程播放地址
    var courseVideoUlr: String? = null
    //课程是否正在播放
    var isVideoPlaying = false

    //播放类型
    val PLAY_TYPE_FM = "player_type_fm"
    val PLAY_TYPE_COURSE = "play_type_course"
    val PLAY_TYPE_CONFIDE = "play_type_confide"
    val PLAY_TYPE_NONE = "play_type_none"

    //当前播放类型 - <电台、倾诉、课程>
    var playingType = PLAY_TYPE_NONE

    var time: Int = 0
    var allTime: Int = 0
    //当前音频是否播放完成
    var currentComplete: Boolean = false
    private var playListener: YDLMusicPlayer.PlayStateChangeListener? = null
    /**
     * 打开播放中的页面详情
     */
    fun startPlayingActivity(context: Context?, fullScreen: Int = 0) {
        if (playingType == PLAY_TYPE_FM) {
            //FM播放页
            startFMPlayActivity(context)
        } else if (playingType == PLAY_TYPE_COURSE) {
            var url = if (playType == 0) {
                getCurrentData()?.url
            } else {
                courseVideoUlr
            }
            //课程播放页
            startCoursePlayActivity(
                context,
                1,
                fullScreen,
                url,
                true
            )
        }
    }

    fun startCoursePlayActivity(context: Context?, from: Int, fullScreen: Int = 0, coursePlayUrl: String? = "", isFromFloatView: Boolean = false) {
        ARouter.getInstance()
                .build("/course/play")
                .withInt("course_id", course_id)
                .withInt("course_type", playType)
                .withString("coursePlayUrl", coursePlayUrl)
                .withInt("from", from)
                .withBoolean("isFromFloatView", isFromFloatView)
                .withInt("fullScreen", fullScreen)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation()
    }

    fun startFMPlayActivity(context: Context?) {
        val bundle = Bundle()
        bundle.putInt("id", fmId)
        ARouter.getInstance().build("/fm/detail")
                .withBundle("bundle", bundle)
                .navigation()
    }

    fun isCoursePlaying(): Boolean {
        return YDLMusicPlayer.getInstance().isPlaying() && playingType == PLAY_TYPE_COURSE
    }

    fun isFMPlaying(): Boolean {
        return YDLMusicPlayer.getInstance().isPlaying() && playingType == PLAY_TYPE_FM
    }

    fun isConfidePlaying(): Boolean {
        return YDLMusicPlayer.getInstance().isPlaying() && playingType == PLAY_TYPE_CONFIDE
    }

    fun isPlaying(): Boolean {
        return YDLMusicPlayer.getInstance().isPlaying()
    }

    fun fmPlay(url: String?) {
        playingType =
            PLAY_TYPE_FM
        play(url)
    }

    fun confidePlay(url: String?) {
        playingType =
            PLAY_TYPE_CONFIDE
        play(url)
    }

    fun coursePlay(url: String) {
        playingType =
            PLAY_TYPE_COURSE
        play(url)
    }

    fun pause() {
        YDLMusicPlayer.getInstance().pause()
    }

    fun seekTo(position: Int) {
        YDLMusicPlayer.getInstance().seekTo(position)
    }

    fun play(url: String?, autoSaveProgress: Boolean = false) {
        addHelperListener()
        YDLMusicPlayer.getInstance().play(url, autoSaveProgress)
    }

    /**
     * 直接播放
     * 适用于已设置过播放参数的情景
     */
    fun directPlay(index: Int = 0, autoSaveProgress: Boolean = false) {
        addHelperListener()
        YDLMusicPlayer.getInstance().play(autoSaveProgress, index)
    }

    private fun addHelperListener() {
        if (playListener == null) {
            playListener = object :
                YDLMusicPlayer.PlayStateChangeListener {
                override fun update(time: Int, allTime: Int) {
                    YDLMusicHelper.time = time
                    YDLMusicHelper.allTime = allTime
                    currentComplete = false
                }

                override fun reset() {
                    currentComplete = false
                }

                override fun completion() {
                    currentComplete = true
                }

                override fun prepared() {
                    currentComplete = false
                }
            }
            YDLMusicPlayer.getInstance()
                .addListener(playListener!!)
        }
    }

    fun start() {
        if (currentComplete) {
            YDLMusicPlayer.getInstance().replay()
        } else {
            YDLMusicPlayer.getInstance().start()
        }
    }

    fun stop() {
        YDLMusicPlayer.getInstance().stop()
    }

    fun replay() {
        YDLMusicPlayer.getInstance().replay()
    }

    fun release() {
        YDLMusicPlayer.getInstance().release()
    }

    fun getCurrentData(): MusicInfoBean? {
        return YDLMusicPlayer.getInstance().musicServiceControl?.getCurrentData()
    }


    /**
     * 清除临时数据
     */
    fun clearTempData() {
        title = null
        cover = null
        name = null
        courseVideoUlr = null
        fmId = 0
        isVideoPlaying = false
        course_id = 0
        playType = 0
    }
}

