using Contracts.BindingModels;
using Contracts.BusinessLogicsContracts;
using Contracts.SearchModels;
using Contracts.ViewModels;

namespace GuarantorAPP
{
	public class GuarantorData
	{
		private readonly ILogger _logger;
		private readonly IGuarantorLogic _guarantorLogic;
		private readonly IWorkerLogic _workerLogic;
		private readonly IMachineLogic _machineLogic;
		private readonly IWorkshopLogic _workshopLogic;
		private readonly IProductionLogic _productionLogic;

		public GuarantorData(ILogger logger, IGuarantorLogic guarantorLogic, IWorkerLogic workerLogic, IMachineLogic machineLogic, IWorkshopLogic workshopLogic, IProductionLogic productionLogic)
		{
			_logger = logger;
			_guarantorLogic = guarantorLogic;
			_workerLogic = workerLogic;
			_machineLogic = machineLogic;
			_workshopLogic = workshopLogic;
			_productionLogic = productionLogic;
		}

		public GuarantorViewModel? Login(string login,  string password)
		{
			return _guarantorLogic.ReadElement(new()
			{ 
				Login = login,
				Password = password
			});
		}

		public bool Register(GuarantorBindingModel model)
		{
			return _guarantorLogic.Create(model);
		}

		public bool UpdateUser(GuarantorBindingModel model)
		{
			return _guarantorLogic.Update(model);
		}

		public List<WorkerViewModel>? GetWorkers(int userId)
		{
			return _workerLogic.ReadList(new WorkerSearchModel() { UserId = userId });
		}
		public bool DeleteWorker(int workerId)
		{
			return _workerLogic.Delete(new() { Id = workerId });
		}
		public bool CreateWorker(WorkerBindingModel model)
		{
			return _workerLogic.Create(model);
		}
		public bool UpdateWorker(WorkerBindingModel model)
		{
			return _workerLogic.Update(model);
		}
		public WorkerViewModel? GetWorker(int id)
		{
			return _workerLogic.ReadElement(new() { Id = id });
		}

		public List<WorkshopViewModel>? GetWorkshops(int userId)
		{
			return _workshopLogic.ReadList(new WorkshopSearchModel() { UserId = userId });
		}
		public WorkshopViewModel? GetWorkshop(int id)
		{
			return _workshopLogic.ReadElement(new() { Id = id });
		}
		public bool UpdateWorkshop(WorkshopBindingModel model)
		{
			return _workshopLogic.Update(model);
		}
		public bool DeleteWorkshop(int workshopId)
		{
			return _workshopLogic.Delete(new() { Id = workshopId });
		}
		public bool CreateWorkshop(WorkshopBindingModel model)
		{
			return _workshopLogic.Create(model);
		}

		public List<MachineViewModel>? GetMachines(int userId)
		{
			return _machineLogic.ReadList(new() { UserId = userId });
		}
		public MachineViewModel? GetMachine(int id)
		{
			return _machineLogic.ReadElement(new() { Id = id });
		}
		public bool CreateMachine(MachineBindingModel model)
		{
			return _machineLogic.Create(model);
		}
		public bool UpdateMachine(MachineBindingModel model)
		{
			return _machineLogic.Update(model);
		}
		public bool DeleteMachine(int machineId)
		{
			return _machineLogic.Delete(new() { Id = machineId });
		}

		public List<ProductionViewModel>? GetProductions()
		{
			return _productionLogic.ReadList(null);
		}

		public List<MachineWorkshopTimeReport> GetTimeReport(DateTime? startDate, DateTime? endDate, int UserId)
		{
			var workshops = _workshopLogic.ReadList(new() { DateFrom = startDate, DateTo = endDate, UserId = UserId });
			if (workshops == null)
				return new();
			List<MachineWorkshopTimeReport> machineWorkshopTimeReports = new List<MachineWorkshopTimeReport>();
			foreach (var workshop in workshops)
			{
				var report = new MachineWorkshopTimeReport();
				report.WorkshopName = workshop.Title;
				var machines = _machineLogic.ReadList(new() { WorkshopId = workshop.Id, UserId = UserId });
				if (machines != null)
					report.Machines = machines.Select(p => p.Title).ToList();
				machineWorkshopTimeReports.Add(report);
			}
			return machineWorkshopTimeReports;
		}
	}
}
