package org.foxarmy.barcodescannerforemployees.views

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.startActivity
import org.foxarmy.barcodescannerforemployees.*
import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import java.io.File

class AbstractProductView: LinearLayout {
    private var productLayout: ConstraintLayout? = null
    private var productPicture: ImageView? = null
    private var productNameField: TextView? = null
    private var netWeightField: TextView? = null
    private var categoryField: TextView? = null
    private var unitField: TextView? = null
    var abstractProduct: AbstractProduct = AbstractProduct()
    var isProductSelected = false
    private lateinit var activity: Activity

    constructor(context: Context, a: AttributeSet) : super(context, a) {
        activity = getActivity(context)!!
        val inflater:LayoutInflater = activity.layoutInflater
        inflater.inflate(R.layout.abstract_product_view, this)
    }

    constructor(activity: Activity, context: Context, abstractProduct: AbstractProduct) : super(context) {
        this.abstractProduct = abstractProduct
        this.activity = activity
        val inflater:LayoutInflater = activity.layoutInflater
        inflater.inflate(R.layout.abstract_product_view, this)

        update()
    }

    fun init () {
        productLayout = findViewById(R.id.productLayout)
        productPicture = findViewById(R.id.productPicture)
        productNameField = findViewById(R.id.productNameView)
        netWeightField = findViewById(R.id.productNetWeightView)
        categoryField = findViewById(R.id.categoryView)
        unitField = findViewById(R.id.unitView)


        productPicture!!.setOnClickListener {
            val fullscreenIntent = Intent(activity, FullscreenActivity::class.java)
            val extras = Bundle()
            extras.putString("imagehash", abstractProduct.imageHash)
            fullscreenIntent.putExtras(extras)
            startActivity(context, fullscreenIntent, extras)
        }

        productLayout!!.setOnClickListener {

        }

        unitField?.text = getUnitNameById(context, abstractProduct.unit)

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

    fun update() {
        init()

        val thumbnailsDir = File(context.cacheDir, "thumbnails")
        thumbnailsDir.mkdirs()
        val imageUri = getImageUri(activity, File(thumbnailsDir, "${abstractProduct.imageHash}.webp"))
        productPicture!!.setImageURI(imageUri)
        productPicture!!.rotation = 90f

        productNameField!!.text = abstractProduct.name
        netWeightField!!.text = abstractProduct.netWeight.toString()
        categoryField!!.text = DBStorageController(context).getCategoryNameById(DBStorageController(context).readableDatabase, abstractProduct.category)
    }
}