package com.gh.common.view

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.feature.entity.TagStyleEntity
import kotlin.math.ceil

/**
 * 首页游戏标签，最多显示三个，实际显示个数由控件宽度决定
 */
class GameTagFlexLinearLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    LinearLayout(context, attrs) {
    private var mTotalCount = 0
    private var mTags = ArrayList<TagStyleEntity>()
    private var mItemHeight = DisplayUtils.dip2px(13F)
    private var mPadding = DisplayUtils.dip2px(4F)
    private var mMargin = DisplayUtils.dip2px(4F)
    private var mTextSize = 9F
    private var mTotalWidth = 0
    private val mMaxCount = 3

    init {
        gravity = Gravity.CENTER_VERTICAL
    }

    fun setTags(datas: ArrayList<TagStyleEntity>) {
        val tags = datas.take(mMaxCount)
        mTags.clear()
        mTotalCount = tags.size
        mTotalWidth = measuredWidth
        val paint = Paint()
        paint.textSize = DisplayUtils.sp2px(context, mTextSize).toFloat()

        var currentWidth = 0f
        tags.forEach {
            val strWidth = getTextWidth(paint, it.name)
            if (currentWidth + strWidth + mPadding * 2 + mMargin <= mTotalWidth) {
                currentWidth += strWidth + mPadding * 2 + mMargin
                mTags.add(it)
            }
        }
        if (mTags.isNotEmpty()) {
            addTags()
        }
    }

    private fun getTextWidth(paint: Paint, str: String?): Int {
        var width = 0
        if (str != null && str.isNotEmpty()) {
            val len = str.length
            val widths = FloatArray(len)
            paint.getTextWidths(str, widths)
            for (i in 0 until len) {
                width += ceil(widths[i].toDouble()).toInt()
            }
        }
        return width
    }

    private fun addTags() {
        removeAllViews()
        mTags.forEach {
            addView(createView(it))
        }
    }

    private fun createView(tag: TagStyleEntity): View {
        return TextView(context).apply {
            text = tag.name
            includeFontPadding = false
            textSize = mTextSize
            gravity = Gravity.CENTER
            setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))

            val params = LayoutParams(LayoutParams.WRAP_CONTENT, mItemHeight)
            params.setMargins(0, 0, mMargin, 0)
            setPadding(mPadding, 0, mPadding, 0)
            layoutParams = params

            val gradientDrawable = createBackgroundDrawable()
            background = gradientDrawable
        }
    }

    private fun createBackgroundDrawable(): GradientDrawable {
        val gradientDrawable = GradientDrawable()
        gradientDrawable.setColor(Color.TRANSPARENT)
        gradientDrawable.setStroke(DisplayUtils.dip2px(context, 0.5f), Color.parseColor("#CCCCCC"))
        gradientDrawable.cornerRadius = DisplayUtils.dip2px(2f).toFloat()
        return gradientDrawable
    }
}