using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using DemoService.Models;
using System.Collections.Generic;
using System.Linq;

namespace DemoService;

public partial class ServiceWindow : Window
{
    private readonly bool AdminMode;
    private bool? SortedMode;
    private List<Service> displayList = Utils.Context.Services;
    public ServiceWindow()
    {
        InitializeComponent();
        ServicesListBox.ItemsSource = displayList;
    }
    public ServiceWindow(bool admin)
    {
        InitializeComponent();
        AdminMode = admin;
        if (!admin)
        {
            AddButton.IsVisible = false;
        }
        ServicesListBox.ItemsSource = displayList;
        foreach (var service in ServicesListBox.ItemsSource)
        {

        }
    }
    public void AddButtonClick(object sender, RoutedEventArgs args)
    {
        AddEditWindow addEditWindow = new();
        addEditWindow.Show();
        Close();
    }
    private decimal? CostAfterDiscount(decimal previousCost, decimal? discount)
    {
        if (discount.HasValue || discount != 0)
        {
            decimal actualCost = previousCost - previousCost * discount.Value;
            return actualCost;
        }
        return null;
    }

    private void AddButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        AddEditWindow addEditWindow = new();
        addEditWindow.Show();
        Close();
    }

    private void SortComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
    {
        int selectedIndex = (sender as ComboBox).SelectedIndex;
        switch (selectedIndex)
        {
            case 0: SortedMode = false; break;
            case 1: SortedMode = true; break;
            default: SortedMode = null; break;
        }
        DisplayService();
    }
    private void DisplayService()
    {
        switch (SortedMode)
        {
            case true:
                displayList = displayList.OrderBy(service => service.Cost).ToList();
                break;
            case false:
                displayList = displayList.OrderByDescending(service => service.Cost).ToList();
                break;
        }
        ServicesListBox.ItemsSource = displayList.ToList();
    }
}