package org.foxarmy.barcodescannerforemployees.activities

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager
import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.ViewPagerAdapter
import org.foxarmy.barcodescannerforemployees.databinding.ActivityMainBinding
import org.foxarmy.barcodescannerforemployees.dataclasses.Category
import org.foxarmy.barcodescannerforemployees.fragments.CategoriesFragment
import org.foxarmy.barcodescannerforemployees.fragments.ShelfFragment
import org.foxarmy.barcodescannerforemployees.fragments.StorageFragment

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    lateinit var adapter: ViewPagerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.toolbar)
        setupViewPager(binding.tabViewpager)
        binding.tabTablayout.setupWithViewPager(binding.tabViewpager)

        binding.newElementFab.setOnClickListener { view ->
            val currentPosition = binding.tabTablayout.selectedTabPosition
            val fragment = adapter.getItem(currentPosition)

            when (fragment::class.simpleName.toString()) {
                "StorageFragment" -> {
                    val addAbstractProductIntent = Intent(this, AddAbstractProductActivity::class.java)
                    val extras = Bundle()
                    // I reuse the same stuff for editing and adding new product.
                    // if abstractProduct == null, it means that we need to create new object
                    extras.putParcelable("abstractProduct", null)
                    addAbstractProductIntent.putExtras(extras)
                    ContextCompat.startActivity(this, addAbstractProductIntent, extras)
                }

                "CategoriesFragment" -> {
                    val addCategoryIntent = Intent(this, AddCategoryActivity::class.java)
                    val extras = Bundle()
                    extras.putParcelable("category", Category(0, "New category"))
                    addCategoryIntent.putExtras(extras)
                    ContextCompat.startActivity(this, addCategoryIntent, extras)
                }

                "ShelfFragment" -> {
                    val addProductIntent = Intent(this, AddProductActivity::class.java)
                    val extras = Bundle()
                    extras.putParcelable("product", null)
                    addProductIntent.putExtras(extras)
                    ContextCompat.startActivity(this, addProductIntent, extras)
                }
            }
        }
    }

    private fun setupViewPager(viewpager: ViewPager) {
        adapter = ViewPagerAdapter(supportFragmentManager)

        adapter.addFragment(CategoriesFragment(), "Categories")
        adapter.addFragment(StorageFragment(), "Storage")
        adapter.addFragment(ShelfFragment(), "Shelf")
        //TODO: settings fragments

        // setting adapter to view pager.
        viewpager.setAdapter(adapter)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val currentPosition = binding.tabTablayout.selectedTabPosition
        val fragment = adapter.getItem(currentPosition)

        return when (item.itemId) {


            R.id.action_settings -> {
                true
            }

            R.id.action_delete -> {

                when (fragment::class.simpleName.toString()) {
                    "StorageFragment" -> {
                        AlertDialog.Builder(this)
                            .setMessage("Deleting an abstract product will also delete ALL the products, that belong to it. Do you want to proceed?")
                            .setPositiveButton("Yes") { _: DialogInterface, _: Int ->
                                val storageFragment = fragment as StorageFragment
                                storageFragment.removeSelected()
                            }
                            .setNegativeButton("No") { _: DialogInterface, _: Int ->

                            }.show()
                    }

                    "CategoriesFragment" -> {
                        AlertDialog.Builder(this)
                            .setMessage("Deleting a category will also delete ALL the products, that belong to that category. Do you want to proceed?")
                            .setPositiveButton("Yes") { _: DialogInterface, _: Int ->
                                val categoriesFragment = fragment as CategoriesFragment
                                categoriesFragment.removeSelected()
                            }
                            .setNegativeButton("No") { _: DialogInterface, _: Int ->

                            }.show()
                    }
                    "ShelfFragment" -> {
                        val shelfFragment = fragment as ShelfFragment
                        shelfFragment.removeSelected()
                    }
                }
                true
            }

            R.id.action_update -> {
                when (fragment::class.simpleName.toString()) {
                    "StorageFragment" -> {
                        val storageFragment = fragment as StorageFragment
                        storageFragment.updateSelected()
                    }

                    "CategoriesFragment" -> {
                        val categoriesFragment = fragment as CategoriesFragment
                        categoriesFragment.updateSelected()
                    }

                    "ShelfFragment" -> {
                        val shelfFragment = fragment as ShelfFragment
                        shelfFragment.updateSelected()
                    }
                }
                true
            }

            else -> super.onOptionsItemSelected(item)
        }
    }
}