using Caliburn.Micro;
using Gremlin_BlazorServer.Data.EntityClasses;
using Gremlin_BlazorServer.Utilities;
using System;
using System.Diagnostics.Contracts;
using System.Windows.Data;

namespace Gremlin_BlazorServer.MVVM.ViewModels
{
	public class ContactViewModel : PropertyChangedBase
	{
		private Enums.Gender gender = Enums.Gender.Male;
		private string firstName = "firstName";
		private string lastName = "lastName";
		private string eMail = "email@email.de";
		private string accountName = "accountName";
		private string accountStreet = "accountStreet";
		private uint accountZIP;
		private string accountCity = "accountCity";
		private string accountTypeCode;

		public uint ContactID { get; set; }
		public Enums.Gender Gender { get => gender; set { gender = value; NotifyOfPropertyChange(() => Gender); } }
		public string FirstName { get => firstName; set { firstName = value; NotifyOfPropertyChange(() => FirstName); } }
		public string LastName { get => lastName; set { lastName = value; NotifyOfPropertyChange(() => LastName); } }
		public string EMail { get => eMail; set { eMail = value; NotifyOfPropertyChange(() => EMail); } }
		public string AccountName { get => accountName; set { accountName = value; NotifyOfPropertyChange(() => AccountName); } }
		public string AccountStreet { get => accountStreet; set { accountStreet = value; NotifyOfPropertyChange(() => AccountStreet); } }
		public uint AccountZIP { get => accountZIP; set { accountZIP = value; NotifyOfPropertyChange(() => AccountZIP); } }
		public string AccountCity { get => accountCity; set { accountCity = value; NotifyOfPropertyChange(() => AccountCity); } }
		public string AccountTypeCode { get => accountTypeCode; set { accountTypeCode = value; NotifyOfPropertyChange(() => AccountTypeCode); } }

		public ContactViewModel() { }

		public ContactViewModel(Enums.Gender gender, string firstName, string lastName, string eMail, string accountName, string accountStreet, uint accountZIP, string accountCity, string accountTypeCode)
		{
			Gender = gender;
			FirstName = firstName ?? throw new ArgumentNullException(nameof(firstName));
			LastName = lastName ?? throw new ArgumentNullException(nameof(lastName));
			EMail = eMail ?? throw new ArgumentNullException(nameof(eMail));
			AccountName = accountName ?? throw new ArgumentNullException(nameof(accountName));
			AccountStreet = accountStreet ?? throw new ArgumentNullException(nameof(accountStreet));
			AccountZIP = accountZIP;
			AccountCity = accountCity ?? throw new ArgumentNullException(nameof(accountCity));
			AccountTypeCode = accountTypeCode;
		}

		public override string ToString()
		{
			return Gender == Enums.Gender.Male
				? $"Herr {FirstName} {LastName}; {AccountName}, {AccountStreet}, {AccountZIP} {AccountCity}"
				: $"Frau {FirstName} {LastName}; {AccountName}, {AccountStreet}, {AccountZIP} {AccountCity}";
		}

		internal static Predicate<object> SearchContact(ListCollectionView listCollectionContacts, string searchString)
		{
			Contract.Requires(listCollectionContacts != null);

			return listCollectionContacts.Filter = (c) => {
				ContactViewModel contactVM = c as ContactViewModel;
				return contactVM.LastName.ToLower().Contains(searchString.ToLower())
					|| contactVM.FirstName.ToLower().Contains(searchString.ToLower())
					|| contactVM.AccountName.ToLower().Contains(searchString.ToLower())
					|| contactVM.AccountCity.ToLower().Contains(searchString.ToLower());
			};
		}

		internal static string FilterContacts(object selectedItem)
		{
			return selectedItem != null
				? selectedItem == CollectionView.NewItemPlaceholder
					? "Bitte neuen Kontakt erstellen"
					: TexHandler.CreateBriefkopf(selectedItem as ContactViewModel).ToString()
				: "Kein Kontakt gefunden!";
		}

		internal static ContactViewModel GetSalesRepAsContact(int SalesRepCode)
		{
			switch (SalesRepCode)
			{
				case 1:
					{
						ContactViewModel salesRepWoitschetzki = new(Enums.Gender.Male,
											"Sascha",
											"Woitschetzki",
											"sascha.woitschetzki@non.agilent.com",
											"Handelsvertretung Sascha Woitschetzki",
											"Parsevalstr. 58",
											45470,
											"Mülheim an der Ruhr",
											"SUP");
						return salesRepWoitschetzki;
					}

				case 2:
					{
						ContactViewModel salesRepWelsch = new(Enums.Gender.Male,
													"Sebastian",
													"Welsch",
													"sebastian.welsch@non.agilent.com",
													"Handelsvertretung Sebastian Welsch",
													"",
													0,
													"",
													"SUP");
						return salesRepWelsch;
					}

				default:
					{
						ContactViewModel salesRep = new();
						return salesRep;
					}
			}
		}
	}
}