package com.gh.gamecenter.game.rank

import android.content.Context
import android.graphics.Typeface
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.feature.view.GameIconView
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieDrawable
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.setDrawableEnd
import splitties.dimensions.dip
import splitties.views.bottomPadding
import splitties.views.dsl.constraintlayout.*
import splitties.views.dsl.core.*
import splitties.views.dsl.idepreview.UiPreView
import splitties.views.endPadding
import splitties.views.topPadding

class RankGameItemUi(override val ctx: Context) : Ui {

    var orderTv: TextView
    var iconIv: GameIconView
    var nameTv: TextView
    var tagContainer: LinearLayout
    var downloadTv: DownloadButton
    var downloadTipsLottie: LottieAnimationView
    var multiVersionDownloadTv: TextView

    override val root: ConstraintLayout = constraintLayout {
        orderTv = initOrderTv()
        iconIv = GameIconView(ctx).apply { setCornerRadius(dip(10)) }
        nameTv = initNameTv()
        tagContainer = LinearLayout(ctx).apply { orientation = LinearLayout.HORIZONTAL }
        downloadTv = initDownloadTv()
        downloadTipsLottie = initDownloadTipsLottie()
        multiVersionDownloadTv = initMultiVersionDownloadTv()

        topPadding = dip(8)
        bottomPadding = dip(8)
        endPadding = dip(8)

        add(orderTv, lParams(wrapContent, wrapContent) {
            topOfParent()
            bottomOfParent()
            startOfParent()
            endToStartOf(iconIv)

            startMargin = dip(4)
        })

        add(iconIv, lParams(dip(40), dip(40)) {
            topOfParent()
            bottomOfParent()
            startToEndOf(orderTv)
            endToStartOf(nameTv)

            startMargin = dip(4)
        })

        add(nameTv, lParams(0, wrapContent) {
            topOfParent()
            startToEndOf(iconIv)
            endToStartOf(downloadTv)
            bottomToTopOf(tagContainer)
            verticalChainStyle = packed

            startMargin = dip(12)
            endMargin = dip(8)
        })

        add(downloadTv, lParams(dip(52), dip(26)) {
            topOfParent()
            bottomOfParent()
            endOfParent()
        })

        add(multiVersionDownloadTv, lParams(wrapContent, dip(26)) {
            topToTopOf(downloadTv)
            bottomToBottomOf(downloadTv)
            startToStartOf(downloadTv)
            endToEndOf(downloadTv)
        })

        add(downloadTipsLottie, lParams(dip(16), dip(16)) {
            topToTopOf(downloadTv)
            endToEndOf(downloadTv)

            topMargin = dip(-8)
        })

        add(tagContainer, lParams(0, wrapContent) {
            topToBottomOf(nameTv)
            startToEndOf(iconIv)
            endToStartOf(downloadTv)
            bottomOfParent()

            topMargin = dip(8)
            startMargin = dip(12)
            endMargin = dip(8)
        })
    }

    private fun initOrderTv() = textView {
        gravity = Gravity.CENTER
        textSize = 12F
        setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.title))
        minWidth = dip(24)
        minHeight = dip(24)
    }

    private fun initNameTv() = textView {
        setSingleLine()
        setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.text_primary))
        textSize = 13F
        typeface = Typeface.DEFAULT_BOLD
        ellipsize = TextUtils.TruncateAt.END
    }

    private fun initDownloadTv() = DownloadButton(ctx).apply {
        id = R.id.download_btn
        text = context.getString(com.gh.gamecenter.feature.R.string.download)
        showProgress = true
    }

    private fun initDownloadTipsLottie(): LottieAnimationView {
        return LottieAnimationView(ctx).apply {
            id = R.id.downloadTipsLottie
            repeatMode = LottieDrawable.RESTART
            repeatCount = LottieDrawable.INFINITE
            visibility = View.GONE
        }
    }

    private fun initMultiVersionDownloadTv(): TextView {
        return textView {
            id = R.id.multiVersionDownloadTv
            textSize = 12F
            gravity = Gravity.CENTER
            text = "展开"
            setTextColor(ContextCompat.getColor(context, com.gh.gamecenter.common.R.color.white))
            setDrawableEnd(AppCompatResources.getDrawable(context, com.gh.gamecenter.feature.R.drawable.ic_jump_universal))
            compoundDrawablePadding = dip(2)
            visibility = View.GONE
        }
    }
}


@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 = {
        RankGameItemUi(it).apply {
            nameTv.text = "地海争霸：巫妖王再怒"
            tagContainer.addView(textView { text = "好玩" })
        }
    }
)