package com.example.elva.ui.health

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

class CaloriesInputDialog(
    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.calories)
            .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
    }
}

