using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml;
using demo0411.Models;
using Metsys.Bson;
using System.Linq;
using System;

namespace demo0411;

public partial class DobavRedactProduct : Window
{
    private int idArticl;
    private int typeProduct;
    private bool redact = false;
    public DobavRedactProduct()
    {
        InitializeComponent();
        TypeProd.ItemsSource = Helper.DbContext.ProductTypes.Select(a => a.Name).ToList();
        GeneretArticl();
        Art.Text = Convert.ToString(idArticl);
    }
    public DobavRedactProduct(int id)
    {
        InitializeComponent();
        TypeProd.ItemsSource = Helper.DbContext.ProductTypes.Select(a => a.Name).ToList();
        idArticl = id;
        Product product = new Product();
        product = Helper.DbContext.Products.ToList().FirstOrDefault(s => s.Id == idArticl);
        NameProduct.Text = product.Name;
        typeProduct = (int)product.TypeId;
        if (typeProduct == 4)
        {
            typeProduct = 3;
        }
        typeProduct--;
        MinCostProduct.Text = Convert.ToString(product.MinCost);
        Art.Text = Convert.ToString(idArticl);
        Art.IsEnabled = false;
        TypeProd.SelectedIndex = typeProduct;
        redact = true;
    }

    private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        new ProductList().Show();
        Close();
    }

    private void Button_Click_1(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        if (redact == false)
        {
            if (Helper.DbContext.Products.ToList().Where(t => t.Name == NameProduct.Text).Count() != 0)
            {
                var dialogWindow = new Oshibka3();
                dialogWindow.ShowDialog(this);
                return;
            }
            Product product = new Product();
            product.Id = idArticl;
            product.TypeId = typeProduct;
            product.Name = NameProduct.Text;
            product.MinCost = float.Parse(MinCostProduct.Text);
            Helper.DbContext.Products.Add(product);
            Helper.DbContext.SaveChanges();
        }
        else
        {
            foreach (var part in Helper.DbContext.Partners)
            {
                if (NameProduct.Text == part.Name && idArticl != part.Id)
                {
                    var dialogWindow = new Oshibka3();
                    dialogWindow.ShowDialog(this);
                    return;
                }
            }
            Product product = new Product();
            product = Helper.DbContext.Products.ToList().FirstOrDefault(s => s.Id == idArticl);
            product.TypeId = typeProduct;
            product.Name = NameProduct.Text;
            product.MinCost = float.Parse(MinCostProduct.Text);
            Helper.DbContext.Products.Update(product);
            Helper.DbContext.SaveChanges();
        }
        new ProductList().Show();
        Close();
    }
    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {

        TextBox textBox = (TextBox)sender;
        string text = textBox.Text;

        if (text.Any(c => !char.IsDigit(c)))
        {
            OshibkaWindow();
            textBox.Text = new string(text.Where(c => char.IsDigit(c)).ToArray());
        }
    }

    private void OshibkaWindow()
    {
        var dialogWindow = new Oshibka();
        dialogWindow.ShowDialog(this);
    }


    private void ComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
    {
        typeProduct = TypeProd.SelectedIndex;
        typeProduct++;
        if (typeProduct == 4)
        {
            typeProduct = 1;
        }
    }
    private void GeneretArticl()
    {
        Random random = new Random();
        idArticl = random.Next(1000000, 9999999);
        foreach (var products in Helper.DbContext.Products)
        {
            if (products.Id == idArticl)
            {
                GeneretArticl();
            }
        }
    }
}