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 CreditProgramLogic : ICreditProgramLogic
    {
        private readonly ILogger _logger;
        private readonly ICreditProgramStorage _CreditProgramStorage;
        public CreditProgramLogic(ILogger<CreditProgramLogic> logger, ICreditProgramStorage CreditProgramStorage)
        {
            _logger = logger;
            _CreditProgramStorage = CreditProgramStorage;
        }
        public List<CreditProgramViewModel>? ReadList(CreditProgramSearchModel? model)
        {
            _logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
            var list = model == null ? _CreditProgramStorage.GetFullList() : _CreditProgramStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation("ReadList. Count:{Count}", list.Count);
            return list;
        }
        public CreditProgramViewModel? ReadElement(CreditProgramSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
            var element = _CreditProgramStorage.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(CreditProgramBindingModel model)
        {
            CheckModel(model);
            if (_CreditProgramStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }

        public bool Delete(CreditProgramBindingModel model)
        {
            CheckModel(model);
            if (_CreditProgramStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }

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

        private void CheckModel(CreditProgramBindingModel 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 (model.Persent <= 0)
            {
                throw new InvalidOperationException("Процент кредитования неположительный!");
            }
            _logger.LogInformation("Name:{ Name}. Percent: {Percent}. Id: { Id}", model.Name, model.Persent, model.Id);
        }
    }
}
