using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using InventoryTraker.Web.Core;

namespace InventoryTraker.Web.Utilities
{
	public static class ModelHelper
	{
		public static List<Transaction> GetAddedTransaction(DateTime addedDate)
		{
			return
				new List<Transaction>
				{
					new Transaction
					{
						TransactionType = TransactionType.Added,
						AddedQuantity = 1,
						CurrentQuantity = 1,
						Memo = "Added",
						Timestamp = DateTime.Now,
						TransactionDate = addedDate
					}
				};
		}

		public static string GetUpdateExceptionMessage(Exception e)
		{
			if (e.InnerException?.InnerException is SqlException)
			{
				var innerMessage = e.InnerException.InnerException.Message;
				if (innerMessage.StartsWith(@"Violation of PRIMARY KEY constraint 'PK_dbo.Inventories'."))
				{
					var id = Regex.Replace(innerMessage, @".*\((.*)\).*", "$1", RegexOptions.Singleline);
					return "Duplicate Inventory Id: " + id;
				}
				return innerMessage;
			}
			return e.InnerException?.Message ?? e.Message;
		}
	}
}