package com.develop.common.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.develop.common.R
import com.develop.common.data_repo.db.CategoryType
import com.develop.common.databinding.CategotyTabViewBinding

class CategoryTabViewLayout : RelativeLayout {

    private var binding: CategotyTabViewBinding

    var onCategoryTabSelectedListener: OnCategoryTabSelectedListener? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    init {
        val root = View.inflate(context, R.layout.categoty_tab_view, this)
        binding = CategotyTabViewBinding.bind(root)
        binding.tvAll.setOnClickListener {
            binding.lineGap1.visibility = View.GONE
            binding.lineGap2.visibility = View.VISIBLE
            onCategoryTabSelectedListener?.onCurrentSelected(CategoryType.All)
            binding.tvAll.setBackgroundResource(R.drawable.shape_15pxf4a12e_top_left_conners)
            binding.tvRecipes.setBackgroundResource(R.drawable.shape_e5e5e5_solid)
            binding.tvAll.setTextColor(context.resources.getColor(R.color.color_F4A12E))
            binding.tvRecipes.setTextColor(context.resources.getColor(R.color.color_6B6B6B))
            binding.tvIngredients.setTextColor(context.resources.getColor(R.color.color_6B6B6B))
            binding.tvIngredients.setBackgroundResource(R.drawable.shape_15pxe5e5e5_top_right_conners)
        }

        binding.tvRecipes.setOnClickListener {
            binding.lineGap1.visibility = View.GONE
            binding.lineGap2.visibility = View.GONE
            binding.tvAll.setTextColor(context.resources.getColor(R.color.color_6B6B6B))
            binding.tvRecipes.setTextColor(context.resources.getColor(R.color.color_F4A12E))
            binding.tvIngredients.setTextColor(context.resources.getColor(R.color.color_6B6B6B))
            onCategoryTabSelectedListener?.onCurrentSelected(CategoryType.Recipes)
            binding.tvAll.setBackgroundResource(R.drawable.shape_15pxe5e5e5_top_left_conners)
            binding.tvRecipes.setBackgroundResource(R.drawable.shape_f4a12e_conners)
            binding.tvIngredients.setBackgroundResource(R.drawable.shape_15pxe5e5e5_top_right_conners)
        }

        binding.tvIngredients.setOnClickListener {
            binding.lineGap1.visibility = View.VISIBLE
            binding.lineGap2.visibility = View.GONE
            binding.tvAll.setTextColor(context.resources.getColor(R.color.color_6B6B6B))
            binding.tvRecipes.setTextColor(context.resources.getColor(R.color.color_6B6B6B))
            binding.tvIngredients.setTextColor(context.resources.getColor(R.color.color_F4A12E))
            onCategoryTabSelectedListener?.onCurrentSelected(CategoryType.Ingredients)
            binding.tvAll.setBackgroundResource(R.drawable.shape_15pxe5e5e5_top_left_conners)
            binding.tvRecipes.setBackgroundResource(R.drawable.shape_e5e5e5_solid)
            binding.tvIngredients.setBackgroundResource(R.drawable.shape_15pxf4a12e_top_right_conners)
        }
    }


    fun updateCategorySize(allSize: Int, recipesSize: Int, ingredientSize: Int) {
        binding.tvAll.text = "${context.getString(R.string.all)}(${allSize})"
        binding.tvRecipes.text = "${context.getString(R.string.Recipes)}(${recipesSize})"
        binding.tvIngredients.text = "${context.getString(R.string.ingredients)}(${ingredientSize})"
    }

    interface OnCategoryTabSelectedListener {
        fun onCurrentSelected(categoryType: CategoryType)
    }

}