package com.yidianling.ydlcommon.custom.notification

import android.app.Notification
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import android.support.v4.content.ContextCompat
import android.widget.RemoteViews
import com.bumptech.glide.request.RequestOptions
import com.ydl.ydl_image.module.GlideApp
import com.ydl.ydl_image.target.YDLNotificationTarget
import com.yidianling.ydlcommon.core.base.BaseApplication
import com.yidianling.common.tools.LogUtil
import com.yidianling.ydlcommon.R

class MusicPlayerService : Service() {

    companion object {
        val TAG = MusicPlayerService::class.java.simpleName!!
        val ACTION_PLAY = "play"
        val ACTION_PAUSE = "pause"
        val ACTION_STOP = "stop"
        val ACTION_DETAIL = "detail"
        val REQUEST_CODE = 100

        fun startServiceFromCourse(context: Context,playType:String,headUrl:String?,title:String?){
            var i=Intent(context, MusicPlayerService::class.java)
            i.putExtra("play_type",playType)
            i.putExtra("head_url",headUrl)
            i.putExtra("title",title)
            context.startService(i)
        }
    }

//    private var coursePlayer = CoursePlayer2
//    private val myPlayer = MyPlayer.getInstance()
    private var playType: String? = null

    override fun onBind(intent: Intent): IBinder? = null


    @Suppress("DEPRECATION")
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        LogUtil.d(TAG, "onStartCommand")
        playType = intent?.getStringExtra("play_type")
        val remoteViews = createRemoteViews()
        val builder = Notification.Builder(applicationContext)
                .setContent(remoteViews)
                .setSmallIcon(R.drawable.ic_music_not) // 设置状态栏内的小图标
                .setWhen(System.currentTimeMillis()) // 设置该通知发生的时间
        val notification = builder.build() // 获取构建好的Notification
        val target = YDLNotificationTarget(BaseApplication.getApp(), R.id.iv_head, remoteViews, notification, 110)
        if (playType == "course") {
            val headUrl=intent?.getStringExtra("head_url")
            val title=intent?.getStringExtra("title")
            GlideApp.with(this)
                    .asBitmap()
                    .load(headUrl)
                    .apply(RequestOptions.centerCropTransform())
                    .into(target)
//            GlideApp.with(this)
//                    .asBitmap()
//                    .load(headUrl)
////                    .apply(RequestOptions.centerCropTransform())
//                    .into(target)
            remoteViews.setTextViewText(R.id.tv_title, title)
            if (PlayerState.isCoursePlaying()) {
                val intentPause = Intent(ACTION_PAUSE)
                intentPause.putExtra("play_type", playType)
                val pIntentPause = PendingIntent.getBroadcast(applicationContext,
                    REQUEST_CODE, intentPause, PendingIntent.FLAG_UPDATE_CURRENT)
                remoteViews.setOnClickPendingIntent(R.id.btn_pause_play, pIntentPause)
                remoteViews.setImageViewResource(R.id.btn_pause_play, R.drawable.ic_pause_24dp)
            } else {
                val intentPlay = Intent(ACTION_PLAY)
                intentPlay.putExtra("play_type", playType)
                val pIntentPlay = PendingIntent.getBroadcast(applicationContext,
                    REQUEST_CODE, intentPlay, PendingIntent.FLAG_UPDATE_CURRENT)
                remoteViews.setOnClickPendingIntent(R.id.btn_pause_play, pIntentPlay)
                remoteViews.setImageViewResource(R.id.btn_pause_play, R.drawable.ic_play_24dp)
            }
        } else {
//            GlideApp.with(this)
//                    .asBitmap()
//                    .load(myPlayer.cover)
//                    .centerCrop()
//                    .into(target)
//            remoteViews.setTextViewText(R.id.tv_title, myPlayer.title)
//            if (myPlayer.isPlaying) {
//                val intentPause = Intent(ACTION_PAUSE)
//                intentPause.putExtra("play_type", playType)
//                val pIntentPause = PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, intentPause, PendingIntent.FLAG_UPDATE_CURRENT)
//                remoteViews.setOnClickPendingIntent(R.id.btn_pause_play, pIntentPause)
//                remoteViews.setImageViewResource(R.id.btn_pause_play, R.mipmap.ic_pause_24dp)
//            } else {
//                val intentPlay = Intent(ACTION_PLAY)
//                intentPlay.putExtra("play_type", playType)
//                val pIntentPlay = PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, intentPlay, PendingIntent.FLAG_UPDATE_CURRENT)
//                remoteViews.setOnClickPendingIntent(R.id.btn_pause_play, pIntentPlay)
//                remoteViews.setImageViewResource(R.id.btn_pause_play, R.mipmap.ic_play_24dp)
//            }
        }
        startForeground(110, notification)
        return START_STICKY
    }


    private fun createRemoteViews(): RemoteViews {
        val remoteViews = RemoteViews(packageName, R.layout.ui_music_notification)

        val intentStop = Intent(ACTION_STOP)
        intentStop.putExtra("play_type", playType)
        val pIntentStop = PendingIntent.getBroadcast(applicationContext,
            REQUEST_CODE, intentStop, PendingIntent.FLAG_UPDATE_CURRENT)
        remoteViews.setOnClickPendingIntent(R.id.btn_close, pIntentStop)

        val intentDetail = Intent(ACTION_DETAIL)
        intentDetail.putExtra("play_type", playType)
        val pIntentDetail = PendingIntent.getBroadcast(applicationContext,
            REQUEST_CODE, intentDetail, PendingIntent.FLAG_UPDATE_CURRENT)
        remoteViews.setOnClickPendingIntent(R.id.iv_head, pIntentDetail)
        remoteViews.setOnClickPendingIntent(R.id.tv_title, pIntentDetail)
        val color = getColor()
        remoteViews.setTextColor(R.id.tv_title, color)
        return remoteViews
    }

    private fun getColor(): Int {
        var color = ContextCompat.getColor(BaseApplication.getApp(), R.color.default_text_color)
        val tempColor =
            com.yidianling.ydlcommon.custom.notification.NotificationHelper.getNotificationColor(BaseApplication.getApp())
        if (tempColor != com.yidianling.ydlcommon.custom.notification.NotificationHelper.INVALID_COLOR) {
            color = tempColor
        }
        return color
    }

    override fun onDestroy() {
        stopForeground(true)
        super.onDestroy()
    }

}
