using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TravelCompanyContracts.BindingModels.OperatorBindingModels;
using TravelCompanyContracts.BusinessLogicsContracts.OperatorBusinessLogicsContracts;
using TravelCompanyContracts.SearchModels.OperatorSearchModels;
using TravelCompanyContracts.StoragesContracts.OperatorStoragesContracts;
using TravelCompanyContracts.ViewModels.OperatorViewModels;

namespace TravelCompanyBusinessLogic.BusinessLogic.OperatorLogics
{
    public class GuideLogic:IGuideLogic
    {
        private readonly ILogger _logger;

        private readonly IGuideStorage _guideStorage;

        public GuideLogic(ILogger<GuideLogic> logger, IGuideStorage guideStorage)
        {
            _logger = logger;
            _guideStorage = guideStorage;
        }

        public bool Create(GuideBindingModel model)
        {
            CheckModel(model);

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

            return true;
        }
        public bool Delete(GuideBindingModel model)
        {
            CheckModel(model, false);

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

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

            return true;
        }
        public GuideViewModel? ReadElement(GuideSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

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

            var element = _guideStorage.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<GuideViewModel>? ReadList(GuideSearchModel? model)
        {
            _logger.LogInformation("ReadList. Surname:{Surname}.Id:{ Id}", model?.Surname, model?.Id);

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

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

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

            return list;
        }

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

            if (!withParams)
            {
                return;
            }

            if (string.IsNullOrEmpty(model.Name))
            {
                throw new ArgumentNullException("Нет имени гида", nameof(model.Name));
            }
            if (string.IsNullOrEmpty(model.Surname))
            {
                throw new ArgumentNullException("Нет фамилии гида", nameof(model.Surname));
            }
            _logger.LogInformation("Guide. Id:{Id}.Surname:{Surname}.Name:{Name}.Patronymic:{Patronymic}.OperatorId:{OperatorId}.",
                model.Id, model.Surname, model.Name, model.Patronymic, model.OperatorId);

            var element = _guideStorage.GetElement(new GuideSearchModel
            {
                Surname = model.Surname
            });

            if (element != null && element.Id != model.Id && element.OperatorId == model.OperatorId)
            {
                throw new InvalidOperationException("Этот гид уже добавлен");
            }
        }
    }
}
