using CarServiceContracts.BusinessLogicsContracts;

namespace CarServiceBusinessLogic.BusinessLogics
{
	/// <summary>
	/// Заглушка для роли клиента
	/// </summary>
	public class CustomerRoleImitationLogic
	{
		private readonly ICustomerLogic _customerLogic;
		private readonly IVehicleLogic _vehicleLogic;
		private readonly IRepairRequestLogic _repairRequestLogic;
		private readonly IWorkInRequestLogic _workInRequestLogic;
		private readonly IWorkLogic _workLogic;
		public CustomerRoleImitationLogic(ICustomerLogic customerLogic, IVehicleLogic vehicleLogic, IRepairRequestLogic repairRequestLogic, IWorkInRequestLogic workInRequestLogic, IWorkLogic workLogic)
		{
			_customerLogic = customerLogic;
			_vehicleLogic = vehicleLogic;
			_repairRequestLogic = repairRequestLogic;
			_workInRequestLogic = workInRequestLogic;
			_workLogic = workLogic;
		}
		private bool GenerateCustomerData()
		{
			if (_customerLogic.ReadList(null)?.Count != 0)
			{
				return false;
			}
			using (StreamReader sr = new("customers.txt"))
			{
				string? currentString;
				while ((currentString = sr.ReadLine()) != null)
				{
					var customerRecData = currentString.Split(',');
					_customerLogic.Create(new()
					{
						Login = customerRecData[0],
						Password = customerRecData[1],
						Name = customerRecData[2],
						Surname = customerRecData[3]
					});
				}
			}
			return true;
		}
		private bool GenerateVehicleData()
		{
			if (_vehicleLogic.ReadList(null)?.Count != 0)
			{
				return false;
			}
			using (StreamReader sr = new("vehicles.txt"))
			{
				string? currentString;
				while ((currentString = sr.ReadLine()) != null)
				{
					var vehicleRecData = currentString.Split(',');
					_vehicleLogic.Create(new()
					{
						Name = vehicleRecData[0],
						Plate = vehicleRecData[1],
						VIN = vehicleRecData[2],
						CustomerId = Convert.ToInt32(vehicleRecData[4])
					});
				}
			}
			return true;
		}
		private bool GenerateRepairRequestData()
		{
			if (_repairRequestLogic.ReadList(null)?.Count != 0)
			{
				return false;
			}
			using (StreamReader sr = new("requests.txt"))
			{
				string? currentString;
				while ((currentString = sr.ReadLine()) != null)
				{
					var repairRequestRecData = currentString.Split(',');
					_repairRequestLogic.Create(new()
					{
						DateCreated = Convert.ToDateTime(repairRequestRecData[0]),
						VehicleId = Convert.ToInt32(repairRequestRecData[1])
					});
				}
			}
			return true;
		}
		public bool GenerateRequestData()
		{
			if (!GenerateCustomerData())
			{
				return false;
			}
			if (!GenerateVehicleData())
			{
				return false;
			}
			if (!GenerateRepairRequestData())
			{
				return false;
			}
			return true;
		}
		public bool GenerateWorksInRequest()
		{
			var WorksList = _workLogic.ReadList(null);
			if (WorksList == null)
			{
				return false;
			}
			if (WorksList.Count == 0)
			{
				return false;
			}
			var RequestList = _repairRequestLogic.ReadList(null);
			if (RequestList == null)
			{
				return false;
			}
			if (RequestList.Count == 0)
			{
				return false;
			}
			Random r = new();
			for (int i = 0; i < 2; i++)
			{
				_workInRequestLogic.Create(new()
				{
					RepairRequestId = r.Next(0, RequestList.Count),
					WorkId = r.Next(0, WorksList.Count),
					Count = r.Next(1, 2)
				});
			}
			return true;

		}
	}
}