using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using AvaloniaApplication1.DataBase;
using DaireApplication.Views;
using System;

namespace DaireApplication;

public partial class RecipeEdit : UserControl
{
    Button controllButton = new Button();
    private MainWindow? _mainWindow;
    private UserTable? _currentUser;
    private RecipeTable? _recipeTable;


    public RecipeEdit()
    {
        InitializeComponent();
    }
    public RecipeEdit(MainWindow mainWindow, UserTable currentUser,RecipeTable recipeTable)
    {
        _currentUser = currentUser;
        _mainWindow = mainWindow;
        _recipeTable=recipeTable;
        InitializeComponent();
        setDefaultSettings();
    }

    string oldNumber = "";
    private void OnKeyClick(object? sender, RoutedEventArgs e)
    {
        if (sender is Button button)
        {

            var StackPanel = controllButton.Content as StackPanel;
            var Label = StackPanel.Children;
            if (Label[1] is TextBlock targetText)
            {
                //number += button.Content?.ToString();
                targetText.Text += button.Content ;
            }
            // Append the button's content to the input box
            //InputTextBox.Text += button.Content?.ToString();
        }

    }
    private void OnBackClick(object? sender, RoutedEventArgs e)
    {
        var StackPanel = controllButton.Content as StackPanel;
        var Label = StackPanel.Children;
        if (Label[1] is TextBlock targetText)
        {
            if (!string.IsNullOrEmpty(targetText.Text))
            {
                // Remove the last character from the text box
                targetText.Text = targetText.Text.Remove(targetText.Text.Length -1, 1);
                //number = number.Remove(number.Length - 1);
            }
        }

    }
    private void EnterClick(object? sender, RoutedEventArgs e)
    {
        // انسخ القيم الحالية بدل إنشاء كائن جديد فارغ
        RecipeTable recipe = new RecipeTable
        {
            Id = _recipeTable.Id,
            Name = _recipeTable.Name,
            TankTemp = _recipeTable.TankTemp,
            FountainTemp = _recipeTable.FountainTemp,
            Mixer = _recipeTable.Mixer ?? false,
            Fountain = _recipeTable.Fountain ?? false,
            MoldHeater = _recipeTable.MoldHeater ?? false,
            Vibration = _recipeTable.Vibration ?? false,
            VibHeater = _recipeTable.VibHeater ?? false,
            Pedal = _recipeTable.Pedal ?? false,
            PedalOnTime = _recipeTable.PedalOnTime,
            PedalOffTime = _recipeTable.PedalOffTime,
            HeatingGoal = _recipeTable.HeatingGoal,
            CoolingGoal = _recipeTable.CoolingGoal,
            PouringGoal = _recipeTable.PouringGoal
        };

        var heatingStackPanel = heatingBtn.Content as StackPanel;
        var heatingText = (heatingStackPanel?.Children[1] as TextBlock)?.Text;

        var coolingStackPanel = coolingBtn.Content as StackPanel;
        var coolingText = (coolingStackPanel?.Children[1] as TextBlock)?.Text;

        var stackPanel = controllButton.Content as StackPanel;
        var label = stackPanel?.Children;
        var targetText = label?[1] as TextBlock;
        var activeTxt = label?[0] as TextBlock;

        if (activeTxt == null || targetText == null)
            return;

        switch (activeTxt.Text)
        {
            case "HEATING:":
                if (!string.IsNullOrEmpty(targetText.Text) && int.TryParse(targetText.Text, out int heatingValue))
                {
                    if (heatingValue > 60)
                    {
                        tempErrorMsg.Text = "Heating Temperature must be lower than 60 °C";
                        return;
                    }
                    else if (heatingValue < 40)
                    {
                        tempErrorMsg.Text = "Heating Temperature must be greater than 40 °C";
                        return;
                    }

                    recipe.HeatingGoal = heatingValue;
                }
                break;

            case "POURING:":
                if (!string.IsNullOrEmpty(targetText.Text) && int.TryParse(targetText.Text, out int pouringValue))
                {
                    if (string.IsNullOrEmpty(heatingText) || string.IsNullOrEmpty(coolingText))
                    {
                        tempErrorMsg.Text = "Enter heating and cooling temperature first";
                        return;
                    }

                    if (!int.TryParse(heatingText, out int heatingTemp) || !int.TryParse(coolingText, out int coolingTemp))
                    {
                        tempErrorMsg.Text = "Invalid temperature values";
                        return;
                    }

                    if (pouringValue > heatingTemp)
                    {
                        tempErrorMsg.Text = $"Pouring Temperature must be lower than Heating Temperature ({heatingTemp}°C)";
                        return;
                    }
                    else if (pouringValue < coolingTemp)
                    {
                        tempErrorMsg.Text = $"Pouring Temperature must be greater than Cooling Temperature ({coolingTemp}°C)";
                        return;
                    }

                    recipe.PouringGoal = pouringValue;
                }
                break;

            case "COOLING:":
                if (!string.IsNullOrEmpty(targetText.Text) && int.TryParse(targetText.Text, out int coolingValue))
                {
                    if (coolingValue > 40)
                    {
                        tempErrorMsg.Text = "Cooling Temperature must be lower than 40 °C";
                        return;
                    }
                    else if (coolingValue < 20)
                    {
                        tempErrorMsg.Text = "Cooling Temperature must be greater than 20 °C";
                        return;
                    }

                    recipe.CoolingGoal = coolingValue;
                }
                break;
        }

        _recipeTable.UpdateRecipe(recipe);
        tempErrorMsg.Text = "";
        heatingBtn.IsEnabled = true;
        coolingBtn.IsEnabled = true;
        pouringBtn.IsEnabled = true;
        PopupOverlay.IsVisible = false;
        setDefaultSettings();
    }


    private void showPopUp(object? sender, RoutedEventArgs e)
    {
        if (sender is Button button)
        {
            heatingBtn.IsEnabled = false;
            coolingBtn.IsEnabled = false;
            pouringBtn.IsEnabled = false;
            button.IsEnabled = true;
            var StackPanel = button.Content as StackPanel;
            var Label = StackPanel.Children;
            if (Label[1] is TextBlock targetText)
            {
                oldNumber = targetText.Text;
            }
            controllButton = button;
            PopupOverlay.IsVisible = true;
        }

    }
    private void OnPopupOverlayPointerPressed(object sender, PointerPressedEventArgs e)
    {
        // Close the popup when clicking outside of it
        //controllButton.Foreground = Avalonia.Media.Brushes.Black;
        var StackPanel = controllButton.Content as StackPanel;
        var Label = StackPanel.Children;
        if (Label[1] is TextBlock targetText)
        {
            //number += button.Content?.ToString();
            targetText.Text = oldNumber;
        }
        tempErrorMsg.Text = "";
        heatingBtn.IsEnabled = true;
        coolingBtn.IsEnabled = true;
        pouringBtn.IsEnabled = true;

        PopupOverlay.IsVisible = false;
    }

    private void setDefaultSettings()
    {
        //set Values
        _recipeTable = _recipeTable.ReadRecipesById(_recipeTable.Id.ToString());
        heatingValue.Text = _recipeTable?.HeatingGoal == 0 ? "" : _recipeTable?.HeatingGoal.ToString(); 
        coolingValue.Text = _recipeTable?.CoolingGoal == 0 ? "" : _recipeTable?.CoolingGoal.ToString();
        pouringValue.Text = _recipeTable?.PouringGoal == 0 ? "" : _recipeTable?.PouringGoal.ToString();

        //Set Track Up
        _mainWindow.HomeTrack.IsVisible = true;
        //_mainWindow.HomePolygon.Stroke = Avalonia.Media.Brushes.Black;
        _mainWindow.RecipePanelTrack.IsVisible = true;
        _mainWindow.RecipePanelPolygon.Stroke = Avalonia.Media.Brushes.Black;
        _mainWindow.RecipeEditTrack.IsVisible = true;
        _mainWindow.RecipeEditPolygon.Stroke = Brush.Parse("#A4275D");
        _mainWindow.RunInterfaceTrack.IsVisible = false;
        _mainWindow.RecipeSelTrack.IsVisible = false;
        _mainWindow.SettingTrack.IsVisible = false;
        _mainWindow.DiagnosticsTrack.IsVisible = false;

        _mainWindow.TitleBtn.IsVisible = true;
        _mainWindow.Title.Text = _recipeTable.Name;

        //Set Footer
        _mainWindow.footerMsg.Text = "Select the tempereture to edit it";
        _mainWindow.footer.Background = Avalonia.Media.Brushes.WhiteSmoke;
        _mainWindow.footerMsg.Foreground = Brush.Parse("#A4275D");
        _mainWindow.footerDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        _mainWindow.footerTime.Text = DateTime.Now.ToString("hh:mm tt");
        _mainWindow.footerDateContainer.IsVisible = true;
        _mainWindow.footerStartBtn.IsVisible = false;
        _mainWindow.adminBtns.IsVisible = false;
    }
}