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 OperatorLogic:IOperatorLogic
    {
        private readonly ILogger _logger;
        private readonly IOperatorStorage _operatorStorage;
        public OperatorLogic(ILogger<OperatorLogic> logger, IOperatorStorage operatorStorage)
        {
            _logger = logger;
            _operatorStorage = operatorStorage;
        }
        public List<OperatorViewModel>? ReadList(OperatorSearchModel? model)
        {
            _logger.LogInformation("ReadList.  Login:{Login}.Id:{ Id}", model?.Login, model?.Id);
            var list = model == null ? _operatorStorage.GetFullList() : _operatorStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation("ReadList. Count:{Count}", list.Count);
            return list;
        }
        public OperatorViewModel? ReadElement(OperatorSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. Login:{Login}.Id:{ Id}", model?.Login, model?.Id);
            var element = _operatorStorage.GetElement(model);
            if (element == null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }
            _logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
            return element;
        }
        public bool Create(OperatorBindingModel model)
        {
            CheckModel(model);
            if (_operatorStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }
        public bool Update(OperatorBindingModel model)
        {
            CheckModel(model);
            if (_operatorStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }
        private void CheckModel(OperatorBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
            if (string.IsNullOrEmpty(model.Login))
            {
                throw new ArgumentNullException("Нет логина оператора", nameof(model.Login));
            }
            if (string.IsNullOrEmpty(model.Email))
            {
                throw new ArgumentNullException("Нет почты оператора", nameof(model.Email));
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                throw new ArgumentNullException("Нет имени оператора", nameof(model.Name));
            }
            if (string.IsNullOrEmpty(model.Surname))
            {
                throw new ArgumentNullException("Нет фамилии оператора", nameof(model.Surname));
            }
            if (string.IsNullOrEmpty(model.Password))
            {
                throw new ArgumentNullException("Нет пароля оператора", nameof(model.Password));
            }
            if (model.BirthDay == default)
            {
                throw new ArgumentNullException("Нет даты рождения оператора", nameof(model.BirthDay));
            }
            _logger.LogInformation("Operator. Id:{Id}.Surname:{Surname}.Name:{Name}.Patronomic:{Patronomic}.BirthDay:{BirthDay}.Email:{Email}.Login:{Login}.Password:{ Password}.", 
                model.Id, model.Surname, model.Name, model.Patronymic, model.BirthDay, model.Email, model.Login, new string('*', model.Password.Length));
            var element = _operatorStorage.GetElement(new OperatorSearchModel
            {
                Login = model.Login
            });
            if (element != null && element.Id != model.Id)
            {
                throw new InvalidOperationException("Оператор с таким логином уже есть");
            }
        }
    }
}
