package org.foxarmy.barcodescannerforemployees.views

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import org.foxarmy.barcodescannerforemployees.dataclasses.Category
import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R

class CategoryView : LinearLayout {
    var category: Category
    val categoryName: TextView
    val amountOfProducts: TextView
    var isCategorySelected = false

    constructor(activity: Activity, context: Context, category: Category) : super(context) {
        this.category = category

        val inflater: LayoutInflater = activity.layoutInflater
        inflater.inflate(R.layout.category_view, this)

        this.background = ContextCompat.getDrawable(context, if (isCategorySelected) R.drawable.outline_selected else R.drawable.outline)

        categoryName = findViewById(R.id.categoryNameTextView)
        amountOfProducts = findViewById(R.id.amountOfProducts)

        categoryName.text = category.name
        amountOfProducts.text = DBStorageController(context).getAmountOfAbstractProductsInCategory(DBStorageController(context).readableDatabase, category.id).toString()

        setOnLongClickListener {
            isCategorySelected = !isCategorySelected
            this.background = ContextCompat.getDrawable(context, if (isCategorySelected) R.drawable.outline_selected else R.drawable.outline)
            true
        }
    }
}