package org.foxarmy.barcodescannerforemployees.fragments

import android.content.Intent
import android.os.Bundle
import android.provider.BaseColumns
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.fragment.app.Fragment
import org.foxarmy.barcodescannerforemployees.CategoriesContract
import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity
import org.foxarmy.barcodescannerforemployees.dataclasses.Category
import org.foxarmy.barcodescannerforemployees.views.CategoryView

class CategoriesFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_categories, container, false)
    }

    override fun onResume() {
        super.onResume()

        updateContent()
    }

    fun removeSelected() {
        val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)

        val db = DBStorageController(requireContext())
        var deleted = false
        for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) {
            if (view.isCategorySelected) {
                db.eraseCategory(db.writableDatabase, view.category.id, requireContext())
                deleted = true
            }
        }

        if (!deleted) {
            Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show()
        }
        updateContent()
    }

    fun updateSelected() {
        val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
        for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) {
            if (view.isCategorySelected) {
                val addCategoryIntent = Intent(context, AddCategoryActivity::class.java)
                val extras = Bundle()
                extras.putParcelable("category", view.category)
                addCategoryIntent.putExtras(extras)
                ContextCompat.startActivity(context!!, addCategoryIntent, extras)
            }
        }
    }

    fun updateContent() {
        val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
        layout?.removeAllViews()

        val db = DBStorageController(requireContext()).readableDatabase

        val projection = arrayOf(
            BaseColumns._ID,
            CategoriesContract.CategoryEntry.CATEGORY_NAME
        )

        val cursor = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection, null, null, null, null, null)

        with (cursor) {
            while(moveToNext()) {
                val categoryId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
                val categoryName = getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME))

                val category = Category(categoryId, categoryName)

                val categoryView = CategoryView(requireActivity(), requireContext(), category)
                layout?.addView(categoryView)
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        updateContent()
    }
}