using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media.TextFormatting.Unicode;
using demo0411.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Tmds.DBus.Protocol;

namespace demo0411;

public partial class DobavPartner : Window
{
    private List<PartnerType> partnerTypes;
    private int index = 0;
    private int zapusk;
    private int type;
    private bool redact;
    private int typeName;
    public DobavPartner()
    {
        InitializeComponent();
        partnerTypes = Helper.DbContext.PartnerTypes.ToList();
        PartType.ItemsSource = Helper.DbContext.PartnerTypes.Select(s => s.Name).ToList();
        zapusk = 0;
    }
    public DobavPartner(int ind, int stop)
    {
        zapusk = stop;
        InitializeComponent();
        zapusk = stop;
        Partner partner = new Partner();
        partnerTypes = Helper.DbContext.PartnerTypes.ToList();
        partner = Helper.DbContext.Partners.ToList().FirstOrDefault(s => s.Id == ind);
        Name.Text = partner.Name;
        Adress.Text = partner.Addres;
        FIO.Text = partner.Director;
        inn.Text = Convert.ToString(partner.Inn);
        Telephone.Text = partner.Phone;
        Email.Text = partner.Email;
        Reiting.Text = Convert.ToString(partner.Raiting);
        PartType.ItemsSource = Helper.DbContext.PartnerTypes.Select(s => s.Name).ToList();
        foreach (var partnertype in partnerTypes)
        {
            if (partnertype.Id == partner.PartnerTypeId)
            {
                typeName = partnertype.Id;
                typeName--;
                break;
            }
        }
        PartType.SelectedIndex = typeName;
        index = ind;
        redact = true;
    }

    private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        if (!String.IsNullOrEmpty(Name.Text) && !String.IsNullOrEmpty(Reiting.Text) && !String.IsNullOrEmpty(Adress.Text) && !String.IsNullOrEmpty(FIO.Text) && !String.IsNullOrEmpty(Email.Text) && !String.IsNullOrEmpty(Telephone.Text) && !String.IsNullOrEmpty(inn.Text))
        {
            if (redact)
            {
                Partner partner = Helper.DbContext.Partners.FirstOrDefault(s => s.Id == index);
                foreach (var part in Helper.DbContext.Partners)
                {
                    if (Name.Text == part.Name && index != part.Id)
                    {
                        var dialogWindow = new Oshibka3();
                        dialogWindow.ShowDialog(this);
                        return;
                    }
                }
                partner.Name = Name.Text;
                partner.PartnerTypeId = type;
                partner.Director = FIO.Text;
                partner.Addres = Adress.Text;
                partner.Inn = inn.Text;
                partner.Phone = Telephone.Text;
                partner.Email = Email.Text;
                partner.Raiting = int.Parse(Reiting.Text);
                Helper.DbContext.Partners.Update(partner);
                Helper.DbContext.SaveChanges();
            }
            else
            {
                if (Helper.DbContext.Partners.ToList().Where(t => t.Name == Name.Text).Count() != 0)
                {
                    var dialogWindow = new Oshibka3();
                    dialogWindow.ShowDialog(this);
                    return;
                }
                index = Helper.DbContext.Partners.Count();
                index++;
                Partner partner = new Partner() { Id = index, Director = FIO.Text, Name = Name.Text, PartnerTypeId = type, Addres = Adress.Text, Inn = inn.Text, Phone = Telephone.Text, Email = Email.Text, Raiting = int.Parse(Reiting.Text) };
                Helper.DbContext.Partners.Add(partner);
                Helper.DbContext.SaveChanges();
            }
            new ListPartner().Show();
            this.Close();
        }
        else if (String.IsNullOrEmpty(Name.Text) || String.IsNullOrEmpty(Reiting.Text) || String.IsNullOrEmpty(Adress.Text) || String.IsNullOrEmpty(FIO.Text) || String.IsNullOrEmpty(Email.Text) || String.IsNullOrEmpty(Telephone.Text) || String.IsNullOrEmpty(inn.Text))
        {
            var dialogWindow = new Oshibka2();
            dialogWindow.ShowDialog(this);
        }
    }

    private void ComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
    {
        type = PartType.SelectedIndex;
        type++;
    }

    private void Button_Click_1(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        new ListPartner().Show();
        Close();
    }
    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (zapusk == 0)
        {
            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());
            }
        }
        else { zapusk--; }
    }
    private void OshibkaWindow()
    {
        var dialogWindow = new Oshibka();
        dialogWindow.ShowDialog(this);
    }
}