package com.gh.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.facebook.drawee.drawable.ScalingUtils
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.gamecenter.R
import splitties.views.dsl.constraintlayout.*
import splitties.views.dsl.core.Ui
import splitties.views.dsl.core.add
import splitties.views.dsl.core.withTheme
import splitties.views.dsl.idepreview.UiPreView

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

    override val root: ConstraintLayout = constraintLayout {
        gameIconIv = getIv(ctx)
        gameIconDecorationIv = getIv(ctx)
        borderView = View(ctx)
        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)
        })
    }

    private fun getIv(context: Context): SimpleDraweeView {
        return SimpleDraweeView(context).apply {
            hierarchy.setPlaceholderImage(
                ContextCompat.getDrawable(context, R.drawable.occupy),
                ScalingUtils.ScaleType.FIT_XY
            )
            aspectRatio = 1.0F
        }
    }
}

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