package com.example.elva.ui.health

import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import com.example.elva.R
import com.example.elva.databinding.DialogCaloriesInputBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class WaterInputDialog(
    private val currentValue: Int,
    private val goal: Int,
    private val onSave: (consumed: Int, goal: Int) -> Unit
) : DialogFragment() {

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

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        _binding = DialogCaloriesInputBinding.inflate(layoutInflater)

        binding.etConsumed.setText(currentValue.toString())
        binding.etGoal.setText(goal.toString())

        return MaterialAlertDialogBuilder(requireContext())
            .setTitle(R.string.water)
            .setView(binding.root)
            .setPositiveButton(R.string.save) { _, _ ->
                val consumed = binding.etConsumed.text.toString().toIntOrNull() ?: 0
                val newGoal = binding.etGoal.text.toString().toIntOrNull() ?: goal
                onSave(consumed, newGoal)
            }
            .setNegativeButton(R.string.cancel, null)
            .create()
    }

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

class WeightInputDialog(
    private val currentValue: Float?,
    private val goal: Float?,
    private val onSave: (weight: Float, goal: Float?) -> Unit
) : DialogFragment() {

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

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        _binding = DialogCaloriesInputBinding.inflate(layoutInflater)

        currentValue?.let { binding.etConsumed.setText(String.format("%.1f", it)) }
        goal?.let { binding.etGoal.setText(String.format("%.1f", it)) }

        return MaterialAlertDialogBuilder(requireContext())
            .setTitle(R.string.weight)
            .setView(binding.root)
            .setPositiveButton(R.string.save) { _, _ ->
                val weight = binding.etConsumed.text.toString().toFloatOrNull() ?: 0f
                val newGoal = binding.etGoal.text.toString().toFloatOrNull()
                onSave(weight, newGoal)
            }
            .setNegativeButton(R.string.cancel, null)
            .create()
    }

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

class SleepInputDialog(
    private val currentValue: Float,
    private val goal: Float,
    private val quality: SleepQuality,
    private val onSave: (hours: Float, goal: Float, quality: SleepQuality) -> Unit
) : DialogFragment() {

    private var _binding: DialogCaloriesInputBinding? = null
    private val binding get() = _binding!!
    private var selectedQuality = quality

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        _binding = DialogCaloriesInputBinding.inflate(layoutInflater)

        binding.etConsumed.setText(String.format("%.1f", currentValue))
        binding.etGoal.setText(String.format("%.1f", goal))

        return MaterialAlertDialogBuilder(requireContext())
            .setTitle(R.string.sleep)
            .setView(binding.root)
            .setPositiveButton(R.string.save) { _, _ ->
                val hours = binding.etConsumed.text.toString().toFloatOrNull() ?: 0f
                val newGoal = binding.etGoal.text.toString().toFloatOrNull() ?: goal
                onSave(hours, newGoal, selectedQuality)
            }
            .setNegativeButton(R.string.cancel, null)
            .create()
    }

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

