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

namespace BankBusinessLogic.BusinessLogics
{
    public class BankOperatorLogic : IBankOperatorLogic
    {
        private readonly ILogger _logger;
        private readonly IBankOperatorStorage _BankOperatorStorage;

        public BankOperatorLogic(ILogger<BankOperatorLogic> logger, IBankOperatorStorage BankOperatorStorage)
        {
            _logger = logger;
            _BankOperatorStorage = BankOperatorStorage;
        }
        public List<BankOperatorViewModel>? ReadList(BankOperatorSearchModel? model)
        {
            _logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
            var list = model == null ? _BankOperatorStorage.GetFullList() : _BankOperatorStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation("ReadList. Count:{Count}", list.Count);
            return list;
        }
        public BankOperatorViewModel? ReadElement(BankOperatorSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. Id:{ Id}. Login: { Login}", model.Id, model.Login);
            var element = _BankOperatorStorage.GetElement(model);
            if (element == null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }
            _logger.LogInformation("ReadElement find. Id:{Id}. Login: { Login}", element.Id);
            return element;
        }
        public bool Create(BankOperatorBindingModel model)
        {
            CheckModel(model);
            if (_BankOperatorStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }

        public bool Delete(BankOperatorBindingModel model)
        {
            CheckModel(model, false);
            _logger.LogInformation("Delete. Id:{Id}", model.Id);
            if (_BankOperatorStorage.Delete(model) == null)
            {
                _logger.LogWarning("Delete operation failed");
                return false;
            }
            return true;
        }
        public bool Update(BankOperatorBindingModel model)
        {
            CheckModel(model);
            if (_BankOperatorStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }

        private void CheckModel(BankOperatorBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
            if (string.IsNullOrEmpty(model.FirstName))
            {
                throw new ArgumentNullException("Нет имени!", nameof(model.FirstName));
            }
            if (string.IsNullOrEmpty(model.LastName))
            {
                throw new ArgumentNullException("Нет фамилии!", nameof(model.LastName));
            }
            if (string.IsNullOrEmpty(model.Login))
            {
                throw new ArgumentNullException("Нет логина!", nameof(model.Login));
            }
            if (string.IsNullOrEmpty(model.Password))
            {
                throw new ArgumentNullException("Нет пароля!", nameof(model.Password));
            }
            _logger.LogInformation("FirstName:{ FirstName}. LastName: {LastName}. Login: {Login} .Id: { Id}",
                model.FirstName, model.LastName, model.Login, model.Id);
        }
    }
}
