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

namespace ComputerShopBusinessLogic.BusinessLogics
{
    public class EquipmentReceivingLogic : IEquipmentReceivingLogic
    {
        private readonly ILogger _logger;
        private readonly IEquipmentReceivingStorage _receivingStorage;

        public EquipmentReceivingLogic(ILogger<PurchaseLogic> logger, IEquipmentReceivingStorage receivingStorage)
        {
            _logger = logger;
            _receivingStorage = receivingStorage;
        }
        public List<EquipmentReceivingViewModel>? ReadList(EquipmentReceivingSearchModel? model)
        {
            var list = model == null ? _receivingStorage.GetFullList() : _receivingStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            return list;
        }

        public bool Create(EquipmentReceivingBindingModel model)
        {
            CheckModel(model);
            if (model.Status != EquipmentReceivingStatus.Неизвестен)
            {
                _logger.LogWarning("Insert operation failed. Receiving status incorrect.");
                return false;
            }
            model.Status = EquipmentReceivingStatus.Ожидается;
            if (_receivingStorage.Insert(model) == null)
            {
                model.Status = EquipmentReceivingStatus.Неизвестен;
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }

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

		public bool Finish(EquipmentReceivingBindingModel model)
        {
            return StatusUpdate(model, EquipmentReceivingStatus.Получено);
        }

        private void CheckModel(EquipmentReceivingBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
        }
        public bool StatusUpdate(EquipmentReceivingBindingModel model, EquipmentReceivingStatus newStatus)
        {
            CheckModel(model);
            if (model.Status + 1 != newStatus)
            {
                _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect.");
                return false;
            }
            model.Status = newStatus;
            if (model.Status == EquipmentReceivingStatus.Получено) model.DateImplement = DateTime.Now;
            if (_receivingStorage.Update(model) == null)
            {
                model.Status--;
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }

    }
}
