using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComputerStoreBusinessLogic.BusinessLogic
{
    public class RequestLogic : IRequestLogic
    {
        private readonly ILogger _logger;
        private readonly IRequestStorage _requestStorage;
        public RequestLogic(ILogger<RequestLogic> logger, IRequestStorage requestStorage)
        {
            _logger = logger;
            _requestStorage = requestStorage;
        }

        public bool Create(RequestBindingModel model)
        {
            CheckModel(model);
            if (_requestStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }

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

        public bool Delete(RequestBindingModel model)
        {
            CheckModel(model, false);
            _logger.LogInformation("Delete. ID:{ID}", model.ID);
            if (_requestStorage.Delete(model) == null)
            {
                _logger.LogWarning("Delete operation failed");
                return false;
            }
            return true;
        }

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

            _logger.LogInformation("ReadElement. Request ID:{ ID}. Order ID:{ OrderID}", model.ID, model.OrderID);

            var element = _requestStorage.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<RequestViewModel>? ReadList(RequestSearchModel? model)
        {
            _logger.LogInformation("ReadList. Request ID:{ ID}. Order ID: { OrderID}", model?.ID, model?.OrderID);

            var list = model == null ? _requestStorage.GetFullList() : _requestStorage.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(RequestBindingModel model, bool withParams = true)
        {
            if (model == null) { return; }
            if (!withParams) { return; }
            if (model.Price <= 0) { throw new ArgumentNullException("Invalid request's price", nameof(model)); }
            if (string.IsNullOrEmpty(model.OrderID.ToString())) { throw new ArgumentNullException("Invalid Request's order ID", nameof(model)); }

            _logger.LogInformation("Request. Request ID:{ ID}. Order ID: { OrderID}", model.ID, model.OrderID);
        }
    }
}