package com.gh.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.gh.gamecenter.R
import com.gh.gamecenter.category.CategoryListActivity
import com.gh.gamecenter.entity.CategoryEntity

class SubCategoryView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    LinearLayout(context, attrs, defStyleAttr) {

    var leftTv: TextView
    var centerTv: TextView
    var rightTv: TextView

    var leftDivider: View
    var rightDivider: View

    var primeCategory: CategoryEntity? = null
    var categoryTitle: String? = ""

    init {
        View.inflate(context, R.layout.layout_sub_category, this)

        leftTv = findViewById(R.id.tv_left_sub_category)
        centerTv = findViewById(R.id.tv_center_sub_category)
        rightTv = findViewById(R.id.tv_right_sub_category)

        leftDivider = findViewById(R.id.divider_left)
        rightDivider = findViewById(R.id.divider_right)
    }

    fun setLeftCategory(category: CategoryEntity) {
        setCategory(leftTv, category)
    }

    fun setCenterCategory(category: CategoryEntity) {
        setCategory(centerTv, category)
        leftDivider.visibility = View.VISIBLE
    }

    fun setRightCategory(category: CategoryEntity) {
        setCategory(rightTv, category)
        rightDivider.visibility = View.VISIBLE
    }

    private fun setCategory(tv: TextView, category: CategoryEntity) {
        tv.text = category.name
        tv.setOnClickListener {
            tv.context.startActivity(
                CategoryListActivity.getIntent(
                    tv.context,
                    categoryTitle!!,
                    primeCategory!!,
                    category.name!!
                )
            )
        }
    }

}