using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using demko6.Models;
using Microsoft.EntityFrameworkCore;

namespace demko6;

public partial class FormOrderWindow : Window
{
    private List<Client> clients;
    private List<Service> services;
    private int _employeeId;
    
    public FormOrderWindow(int EmployeeId)
    {
        InitializeComponent();
        using var context = new DemkoNejykovContext();
        
        _employeeId = EmployeeId;
        
        clients = context.Clients
            .Include(c => c.CodeFClientNavigation)
            .Include(c => c.CodeUrClientNavigation)
            .ToList();
        
        var clientNames = clients.Select(c => 
            c.UrOrNo ? c.CodeUrClientNavigation?.Name : c.CodeFClientNavigation?.Name
        ).ToList();
        
        OrderClientComboBox.ItemsSource = clientNames;
        
        services = context.Services.ToList();
        OrderServicesListBox.ItemsSource = services.Select(s => s.Name).ToList();
    }
    
    private void FormOrderButton_OnClick(object? sender, RoutedEventArgs e)
    {
        using var context = new DemkoNejykovContext();

        if (OrderClientComboBox.SelectedIndex == -1)
        {
            ErrorTextBlock.Text = "Выберите услуги или клиента";
            ErrorTextBlock.IsVisible = true;
            return;
        }

        if (OrderServicesListBox.SelectedItems == null || OrderServicesListBox.SelectedItems.Count == 0)
        {
            ErrorTextBlock.Text = "Выберите услуги или клиента";
            ErrorTextBlock.IsVisible = true;
            return;
        }

        var selectedClientIndex = OrderClientComboBox.SelectedIndex;
        var selectedClient = clients[selectedClientIndex];

        var selectedServiceNames = OrderServicesListBox.SelectedItems.Cast<string>().ToList();
        var selectedServices = services.Where(s => selectedServiceNames.Contains(s.Name)).ToList();

        var dateNow = DateTime.Now;
        int maxIdOrder = context.Orders.Any() ? context.Orders.Max(o => o.Id) : 0;
        int newIdOrder = maxIdOrder + 1;
        
        var newOrder = new Order
        {
            Id = newIdOrder,
            OrderNumber = selectedClient.ClientCode + "/" + dateNow.ToString("yyyyMMdd"),
            CreatedAt = dateNow,
            ClientCode = selectedClient.ClientCode,
            Status = "Новая",
            Orederclosetime = null,
            Prokattime = 1,
            EmployeeId = _employeeId
        };
        
        context.Orders.Add(newOrder);
        context.SaveChanges();
        
        foreach (var service in selectedServices)
        {
            var orderService = new OrderService
            {
                OrderId = newOrder.Id,
                ServiceId = service.Id
            };
            context.OrderServices.Add(orderService);
        }
        
        context.SaveChanges();
        
        ErrorTextBlock.Text = "Заказ создан";
        ErrorTextBlock.IsVisible = true;
    }
    
    
    private void CreateClientButton_OnClick(object? sender, RoutedEventArgs e)
    {
        ChoiceClientWindow choiceClientWindow = new ChoiceClientWindow();
        choiceClientWindow.ShowDialog(this);
    }
}