package com.gh.gamecenter.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.utils.dip2px

class DividerView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : View(context, attrs, defStyleAttr, defStyleRes) {

    private var size = DIVIDER_SIZE_THIN

    init {
        initAttrs(context, attrs)
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        attrs ?: return
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.DividerView)
        size = typedArray.getInt(R.styleable.DividerView_divider_size, DIVIDER_SIZE_REGULAR)
        typedArray.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val height = when (size) {
            DIVIDER_SIZE_THIN -> 1
            DIVIDER_SIZE_MEDIUM -> 0.5F.dip2px()
            else -> 1F.dip2px()
        }
        setMeasuredDimension(widthMeasureSpec, height)
    }

    companion object {
        private const val DIVIDER_SIZE_THIN = 0
        private const val DIVIDER_SIZE_MEDIUM = 1
        private const val DIVIDER_SIZE_REGULAR = 12
    }
}