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

namespace CarCenterBusinessLogic.BusinessLogics
{
    public class EmployeeLogic : IEmployeeLogic
    {
        private readonly ILogger _logger;
        private readonly IEmployeeStorage _employeeStorage;
        private readonly IManagerLogic _managerLogic;

        public EmployeeLogic(ILogger<EmployeeLogic> logger, IEmployeeStorage EmployeeStorage, IManagerLogic ManagerLogic)
        {
            _logger = logger;
            _employeeStorage = EmployeeStorage;
            _managerLogic = ManagerLogic;
        }
        public bool Create(EmployeeBindingModel model)
        {
            CheckModel(model);
            model.EmployeeSales = new();

            var result = _employeeStorage.Insert(model);

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

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

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

            var result = _employeeStorage.Delete(model);

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

            return true;
        }

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

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

            var element = _employeeStorage.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<EmployeeViewModel>? ReadList(EmployeeSearchModel? model)
        {
            _logger.LogInformation("ReadList. EmployeeFIO:{EmployeeFIO}.Id:{ Id}", model?.EmployeeFIO, model?.Id);

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

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

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

            return list;
        }

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

            _logger.LogInformation("AddSaleToEmployee. EmployeeFIO:{EmployeeFIO}.Id:{ Id}", model.EmployeeFIO, model.Id);
            var element = _employeeStorage.GetElement(model);

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

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

            element.EmployeeSales[Sale.Id] = Sale;

            _employeeStorage.Update(new()
            {
                Id = element.Id,
                EmployeeFIO = element.EmployeeFIO,
                Specialization = element.Specialization,
                ManagerId = element.ManagerId,
                EmployeeSales = element.EmployeeSales,
            });

            return true;
        }


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

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

            return true;
        }

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

            if (!withParams)
            {
                return;
            }

            if (string.IsNullOrEmpty(model.EmployeeFIO))
            {
                throw new ArgumentNullException("Нет ФИО сотрудника", nameof(model.EmployeeFIO));
            }

            _logger.LogInformation("Employee. EmployeeFIO:{EmployeeFIO}.StartDate:{ StartDate}. Id: { Id}", model.EmployeeFIO, model.Specialization, model.Id);
        }
    }
}
