using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using DemoService.Models;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media.TextFormatting;
using Avalonia.Media.Imaging;
using System;
using DemoService.Context;

namespace DemoService;

public partial class ServiceWindow : Window
{
    private bool AdminMode;
    private List<Service> displayList = Utils.Context.Services;
    public ServiceWindow()
    {
        InitializeComponent();
        ServicesListBox.ItemsSource = displayList;
        UserServicesListBox.ItemsSource = displayList;
        ServicesListBox.Background = Colors.lightYellow;
        UserServicesListBox.Background = Colors.lightYellow;
        SortComboBox.SelectedIndex = 0;
        FilterComboBox.SelectedIndex = 0;
        Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
    }
    public ServiceWindow(bool AdminMode)
    {
        InitializeComponent();
        this.AdminMode = AdminMode;
        switch (this.AdminMode)
        {
            case true:
                ServicesListBox.ItemsSource = displayList;
                UserServicesListBox.IsVisible = false;
                ServicesListBox.Background = Colors.lightYellow;
                break;
            case false:
                AddButton.IsVisible = false;
                UserServicesListBox.ItemsSource = displayList;
                ServicesListBox.IsVisible = false;
                UserServicesListBox.Background = Colors.lightYellow;
                break;
        }
        SortComboBox.SelectedIndex = 0;
        FilterComboBox.SelectedIndex = 0;
        Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
    }
    public void AddButtonClick(object sender, RoutedEventArgs args)
    {
        AddEditWindow addEditWindow = new(AdminMode);
        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, RoutedEventArgs e)
    {
        AddEditWindow addEditWindow = new(AdminMode);
        addEditWindow.Show();
        Close();
    }
    private void SortComboBox_SelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        DisplayService();
    }
    private void SearchTextBox_KeyUp(object? sender, KeyEventArgs e)
    {
        DisplayService();
    }
    private void FilterComboBox_SelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        DisplayService();
    }
    private void DisplayService()
    {
        displayList = Utils.Context.Services;
        switch (SortComboBox.SelectedIndex)
        {
            case 0:
                break;
            case 1:
                displayList = displayList.OrderBy(service => service.Cost).ToList();
                break;
            case 2:
                displayList = displayList.OrderByDescending(service => service.Cost).ToList();
                break;
        }
        switch (FilterComboBox.SelectedIndex)
        {
            case 0:
                break;
            case 1:
                displayList = displayList.Where(x => x.Discount >= 0 && x.Discount < (decimal)0.05).ToList();
                break;
            case 2:
                displayList = displayList.Where(x => x.Discount >= (decimal)0.05 && x.Discount < (decimal)0.15).ToList();
                break;
            case 3:
                displayList = displayList.Where(x => x.Discount >= (decimal)0.15 && x.Discount < (decimal)0.30).ToList();
                break;
            case 4:
                displayList = displayList.Where(x => x.Discount >= (decimal)0.30 && x.Discount < (decimal)0.70).ToList();
                break;
            case 5:
                displayList = displayList.Where(x => x.Discount >= (decimal)0.70 && x.Discount < (decimal)1.00).ToList();
                break;
        }
        if (!string.IsNullOrEmpty(SearchTextBox.Text))
        {
            displayList = displayList.Where(x => x.Title.ToLower().Contains(SearchTextBox.Text.ToLower())).ToList();
        }
        ServicesListBox.ItemsSource = displayList;
    }

    private void DeleteButton_Click(object? sender, RoutedEventArgs e)
    {
        using (var context = new OvsyannikovContext())
        {
            var service = context.Services.Find((int)(sender as Button).Tag);
            context.Remove(service);
            context.SaveChanges();
        }
        Utils.Context.Services = new List<Service>(Utils.Context.DbContext.Services);
        displayList = Utils.Context.Services;
        switch (AdminMode)
        {
            case true:
                ServicesListBox.ItemsSource = displayList;
                break;
            case false:
                UserServicesListBox.ItemsSource = displayList;
                break;
        }
    }

    private void EditButton_Click(object? sender, RoutedEventArgs e)
    {
        AddEditWindow addEditWindow = new(AdminMode, (int)(sender as Button).Tag);
        addEditWindow.Show();
        Close();
    }
}