using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using CarCenterDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace CarCenterBusinessLogic.BusinessLogics
{
    public class EquipmentLogic : IEquipmentLogic
    {
        private readonly ILogger _logger;
        private readonly IEquipmentStorage _equipmentStorage;

        public EquipmentLogic(ILogger<EquipmentLogic> logger, IEquipmentStorage equipmentStorage)
        {
            _logger = logger;
            _equipmentStorage = equipmentStorage;
        }
        public bool Create(EquipmentBindingModel model)
        {
            CheckModel(model);
            model.EquipmentCars = new();

            var result = _equipmentStorage.Insert(model);

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

            return true;
        }

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

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

            var result = _equipmentStorage.Delete(model);

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

            return true;
        }

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

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

            var element = _equipmentStorage.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<EquipmentViewModel>? ReadList(EquipmentSearchModel? model)
        {
            _logger.LogInformation("ReadList. EquipmentName:{EquipmentName}.Id:{ Id}", model?.EquipmentName, model?.Id);

            var list = model == null ? _equipmentStorage.GetFullList() : _equipmentStorage.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(EquipmentBindingModel model)
        {
            CheckModel(model);

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

            return true;
        }

        public bool AddCarToEquipment(EquipmentSearchModel model, ICarModel car)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            _logger.LogInformation("AddCarToEquipment. EquipmentName:{EquipmentName}.Id:{ Id}", model.EquipmentName, model.Id);
            var element = _equipmentStorage.GetElement(model);

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

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

            element.EquipmentCars[car.Id] = car;

            _equipmentStorage.Update(new()
            {
                Id = element.Id,
                EquipmentName = element.EquipmentName,
                EquipmentPrice = element.EquipmentPrice,
                AdministratorId = element.AdministratorId,
                PreSaleWorkId = element.PreSaleWorkId,
                EquipmentCars = element.EquipmentCars,
            });

            return true;
        }

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

            if (!withParams)
            {
                return;
            }

            if (string.IsNullOrEmpty(model.EquipmentName))
            {
                throw new ArgumentNullException("Нет названия комплектации", nameof(model.EquipmentName));
            }

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

            _logger.LogInformation("Equipment. EquipmentName:{EquipmentName}.EquipmentPrice:{ EquipmentPrice}. Id: { Id}", model.EquipmentName, model.EquipmentPrice, model.Id);
        }
    }
}
