using HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using Microsoft.Extensions.Logging;

namespace HotelBusinessLogic.BusinessLogics;

public class CleaningLogic : ICleaningLogic
{
    private readonly ICleaningStorage _cleaningStorage;
    private readonly ILogger _logger;

    public CleaningLogic(ICleaningStorage cleaningStorage, ILogger<MaitreLogic> logger)
    {
        _logger = logger;
        _cleaningStorage = cleaningStorage;
    }
    
    public List<CleaningViewModel>? ReadList(CleaningSearchModel? model)
    {
        var list = model == null ? _cleaningStorage.GetFullList() : _cleaningStorage.GetFilteredList(model);
        _logger.LogInformation("ReadList .Count:{Count}", list.Count);
        return list;
    }

    public CleaningViewModel? ReadElement(CleaningSearchModel model)
    {
        if (model == null)
            throw new ArgumentNullException(nameof(model));
        _logger.LogInformation("ReadElement .Id:{Id}", model.Id);
        var element = _cleaningStorage.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(CleaningBindingModel model)
    {
        CheckModel(model);
        if (_cleaningStorage.Insert(model) != null) return true;
        _logger.LogWarning("Insert operation failed");
        return false;
    }

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

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

    private void CheckModel(CleaningBindingModel? model, bool withParams = true)
    {
        if (model == null)
            throw new ArgumentNullException(nameof(model));
        if (!withParams)
            return;
        if (model.RoomId < 0)
            throw new ArgumentException("RoomId must be more then 0");
    }
}