package cz.muni.sutralog.ui.detail

import android.content.DialogInterface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.skydoves.colorpickerview.ColorEnvelope
import com.skydoves.colorpickerview.ColorPickerDialog
import com.skydoves.colorpickerview.flag.FlagMode
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener
import cz.muni.sutralog.R
import cz.muni.sutralog.data.CategoryPresentableListItem
import cz.muni.sutralog.databinding.CategoryEditBinding
import cz.muni.sutralog.lib.ColorPickerFlag
import cz.muni.sutralog.repository.CategoryRepository
import java.util.*

class CategoryEditFragment : Fragment() {

    private lateinit var binding: CategoryEditBinding

    private val categoryRepository: CategoryRepository by lazy {
        CategoryRepository(requireContext())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = CategoryEditBinding.inflate(layoutInflater, container, false)
        return binding.root
    }

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

        val category = CategoryEditFragmentArgs.fromBundle(requireArguments()).category
        val addCategory = CategoryEditFragmentArgs.fromBundle(requireArguments()).add

        setCurrentCategoryValues(category)
        initColorPicker(category)

        binding.toolbar.title = if (addCategory) "Nová kategorie" else "Upravit kategorii"

        binding.toolbar.setNavigationOnClickListener{
            findNavController().navigateUp()
        }

        binding.toolbar.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.done -> {
                    saveValues(category)
                    findNavController().navigateUp()
                }
            }
            true
        }
    }

    private fun saveValues(oldCategory: CategoryPresentableListItem) {
        oldCategory.name = binding.categoryName.text.toString()
        oldCategory.color = (binding.categoryColorView.background as ColorDrawable).color

        var insertedId : Long = -1
        if(categoryRepository.insertCategory(oldCategory) == insertedId){
            categoryRepository.updateCategory(oldCategory)
        }
    }

    private fun initColorPicker(category: CategoryPresentableListItem) {
        binding.categoryColorView.setOnClickListener {
            val builder = ColorPickerDialog.Builder(requireContext(), R.style.ColorPicker)
                .setTitle("Vyber si barvu")
                .setPositiveButton("Potvrdit",
                    ColorEnvelopeListener { colorEnvelope: ColorEnvelope, fromUser: Boolean ->
                        binding.categoryColorView.setBackgroundColor(colorEnvelope.color)
                    })
                .setNegativeButton("Zrušit") { dialogInterface: DialogInterface, i: Int ->
                    dialogInterface.dismiss()
                }
            //.attachAlphaSlideBar(false)
            builder.colorPickerView.setInitialColor(category.color)
            val bubbleFlag = ColorPickerFlag(requireContext(), R.layout.color_picker_flag)
            bubbleFlag.flagMode = FlagMode.ALWAYS
            builder.colorPickerView.flagView = bubbleFlag
            builder.show()
        }
    }

    private fun setCurrentCategoryValues(category: CategoryPresentableListItem) {
        binding.categoryName.setText(category.name)
        binding.categoryColorView.setBackgroundColor(category.color)
    }
}