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

namespace Gremlin.MVVM
{
    public partial class QuoteUI : Window
    {
        private static ListCollectionView listCollectionContacts = new(new ObservableCollection<Contact>());
        private static ListCollectionView lineItems = new(new ObservableCollection<LineItemViewModel>());
        private QuoteViewModel quoteVM = new(ContactViewModel.GetSalesRepAsContact(1));

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

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

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

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

        private void DgFoundContacts_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            quoteVM.Recipient = ContactViewModel.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 = QuoteViewModel.CreateQuote(tbQuoteNumber.Text,
                                            ContactViewModel.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<LineItemViewModel> lineItemsViewModel = new(quoteVM.LineItemsViewModel);
            lineItems = new(lineItemsViewModel);
            UpdateUI();
        }

        private void BtnCreateTex_Click(object sender, RoutedEventArgs e)
        {
            if (quoteVM.LineItemsViewModel == null || quoteVM.LineItemsViewModel.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.ShowBrutto = cbBrutto.IsChecked;
        }

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

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

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

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

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