using DressAtelierContracts.BindingModels;
using DressAtelierContracts.BusinessLogicContracts;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.StorageContracts;
using DressAtelierContracts.ViewModels;
using DressAtelierDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DressAtelierBusinessLogic.BusinessLogic
{
    public class OrderLogic : IOrderLogic
    {
        private readonly ILogger _logger;

        private readonly IOrderStorage _orderStorage;

        public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
        {
            _logger = logger;
            _orderStorage = orderStorage;
        }

        public bool CreateOrder(OrderBindingModel model)
        {
            CheckModel(model);

            if (model.Status != OrderStatus.Unknown)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }

            model.Status = OrderStatus.Accepted;
            if (_orderStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }

        public bool GivenOrder(OrderBindingModel model)
        {
            CheckModel(model, false);
            _logger.LogInformation("Update to given status. ID:{ID}", model.ID);

            model.Status = OrderStatus.Given;
            model.DateImplement = DateTime.Now;
            model.EmployeeID = ReadElement(new OrderSearchModel
            {
                ID = model.ID
            }).EmployeeID;
            _orderStorage.Update(model);

            return true;
        }

        public bool ReadyOrder(OrderBindingModel model)
        {
            CheckModel(model,false);
            _logger.LogInformation("Update to ready status. ID:{ID}", model.ID);
            model.Status = OrderStatus.Ready;
            model.EmployeeID = ReadElement(new OrderSearchModel
            {
                ID = model.ID
            }).EmployeeID;
            _orderStorage.Update(model);
            return true;
        }

        public bool TakeOrderInWork(OrderBindingModel model)
        {
            CheckModel(model,false);
            var check = ReadElement(new OrderSearchModel
            {
                ID = model.ID
            });

            if(check.EmployeeID.HasValue)
            {
                throw new InvalidOperationException();
            }

            _logger.LogInformation("Update to in process status. ID:{ID}", model.ID);
            model.Status = OrderStatus.InProcess;
            _orderStorage.Update(model);
            return true;

        }

        public List<OrderViewModel>? ReadList(OrderSearchModel? model)
        {
            _logger.LogInformation("ReadList. OrderID:{ID}",model?.ID);

            var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);

            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation("ReadList. Count:{Count}", list.Count);
            return list;
        }

        public OrderViewModel? ReadElement(OrderSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. Status:{Status}. DateTo:{DateTo}. DateFrom:{DateFrom}. EmployeeID:{EmployeeID}. ClientID:{ClientID}.ID:{ ID}", model.Status,model.DateTo,model.DateFrom,model.EmployeeID,model.ClientID, model.ID);
            var element = _orderStorage.GetElement(model);
            if (element == null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }
            _logger.LogInformation("ReadElement find. ID:{ID}", element.ID);
            return element;
        }
        public void CheckModel(OrderBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
            _logger.LogInformation("Order. OrderID: { ID} ",model.ID);

            var element = _orderStorage.GetElement(new OrderSearchModel
            {
                ID = model.ID
            });

            if (element != null && element.ID == model.ID)
            {
                throw new InvalidOperationException("Order with such name already exists.");
            }
        }
    }
}
