package com.ydl.media.music.utils

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.text.TextUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.ydl.media.music.model.Music


/**
 * Created by haorui on 2019-10-27 .
 * Des: 专辑封面图片加载器
 */
class CoverLoader private constructor() {

    private var context: Context? = null
    private var roundLength = 0

    private enum class Type {
        THUMB,
        ROUND,
        BLUR
    }

    private object SingletonHolder {
        val instance = CoverLoader()
    }

    fun init(context: Context) {
        this.context = context.applicationContext
        this.roundLength = ScreenUtils.getScreenWidth(context) / 2
    }

    fun setRoundLength(roundLength: Int) {
        if (this.roundLength != roundLength) {
            this.roundLength = roundLength
        }
    }

    fun loadThumb(music: Music,onCoverLoadListener: OnCoverLoadListenre) {
        loadCover(music, Type.THUMB,onCoverLoadListener)
    }

    fun loadRound(music: Music,onCoverLoadListener: OnCoverLoadListenre){
        loadCover(music, Type.ROUND,onCoverLoadListener)
    }

    fun loadBlur(music: Music,onCoverLoadListener: OnCoverLoadListenre){
        loadCover(music, Type.BLUR,onCoverLoadListener)
    }

    private fun loadCover(music: Music?, type: Type, onCoverLoadListener: OnCoverLoadListenre){
        val key = getKey(music)
        if (TextUtils.isEmpty(key)) {
            onCoverLoadListener.onCpmplete(getDefaultCover(type)!!)
            return
        }

        loadCoverByType(music!!, type,onCoverLoadListener)
    }

    private fun getKey(music: Music?): String? {
        if (music == null) {
            return null
        }

        return if (!TextUtils.isEmpty(music.coverPath)) {
            music.coverPath
        } else {
            null
        }
    }

    private fun getDefaultCover(type: Type): Bitmap? {
        when (type) {
            CoverLoader.Type.ROUND -> {
                var bitmap: Bitmap? = BitmapFactory.decodeResource(
                    context!!.resources,
                    com.ydl.media.R.drawable.play_page_default_cover
                )
                bitmap = ImageUtils.resizeImage(bitmap, roundLength, roundLength)
                return bitmap
            }
            CoverLoader.Type.BLUR -> return BitmapFactory.decodeResource(
                context!!.resources,
                com.ydl.media.R.drawable.play_page_default_bg
            )
            else -> return BitmapFactory.decodeResource(
                context!!.resources,
                com.ydl.media.R.drawable.default_cover
            )
        }
    }

    @SuppressLint("CheckResult")
    private fun loadCoverByType(music: Music, type: Type, onCoverLoadListener: OnCoverLoadListenre) {
        val disallowHardwareConfig =
            RequestOptions().format(DecodeFormat.PREFER_RGB_565).disallowHardwareConfig()

        Glide.with(context).asBitmap().apply(disallowHardwareConfig).load(music.coverPath).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap?, transition: Transition<in Bitmap>?) {
                when (type) {
                    CoverLoader.Type.ROUND -> {
                        var bitmap = ImageUtils.resizeImage(resource, roundLength, roundLength)
                        onCoverLoadListener.onCpmplete(ImageUtils.createCircleImage(bitmap)!!)
                    }
                    CoverLoader.Type.BLUR -> {
                        onCoverLoadListener.onCpmplete(ImageUtils.blur(resource)!!)
                    }
                }
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                loadCover(null, type,onCoverLoadListener)
            }
        })


    }

    companion object {
        val THUMBNAIL_MAX_LENGTH = 500
        private val KEY_NULL = "null"

        fun get(): CoverLoader {
            return SingletonHolder.instance
        }
    }



    interface OnCoverLoadListenre {
        fun onCpmplete(bitmap: Bitmap)
    }

}
