using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using CarCenterDataModels.Models;
using Microsoft.Extensions.Logging;

namespace CarCenterBusinessLogic.BusinessLogics
{
    public class PreSaleWorkLogic : IPreSaleWorkLogic
    {
        private readonly ILogger _logger;
        private readonly IPreSaleWorkStorage _PreSaleWorkStorage;
        private readonly IManagerLogic _managerLogic;

        public PreSaleWorkLogic(ILogger<PreSaleWorkLogic> logger, IPreSaleWorkStorage PreSaleWorkStorage, IManagerLogic ManagerLogic)
        {
            _logger = logger;
            _PreSaleWorkStorage = PreSaleWorkStorage;
            _managerLogic = ManagerLogic;
        }

        public bool AddSaleToPreSaleWork(PreSaleWorkSearchModel model, ISaleModel Sale)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            _logger.LogInformation("AddSaleToPreSaleWork. PreSaleWorkType:{PreSaleWorkType}.Id:{ Id}", model.PreSaleWorkType, model.Id);
            var element = _PreSaleWorkStorage.GetElement(model);

            if (element == null)
            {
                _logger.LogWarning("AddSaleToPreSaleWork element not found");
                return false;
            }

            _logger.LogInformation("AddSaleToPreSaleWork find. Id:{Id}", element.Id);

            element.PreSaleWorkSales[Sale.Id] = Sale;

            _PreSaleWorkStorage.Update(new()
            {
                Id = element.Id,
                PreSaleWorkType = element.PreSaleWorkType,
                PreSaleWorkPrice = element.PreSaleWorkPrice,
                ManagerId = element.ManagerId,
                PreSaleWorkSales = element.PreSaleWorkSales
            });

            return true;
        }

        public bool Create(PreSaleWorkBindingModel model)
        {
            CheckModel(model);
            model.PreSaleWorkSales = new();

            var result = _PreSaleWorkStorage.Insert(model);

            if (result == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }

            return true;
        }

        public bool Delete(PreSaleWorkBindingModel model)
        {
            CheckModel(model, false);

            _logger.LogInformation("Delete. Id:{Id}", model.Id);

            var result = _PreSaleWorkStorage.Delete(model);

            if (result== null)
            {
                _logger.LogWarning("Delete operation failed");
                return false;
            }

            return true;
        }

        public PreSaleWorkViewModel? ReadElement(PreSaleWorkSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            _logger.LogInformation("ReadElement. PreSaleWorkType:{PreSaleWorkType}.Id:{Id}", model.PreSaleWorkType, model.Id);

            var element = _PreSaleWorkStorage.GetElement(model);

            if (element == null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }

            _logger.LogInformation("ReadElement find. Id:{Id}", element.Id);

            return element;
        }

        public List<PreSaleWorkViewModel>? ReadList(PreSaleWorkSearchModel? model)
        {
            _logger.LogInformation("ReadList. PreSaleWorkType:{PreSaleWorkType}.Id:{ Id}", model?.PreSaleWorkType, model?.Id);

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

            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }

            _logger.LogInformation("ReadList. Count:{Count}", list.Count);

            return list;
        }

        public bool Update(PreSaleWorkBindingModel model)
        {
            CheckModel(model);

            if (_PreSaleWorkStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }

            return true;
        }

        private void CheckModel(PreSaleWorkBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (!withParams)
            {
                return;
            }

            if (string.IsNullOrEmpty(model.PreSaleWorkType))
            {
                throw new ArgumentNullException("Нет такого типа предпродажной работы", nameof(model.PreSaleWorkType));
            }

            if (model.PreSaleWorkPrice<0)
            {
                throw new ArgumentNullException("Стоимость предпродажной работы не может быть меньше 0", nameof(model.PreSaleWorkPrice));
            }

            _logger.LogInformation("PreSaleWork. PreSaleWorkType:{PreSaleWorkType}.PreSaleWorkPrice:{ PreSaleWorkPrice}. Id: { Id}", model.PreSaleWorkType, model.PreSaleWorkPrice, model.Id);

            var element = _PreSaleWorkStorage.GetElement(new PreSaleWorkSearchModel
            {
                PreSaleWorkType = model.PreSaleWorkType
            });

            if (element != null && element.Id != model.Id)
            {
                throw new InvalidOperationException("Предпродажная работа с таким типом уже есть");
            }
        }
    }
}
