using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gehGassi.Dto;
using gehGassi.Dto.Banners;
using gehGassi.Dto.Common;
using gehGassi.Dto.News;
using gehGassiApp.Core.Interfaces;
using gehGassiApp.Core.Resources;
using gehGassiApp.Domain.Banners;
using gehGassiApp.Domain.Common;
using Microsoft.Maui.Devices.Sensors;
namespace gehGassiApp.Core.Services
{
///
/// Service der alle Funktionen für Banner zur Verfügung stellt
///
public class BannerService : IBannerService
{
private readonly ICommunicationService _communicationService;
private readonly ILocationService _locationService;
///
/// Erstellt eine Instanz
///
/// Instanz eines ICommunicationService
/// Instanz eines ILocationService
public BannerService(ICommunicationService communicationService, ILocationService locationService)
{
_communicationService = communicationService;
_locationService = locationService;
}
///
/// Gibt einen zufälligen Banner für eine Platzierung zurück
///
/// Bannerplatzierung
/// Optional: Id des Banners der gerade angezeigt wird
/// Aktuelle Position
/// Aktuelles Accesstoken
/// Akteuller Modus der App
/// Gewünschte Sprache
/// CancellationToken
/// ListCommunicationResult
public async Task> GetForLocationAsync(BannerLocation location, string currentBannerId, LocationDto currentLocation, string accessToken, AppMode appMode, string language, CancellationToken token)
{
var result = new CommunicationResult() { Value = null };
var query = new BannerQueryDto
{
AppMode = (AppModeDto)appMode,
Location = currentLocation,
BannerLocation = (BannerLocationDto)location,
CurrentBannerId = currentBannerId,
Language = language,
Take = 1,
Skip = 0
};
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var bannerResult = await _communicationService.GetBannerForLocationAsync(query, accessToken, token);
if (bannerResult.Success)
{
bannerResult.Value.Image += $"?t={DateTime.UtcNow.Ticks}"; //Um zu verhindern, dass ein Android-Image Error passiert...
return bannerResult;
}
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Hinzufügen von Clicks zu einem Banner
///
/// Id des Banners
/// Anzahl der Klicks
/// Akteuller Modus der App
/// Aktuelle Position
/// Gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult - true wenn noch gültig, false wenn der Banner nicht mehr gültig ist
public async Task> AddClicksAsync(string bannerId, int clicks, AppMode appMode, LocationDto location, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult() { Value = false };
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var dtoModel = new BannerClickDto()
{
AppMode = (AppModeDto)appMode,
ClickCount = clicks,
Id = bannerId,
Language = language,
Location = location
};
var bannerResult = await _communicationService.AddClicksAsync(dtoModel, accessToken, token);
if (bannerResult.Success)
return bannerResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
///
/// Hinzufügen von views zu einem Banner
///
/// Id des Banners
/// Anzahl der Views
/// Akteuller Modus der App
/// Aktuelle Position
/// Gewünschte Sprache
/// Aktuelles Accesstoken
/// CancellationToken
/// CommunicationResult - true wenn noch gültig, false wenn der Banner nicht mehr gültig ist
public async Task> AddViewsAsync(string bannerId, int views, AppMode appMode, LocationDto location, string language, string accessToken, CancellationToken token)
{
var result = new CommunicationResult() { Value = false };
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var dtoModel = new BannerViewDto()
{
AppMode = (AppModeDto)appMode,
ViewCount = views,
Id = bannerId,
Language = language,
Location = location
};
var bannerResult = await _communicationService.AddViewsAsync(dtoModel, accessToken, token);
if (bannerResult.Success)
return bannerResult;
}
else
{
result.Success = false;
result.ErrorCode = CommunicationErrors.ServerNoConnection;
result.ErrorMessage = Errors.Server_NoConnection;
}
return result;
}
}
}