using Gremlin.GremlinData.EntityClasses;
using Gremlin.GremlinUI.ViewModels;
using Gremlin.Operations;
using Gremlin.ViewModels;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
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(ContactVM.GetSalesRepAsContact(1));

        public QuoteUI()
        {
            InitializeComponent();
            LoadContacts();
        }

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

        private void LoadContacts()
        {
            listCollectionContacts = new(ContactVM.GetAllContactsVM());
            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 BtnPasteQuote_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),
                                            quoteVM.SalesRep,
                                            vat,
                                            tbQuoteType.Text,
                                            cbBrutto.IsChecked,
                                            warranty,
                                            cbSinglePrices.IsChecked,
                                            cbBrochure.IsChecked,
                                            cbDataSheets.IsChecked,
                                            cbMailTemplate.IsChecked,
                                            tbQuotePath.Text,
                                            validity,
                                            cbShowDiscounts.IsChecked
                                            );

            if (quoteVM == null) return;

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

        private void BtnCreateTex_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 BtnCreateQuote_Click(object sender, RoutedEventArgs e)
        {
            PDFHandler.CreatePDF(quoteVM.QuoteNumber, tbQuotePath.Text);
        }

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

        private void BtnReloadContacts_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.OnPropertyChanged("QuoteType");
        }

        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;
        }

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