package com.gh.gamecenter.feature.view

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.ContextCompat
import com.facebook.drawee.drawable.ScalingUtils
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.gamecenter.common.R
import splitties.views.assignAndGetGeneratedId
import splitties.views.dsl.constraintlayout.*
import splitties.views.dsl.core.*
import splitties.views.dsl.idepreview.UiPreView
import splitties.views.existingOrNewId

class GameIconUi(override val ctx: Context) : Ui {
    val gameIconIv: SimpleDraweeView
    val gameIconDecorationIv: SimpleDraweeView
    val borderView: View
    val bottomLabelTv: TextView

    override val root: ConstraintLayout = constraintLayout {
        gameIconIv = getIv(ctx)
        gameIconDecorationIv = getIv(ctx)
        borderView = View(ctx).apply {
            assignAndGetGeneratedId()
        }
        bottomLabelTv = initBottomLabelTv()
        add(gameIconIv, lParams(0, 0) {
            startOfParent()
            topOfParent()
            endOfParent()
            bottomOfParent()
        })
        add(gameIconDecorationIv, lParams(0, 0) {
            startToStartOf(gameIconIv)
            topToTopOf(gameIconIv)
            endToEndOf(gameIconIv)
            bottomToBottomOf(gameIconIv)
        })
        add(borderView, lParams(0, 0) {
            startToStartOf(gameIconIv)
            topToTopOf(gameIconIv)
            endToEndOf(gameIconIv)
            bottomToBottomOf(gameIconIv)
        })
        add(bottomLabelTv, lParams(0, 0) {
            startOfParent()
            endOfParent()
            bottomOfParent()
        })
        ConstraintSet().also {
            it.clone(this)
            it.setDimensionRatio(bottomLabelTv.existingOrNewId, "h,1000:275")
        }.applyTo(this)
    }

    private fun initBottomLabelTv() = textView {
        assignAndGetGeneratedId()
        setTextColor(Color.WHITE)
        isSingleLine = true
        typeface = Typeface.DEFAULT_BOLD
        textSize = 10F
        gravity = Gravity.BOTTOM.xor(Gravity.CENTER_HORIZONTAL)
        visibility = View.GONE
    }

    private fun getIv(context: Context): SimpleDraweeView {
        return SimpleDraweeView(context).apply {
            assignAndGetGeneratedId()
        }
    }
}

@Deprecated("For IDE preview only", level = DeprecationLevel.HIDDEN)
private class GameItemUiPreview(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : UiPreView(
    context = context,
    attrs = attrs,
    defStyleAttr = defStyleAttr,
    createUi = {
        GameIconUi(it)
    }
)