package partisan.weforge.xyz.pulse

import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.core.content.getSystemService
import partisan.weforge.xyz.pulse.databinding.FragmentPopupSettingsBinding

class PopupSettingsFragment : Fragment() {

    private var _binding: FragmentPopupSettingsBinding? = null
    private val binding get() = _binding!!

    private lateinit var prefs: Preferences
    private lateinit var window: PopupWindow

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

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

        prefs = Preferences(requireContext())
        window = PopupWindow(requireContext(), null)

        binding.popupEnabledCheckbox.isChecked = prefs.popupEnabled
        binding.popupEnabledCheckbox.setOnCheckedChangeListener { _, isChecked ->
            prefs.popupEnabled = isChecked
            updateControls(isChecked)
        }

        binding.popupPreview.setOnClickListener {
            window.preview()
        }

        binding.redirectionDelay.value = (prefs.redirectionDelay / 1000).toFloat()
        binding.redirectionDelay.setLabelFormatter {
            String.format("%.1f", it)
        }
        binding.redirectionDelay.addOnChangeListener { _, value, _ ->
            prefs.redirectionDelay = (value * 1000).toLong()
        }

        val screenHeight = getScreenHeightPx()
        binding.popupHeightSlider.valueFrom = 0f
        binding.popupHeightSlider.valueTo = screenHeight.toFloat()
        binding.popupHeightSlider.value = prefs.popupPosition.toFloat()
        binding.popupHeightSlider.addOnChangeListener { _, value, _ ->
            prefs.popupPosition = value.toInt().coerceIn(0, screenHeight)
        }

        updateControls(prefs.popupEnabled)
    }

    private fun updateControls(enabled: Boolean) {
        binding.redirectionDelay.isEnabled = enabled
        binding.popupHeightSlider.isEnabled = enabled
        binding.popupPreview.isEnabled = enabled
    }

    private fun getScreenHeightPx(): Int {
        val wm = requireContext().getSystemService<android.view.WindowManager>()!!
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val bounds: Rect = wm.currentWindowMetrics.bounds
            bounds.height()
        } else {
            val metrics = DisplayMetrics()
            @Suppress("DEPRECATION")
            requireActivity().windowManager.defaultDisplay.getMetrics(metrics)
            metrics.heightPixels
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
