using Gremlin.GremlinData.EntityClasses;
using Gremlin.GremlinUI.ViewModels;
using Gremlin.Operations;
using Gremlin.ViewModels;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;

namespace Gremlin.GremlinUI
{
    public partial class QuoteUI : Window
    {
        private static ListCollectionView listCollectionContacts = new(new ObservableCollection<Contact>());
        private static ListCollectionView lineItems = new(new ObservableCollection<LineItemVM>());
        private QuoteVM quoteVM = new();
        private readonly int salesRepCode = 1;

        public QuoteUI()
        {
            Random random = new();

            InitializeComponent();
            if (salesRepCode == 1)
                tbQuoteNumber.Text = $"DE-83PE89-{DateTime.Now:My}-{random.Next(999999)}";
            else if (salesRepCode == 2)
                tbQuoteNumber.Text = $"DE-83RE32-{DateTime.Now:My}-{random.Next(999999)}";

            LoadContacts();
        }

        private void UpdateUI()
        {
            dgLineItems.ItemsSource = lineItems;
        }

        private void LoadContacts()
        {
            listCollectionContacts = new(ContactVM.GetContacts());
            listCollectionContacts.Filter = ContactVM.SearchContact(listCollectionContacts, tbContactSearch.Text);
            dgFoundContacts.ItemsSource = listCollectionContacts;
            UpdateUI();
        }

        private void TbContactSearch_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            listCollectionContacts.Filter = ContactVM.SearchContact(listCollectionContacts, tbContactSearch.Text);
        }

        private void DgFoundContacts_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            quoteVM.Recipient = ContactVM.ConvertObjectToContactVM(dgFoundContacts.SelectedItem);
        }

        private void ButPasteQuote_Click(object sender, RoutedEventArgs e)
        {
            float vat = 19f;
            int warranty = int.TryParse(tbWarranty.Text, out int defaultWarranty) ? defaultWarranty : 12;
            int validity = int.TryParse(tbValidity.Text, out int defaultValidity) ? defaultValidity : 60;

            quoteVM = QuoteVM.CreateQuote(tbQuoteNumber.Text,
                                            ContactVM.ConvertObjectToContactVM(dgFoundContacts.SelectedItem),
                                            ContactVM.GetSalesRepAsContact(salesRepCode),
                                            vat,
                                            tbQuoteType.Text,
                                            cbBrutto.IsChecked,
                                            warranty,
                                            cbSinglePrices.IsChecked,
                                            cbBrochure.IsChecked,
                                            cbDataSheets.IsChecked,
                                            cbMailTemplate.IsChecked,
                                            tbQuotePath.Text,
                                            validity
                                            );

            ObservableCollection<LineItemVM> lineItemsViewModel = new(quoteVM.LineItemsVM);
            lineItems = new(lineItemsViewModel);
            UpdateUI();
        }

        private void ButCreateTex_Click(object sender, RoutedEventArgs e)
        {
            if (quoteVM.LineItemsVM == null || quoteVM.LineItemsVM.Count == 0)
            {
                ErrorHandler.ShowMessageBox("Bitte zuerst ein Angebot einfügen");
            }
            else
            {
                TexHandler.CreateTexAndOpen(quoteVM);
            }
        }

        private void ButCreateQuote_Click(object sender, RoutedEventArgs e)
        {
            PDFHandler.CreatePDF(quoteVM.QuoteNumber, tbQuotePath.Text);
        }

        private void ButSendQuote_Click(object sender, RoutedEventArgs e)
        {
            //FileIO.SendQuoteViaMail(quoteVM);
        }

        private void ButReloadContacts_Click(object sender, RoutedEventArgs e)
        {
            LoadContacts();
            _ = MessageBox.Show($"Es wurden {listCollectionContacts.Count} Kontakte geladen.");
        }

        private void TbQuoteType_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            quoteVM.QuoteType = tbQuoteType.Text;
        }

        private void TbQuotePath(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            quoteVM.QuotePath = tbQuotePath.Text;
        }

        private void TbQuoteNumber_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            quoteVM.QuoteNumber = tbQuoteNumber.Text;
        }

        private void TbWarranty_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            quoteVM.Warranty = int.TryParse(tbWarranty.Text, out int defaultWarranty) ? defaultWarranty : 12;
        }

        private void TbValidity_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            quoteVM.Validity = int.TryParse(tbValidity.Text, out int defaultValidity) ? defaultValidity : 60;
        }

        private void CbBrutto_Click(object sender, RoutedEventArgs e)
        {
            quoteVM.Brutto = cbBrutto.IsChecked;
        }

        private void CbSinglePrices_Click(object sender, RoutedEventArgs e)
        {
            quoteVM.SinglePrices = cbSinglePrices.IsChecked;
        }

        private void CbBrochure_Click(object sender, RoutedEventArgs e)
        {
            quoteVM.Brochures = cbBrochure.IsChecked;
        }

        private void CbDataSheets_Click(object sender, RoutedEventArgs e)
        {
            quoteVM.DataSheets = cbDataSheets.IsChecked;
        }

        private void CbMailTemplate_Click(object sender, RoutedEventArgs e)
        {
            quoteVM.MailTemplate = cbMailTemplate.IsChecked;
        }
    }
}