using Caliburn.Micro;
using Gremlin.GremlinData.DBClasses;
using Gremlin.GremlinData.EntityClasses;
using Gremlin.GremlinUtilities;
using Gremlin.MVVM.Models;
using Gremlin.MVVM.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Gremlin.MVVM
{
	public class ShellViewModel : PropertyChangedBase
	{
		private ContactViewModel selectedContact;
		private ContactViewModel contactVM;
		private ObservableCollection<ContactViewModel> contacts;
		private QuoteViewModel quoteVM;
		private string searchContact;
		private bool quoteReady;
		private bool texCreated;
		private bool pdfCreated;
		private bool pdfOpened;
		private bool wroteToDB;

		public string SearchContact { get => searchContact; set { searchContact = value; NotifyOfPropertyChange(() => SearchContact); Contacts = GetContactsVM(searchContact); } }
		public ContactViewModel ContactVM { get => contactVM; set { contactVM = value; NotifyOfPropertyChange(() => ContactVM); } }
		public QuoteViewModel QuoteVM { get => quoteVM; set { quoteVM = value; NotifyOfPropertyChange(() => QuoteVM); QuoteReady = CheckQuoteReady(); } }
		public ContactViewModel SelectedContact { get => selectedContact; set { selectedContact = value; NotifyOfPropertyChange(() => SelectedContact); QuoteVM.Recipient = value; NotifyOfPropertyChange(() => QuoteVM.Recipient); QuoteReady = CheckQuoteReady(); } }
		public ObservableCollection<ContactViewModel> Contacts { get => contacts; set { contacts = value; NotifyOfPropertyChange(() => Contacts); } }
		public bool QuoteReady { get => quoteReady; set { quoteReady = value; NotifyOfPropertyChange(() => QuoteReady); } }
		public bool TexCreated { get => texCreated; set { texCreated = value; NotifyOfPropertyChange(() => TexCreated); } }
		public bool PdfCreated { get => pdfCreated; set { pdfCreated = value; NotifyOfPropertyChange(() => PdfCreated); } }
		public bool PdfOpened { get => pdfOpened; set { pdfOpened = value; NotifyOfPropertyChange(() => PdfOpened); } }
		public bool WroteToDB { get => wroteToDB; set { wroteToDB = value; NotifyOfPropertyChange(() => WroteToDB); } }


		public ShellViewModel()
		{
			QuoteVM = new();
			Contacts = GetContactsVM("");
		}

		private bool CheckQuoteReady()
		{
			return QuoteVM.LineItemsVM.Count != 0 && QuoteVM.Recipient.LastName != "lastName";
		}

		internal static ObservableCollection<ContactViewModel> GetContactsVM(string searchContact)
		{
			try
			{
				using (GremlinContext gremlinContext = new())
				{
					List<Contact> contacts = searchContact is "" or null

						? gremlinContext.Contacts.Include(contact => contact.Account)
						.Include(contact => contact.Account.AccountType)
						.ToList()

						: gremlinContext.Contacts
						.Where(contact => contact.LastName.Contains(searchContact))
						.Include(contact => contact.Account)
						.Include(contact => contact.Account.AccountType).ToList();

					ObservableCollection<ContactViewModel> contactsVM = new();

					foreach (Contact contact in contacts)
					{
						contactsVM.Add(ContactModel.ConvertContactToContactVM(contact));
					}

					return contactsVM;
				}
			}
			catch (Exception ex)
			{
				ErrorHandler.ShowErrorInMessageBox(ex);
				return null;
				throw;
			}
		}

		public void CreateTex()
		{
			if (QuoteReady)
			{
				WroteToDB = QuoteModel.WriteToDB(QuoteVM);
				TexCreated = TexHandler.CreateTex(QuoteVM);
				if (TexCreated) { PdfCreated = PDFHandler.CreatePDF(QuoteVM); }
				if (!PdfOpened & PdfCreated) { PdfOpened = PDFHandler.OpenPDF(QuoteVM); }
			}
		}

		public void GenerateNewQuote()
		{
			QuoteVM = new();
		}

		public void ReadLineItems()
		{
			QuoteReady = QuoteVM.ReadLineItems(QuoteVM);
		}

		//public void CreatePDF()
		//{
		//	PDFCreated = PDFHandler.CreatePDF(QuoteVM);
		//}

		//public void OpenPDF()
		//{
		//	PDFHandler.OpenPDF(QuoteVM);
		//}

		//public void WriteToDB()
		//{
		//	if (QuoteModel.WriteToDB(QuoteVM))
		//	{
		//		ErrorHandler.ShowInfoMessage($"Wrote quote {quoteVM.QuoteNumber} successfully to DB.");
		//	}
		//}
	}
}
