package com.gh.ndownload.suspendwindow.view

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.gamecenter.R
import com.gh.ndownload.suspendwindow.data.NDownloadState
import com.gh.gamecenter.common.utils.display
import com.gh.gamecenter.common.view.RadiusCardView

class NDownloadSuspendIconView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : FrameLayout(context, attrs, defStyle) {

    private var onIconClickListener: OnIconClickListener? = null

    private var downloadState: NDownloadState? = null

    private var iconUrl: String? = null

    init {
        val padding = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            4F,
            context.resources.displayMetrics
        ).toInt()
        setPadding(padding, padding, padding, padding)
    }

    private val icon = SimpleDraweeView(context).also {
        val layoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT
        )
        val cardView = RadiusCardView(context).apply {
            cardElevation = 0F
            setRadius(4F, 4F, 4F, 4F)
        }
        val cardViewLayoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT
        )
        cardViewLayoutParams.gravity = Gravity.CENTER_VERTICAL
        cardView.addView(it, layoutParams)

        addView(cardView, cardViewLayoutParams)
    }

    private val iconMask = TextView(context).also {
        val layoutParams = ViewGroup.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT
        )
        it.visibility = View.GONE
        it.textSize = 10F
        it.gravity = Gravity.CENTER
        it.setTextColor(ContextCompat.getColor(context, android.R.color.white))
        it.setBackgroundResource(R.drawable.shape_download_suspend_icon_mask)
        it.setOnClickListener { view ->
            onIconClickListener?.onIconClick(view, downloadState)
        }
        addView(it, layoutParams)
    }

    private val badge = TextView(context).also {
        val minSize =  TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            12F,
            resources.displayMetrics
        ).toInt()
        val layoutParams = LayoutParams(
            LayoutParams.WRAP_CONTENT,
            minSize
        ).apply {
            gravity = Gravity.TOP or Gravity.END
            topMargin = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -4F,
                resources.displayMetrics
            ).toInt()
            rightMargin = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -4F,
                resources.displayMetrics
            ).toInt()
        }
        it.minWidth = minSize
        it.textSize = 8F
        it.gravity = Gravity.CENTER
        it.visibility = View.GONE
        it.setTextColor(ContextCompat.getColor(context, android.R.color.white))
        it.setBackgroundResource(R.drawable.shape_download_suspend_icon_badge)
        it.includeFontPadding = false
        val padding = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            2F,
            resources.displayMetrics
        ).toInt()
        it.setPadding(padding, 0, padding, 0)
        addView(it, layoutParams)
    }

    fun setOnIconClickListener(listener: OnIconClickListener?) {
        this.onIconClickListener = listener
    }

    fun setDownloadCount(count: Int) {
        badge.visibility = if (count <= 1) {
            View.GONE
        } else {
            badge.text = if(count > 99) "99+" else "$count"
            View.VISIBLE
        }
    }

    fun setDownloadIconUrl(url: String) {
        if (iconUrl == url) return
        this.iconUrl = url
        icon.display(url)
    }

    fun setDownloadState(state: NDownloadState) {
        if (isCurrentDownloadState(state)) return
        val text = getDownloadStateText(state)
        iconMask.visibility = View.VISIBLE
        iconMask.text = text
        downloadState = state
    }

    private fun isCurrentDownloadState(state: NDownloadState): Boolean {
        val downloadState = this.downloadState
        if (state is NDownloadState.Downloading && downloadState is NDownloadState.Downloading) {
            return state.progress == downloadState.progress
        }
        return downloadState == state
    }

    private fun getDownloadStateText(state: NDownloadState): String =
        when (state) {
            is NDownloadState.Downloading -> "${state.progress}%"
            NDownloadState.Complete -> context.getString(R.string.finish)
            NDownloadState.Install -> context.getString(com.gh.gamecenter.feature.R.string.install_1)
            NDownloadState.Installing -> context.getString(com.gh.gamecenter.feature.R.string.installing)
            NDownloadState.Pause -> context.getString(com.gh.gamecenter.feature.R.string.resume)
        }

    interface OnIconClickListener {
        fun onIconClick(v: View, downloadState: NDownloadState?)
    }
}