using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.ViewModels;
using DocumentFormat.OpenXml.Packaging;
using Microsoft.AspNetCore.Mvc;
using System.Windows;

namespace ComputerStoreRestAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainController : Controller
    {
        private readonly ILogger _logger;
        private readonly IComponentLogic _componentLogic;
        private readonly IPCLogic _pcLogic;
        private readonly IProductLogic _productLogic;
        private readonly IRequestComponentLogic _requestComponentLogic;
        private readonly IEmployeeReportLogic _employeeReportLogic;
        private readonly IRequestLogic _requestLogic;

        private readonly IOrderLogic _orderLogic;
        private readonly IConsignmentLogic _consignmentLogic;
        private readonly IReportOrderLogic _reportOrderLogic;


        public MainController(
            ILogger<MainController> logger,
            IComponentLogic componentLogic,
            IPCLogic pcLogic,
            IProductLogic productLogic,
            IEmployeeReportLogic employeeReportLogic,
            IRequestComponentLogic requestComponentLogic,
            IRequestLogic requestLogic,
            IOrderLogic orderLogic,
            IConsignmentLogic consignmentLogic,
            IReportOrderLogic reportOrderLogic)
        {
            _logger = logger;
            _componentLogic = componentLogic;
            _pcLogic = pcLogic;
            _productLogic = productLogic;
            _employeeReportLogic = employeeReportLogic;
            _requestComponentLogic = requestComponentLogic;
            _requestLogic = requestLogic;
            _orderLogic = orderLogic;
            _consignmentLogic = consignmentLogic;
            _reportOrderLogic = reportOrderLogic;
        }

        [HttpGet]
        public List<ComponentViewModel>? GetComponentsList()
        {
            try
            {
                return _componentLogic.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Receiving list of components error.");
                throw;
            }
        }

        [HttpGet]
        public ComponentViewModel? GetComponent(int id)
        {
            try
            {
                return _componentLogic.ReadElement(new ComponentSearchModel { ID = id });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Receiving list of components error.");
                throw;
            }
        }

        [HttpDelete("{id}")]
        public bool DeleteComponent(int id)
        {
            try
            {
                return _componentLogic.Delete(new ComponentBindingModel { ID = id });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Removing component error.");
                throw;
            }
        }

        [HttpPatch]
        public bool UpdateComponent(ComponentBindingModel component)
        {
            try
            {
                return _componentLogic.Update(component);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Updating component error.");
                throw;
            }
        }

        [HttpPost]
        public bool InsertComponent(ComponentBindingModel component)
        {
            try
            {
                return _componentLogic.Create(component);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Inserting component error.");
                throw;
            }
        }

        [HttpGet]
        public List<ProductViewModel>? GetProductsList()
        {
            try
            {
                return _productLogic.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Receiving list of products error.");
                throw;
            }
        }

        [HttpDelete("{id}")]
        public bool DeleteProduct(int id)
        {
            try
            {
                return _productLogic.Delete(new ProductBindingModel { ID = id });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Removing product error.");
                throw;
            }
        }

        [HttpPatch]
        public bool UpdateProduct(ProductBindingModel product)
        {
            try
            {
                return _productLogic.Update(product);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Updating product error.");
                throw;
            }
        }

        [HttpPost]
        public bool InsertProduct(ProductBindingModel product)
        {
            try
            {
                return _productLogic.Create(product);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Inserting product error.");
                throw;
            }
        }

        [HttpGet]
        public List<PCViewModel>? GetPCsList()
        {
            try
            {
                return _pcLogic.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Receiving list of PCs error.");
                throw;
            }
        }

        [HttpDelete("{id}")]
        public bool DeletePC(int id)
        {
            try
            {
                return _pcLogic.Delete(new PCBindingModel { ID = id });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Removing PC error.");
                throw;
            }
        }

        [HttpPatch]
        public bool UpdateProduct(PCBindingModel pc)
        {
            try
            {
                return _pcLogic.Update(pc);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Updating PC error.");
                throw;
            }
        }

        [HttpPost]
        public bool InsertProduct(PCBindingModel pc)
        {
            try
            {
                return _pcLogic.Create(pc);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Inserting PC error.");
                throw;
            }
        }

        [HttpGet]
        public List<RequestComponentViewModel>? GetRequestComponentList(int? id)
        {
            try
            {
                return _requestComponentLogic.ReadList(new RequestSearchModel { ID = id });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Receiving list of requestcomponent error.");
                throw;
            }
        }


		#region Consignment
		[HttpGet]
		public List<ConsignmentViewModel>? GetConsignmentList()
		{
			try
			{
				return _consignmentLogic.ReadList(null);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Receiving list of consignment error.");
				throw;
			}
		}

		[HttpGet]
		public ConsignmentViewModel? GetConsignment(int id)
		{
			try
			{
				return _consignmentLogic.ReadElement(new ConsignmentSearchModel { ID = id });
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Receiving consignment error.");
				throw;
			}
		}

		[HttpDelete("{id}")]
		public bool DeleteConsignment(int id)
		{
			try
			{
				return _consignmentLogic.Delete(new ConsignmentBindingModel { ID = id });
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Removing consignment error.");
				throw;
			}
		}

		[HttpPatch]
		public bool UpdateConsignment(ConsignmentBindingModel consignment)
		{
			try
			{
				return _consignmentLogic.Update(consignment);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Updating consignment error.");
				throw;
			}
		}

		[HttpPost]
		public bool InsertConsignment(ConsignmentBindingModel consignment)
		{
			try
			{
				return _consignmentLogic.Create(consignment);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Inserting consignment error.");
				throw;
			}
		}
		#endregion

        #region Request
        [HttpGet]
        public List<RequestViewModel>? GetRequestList()
        {
            try
            {
                return _requestLogic.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Receiving list of requestcomponent error.");
                throw;
            }
        }

		[HttpGet]
		public RequestViewModel? GetRequest(int id)
		{
			try
			{
				return _requestLogic.ReadElement(new RequestSearchModel { ID = id });
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Receiving request error.");
				throw;
			}
		}

		[HttpDelete("{id}")]
		public bool DeleteRequest(int id)
		{
			try
			{
				return _requestLogic.Delete(new RequestBindingModel { ID = id });
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Removing request error.");
				throw;
			}
		}

		[HttpPatch]
		public bool UpdateRequest(RequestBindingModel request)
		{
			try
			{
				return _requestLogic.Update(request);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Updating request error.");
				throw;
			}
		}

		[HttpPost]
		public bool InsertRequest(RequestBindingModel request)
		{
			try
			{
				return _requestLogic.Create(request);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Inserting request error.");
				throw;
			}
		}
		#endregion

		#region Order
		[HttpGet]
		public List<OrderViewModel>? GetOrdersList()
		{
			try
			{
				return _orderLogic.ReadList(null);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Receiving list of orders error.");
				throw;
			}
		}

		[HttpGet]
		public OrderViewModel? GetOrder(int id)
		{
			try
			{
				return _orderLogic.ReadElement(new OrderSearchModel { ID = id });
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Receiving order error.");
				throw;
			}
		}

		[HttpDelete("{id}")]
		public bool DeleteOrder(int id)
		{
			try
			{
				return _orderLogic.DeleteOrder(new OrderBindingModel { ID = id });
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Removing order error.");
				throw;
			}
		}

		[HttpPatch]
		public bool UpdateOrder(OrderBindingModel order)
		{
			try
			{
				return _orderLogic.UpdateOrder(order);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Updating order error.");
				throw;
			}
		}

		[HttpPatch]
		public bool UpdateOrderInProcess(OrderBindingModel order)
		{
			try
			{
				return _orderLogic.TakeOrderInProcess(order);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Updating order in process error.");
				throw;
			}
		}

		[HttpPatch]
		public bool UpdateOrderReady(OrderBindingModel order)
		{
			try
			{
				return _orderLogic.TakeOrderReady(order);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Updating order ready error.");
				throw;
			}
		}

		[HttpPatch]
		public bool UpdateOrderGiven(OrderBindingModel order)
		{
			try
			{
				return _orderLogic.TakeOrderGiven(order);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Updating order given error.");
				throw;
			}
		}

		[HttpPost]
		public bool InsertOrder(OrderBindingModel order)
		{
			try
			{
				return _orderLogic.CreateOrder(order);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Inserting order error.");
				throw;
			}
		}
        #endregion

        [HttpGet]
        public List<ReportConsignmentsViewModel> GetReportConsignments(ReportComponentsBindingModel model)
        {
            return _employeeReportLogic.GetConsignmentsByComponents(model);
        }

        [HttpGet]
        public byte[] SaveReportConsignmentsToExcel(ReportComponentsBindingModel model)
        {
            return _employeeReportLogic.SaveConsignmentsToExcelFile(new ReportComponentsBindingModel
            {
                FileName = "ReportConsignments",
                Components = model.Components
            });
        }

        [HttpGet]
        public byte[] SaveReportConsignmentsToWord(ReportComponentsBindingModel model)
        {
            return _employeeReportLogic.SaveConsignmentsToWordFile(new ReportComponentsBindingModel
            {
                FileName = "ReportConsignments",
                Components = model.Components
            });
        }
    }
}