using Gremlin_BlazorServer.GremlinData.EntityClasses;
using Gremlin_BlazorServer.MVVM.ViewModels;
using System.Windows.Data;

namespace Gremlin_BlazorServer.MVVM.Models
{
	public class ContactModel
	{
		public static ContactViewModel ConvertContactToContactVM(Contact contact)
		{
			if (contact.Account != null)
			{
				ContactViewModel contactVM = new()
				{
					ContactID = contact.ContactId,
					Gender = (Enums.Gender)contact.Gender,
					LastName = contact.LastName,
					FirstName = contact.FirstName,
					EMail = contact.EMail,
					AccountName = contact.Account.AccountName,
					AccountStreet = contact.Account.Street,
					AccountZIP = contact.Account.ZIP,
					AccountCity = contact.Account.City,
					AccountTypeCode = contact.Account.AccountType.AccountTypeCode
				};
				return contactVM;
			}
			else
			{
				ErrorHandler.ShowErrorMessage($"Kontakt {contact.LastName} hat keinen Account!");
				return null;
			}
		}

		public static ContactViewModel ConvertObjectToContactVM(object selectedItem)
		{
			ContactViewModel selectedContact = new();

			if (selectedItem != CollectionView.NewItemPlaceholder)
			{
				//TryCast
				selectedContact = selectedItem as ContactViewModel;

				//if failed == null
				if (selectedContact == null)
				{
					return default;
				}
			}

			return selectedContact;
		}
	}
}
