package cz.muni.sutralog.ui.detail

import android.app.AlertDialog
import android.content.DialogInterface
import android.os.Bundle
import android.text.Html
import android.text.Html.fromHtml
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import cz.muni.sutralog.R
import cz.muni.sutralog.data.RockPresentableListItem
import cz.muni.sutralog.databinding.RockDetailBinding
import cz.muni.sutralog.lib.SutralogStaticLib.Companion.dateToString
import cz.muni.sutralog.lib.SutralogStaticLib.Companion.getGoogleMapsLink
import cz.muni.sutralog.repository.CategoryRepository
import cz.muni.sutralog.repository.RockRepository

class RockDetailFragment : Fragment() {

    private lateinit var binding: RockDetailBinding

    private val rockRepository: RockRepository by lazy {
        RockRepository(requireContext())
    }

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

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

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

        val rockId = RockDetailFragmentArgs.fromBundle(requireArguments()).rockId
        val rock = rockRepository.getRockById(rockId)
        setRockLayoutValues(rock)

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

        binding.toolbar.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.edit -> findNavController().navigate(
                    RockDetailFragmentDirections
                        .actionRockDetailToRockEdit(rock)
                )
                R.id.remove -> {
                    AlertDialog.Builder(requireContext())
                        .setMessage("Opravdu smazat?")
                        .setCancelable(false)
                        .setNegativeButton("Zrušit") { dialogInterface: DialogInterface, i: Int ->
                            dialogInterface.dismiss()
                        }
                        .setPositiveButton("Smazat") { dialogInterface: DialogInterface, i: Int ->
                            rockRepository.deleteRock(rock)
                            findNavController().navigateUp()
                        }
                        .show()
                }
            }
            true
        }
    }

    private fun setRockLayoutValues(rock: RockPresentableListItem) {
        val category = categoryRepository.getCategoryById(rock.categoryId)

        binding.rockCategory.text = category.name
        binding.toolbar.title = "${rock.name} (${rock.id})"
        binding.rockCount.text = rock.count?.let{ count -> "$count ks"}
        binding.rockDate.text = rock.date?.let { date -> dateToString(date) }
        binding.rockLocality.text = rock.locality
        if (rock.latitude != null && rock.longitude != null) {
            binding.rockMapLink.text =
                fromHtml(getGoogleMapsLink(rock.latitude!!, rock.longitude!!), Html.FROM_HTML_MODE_COMPACT)
            binding.rockMapLink.movementMethod = LinkMovementMethod.getInstance()
        }
        binding.rockWhere.text = rock.where
        binding.rockFrom.text = rock.from
        binding.rockDescription.text = rock.description
        binding.rockComment.text = rock.comment
    }
}