using HoneyBox.Core.Interfaces;
using HoneyBox.Model.Data;
using HoneyBox.Model.Models.FloatBall;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace HoneyBox.Core.Services;

/// <summary>
/// 悬浮球服务实现
/// </summary>
public class FloatBallService : IFloatBallService
{
    private readonly HoneyBoxDbContext _dbContext;
    private readonly ILogger<FloatBallService> _logger;

    public FloatBallService(
        HoneyBoxDbContext dbContext,
        ILogger<FloatBallService> logger)
    {
        _dbContext = dbContext;
        _logger = logger;
    }

    /// <inheritdoc />
    public async Task<List<FloatBallResponse>> GetEnabledFloatBallsAsync()
    {
        try
        {
            var floatBalls = await _dbContext.FloatBallConfigs
                .Where(f => f.Status == 1)
                .Select(f => new FloatBallResponse
                {
                    Id = f.Id,
                    Type = f.Type,
                    Image = f.Image,
                    LinkUrl = f.LinkUrl,
                    PositionX = f.PositionX,
                    PositionY = f.PositionY,
                    Width = f.Width,
                    Height = f.Height,
                    Effect = f.Effect,
                    Title = f.Title,
                    ImageDetails = f.ImageDetails,
                    ImageBj = f.ImageBj,
                    ImageDetailsX = f.ImageDetailsX,
                    ImageDetailsY = f.ImageDetailsY,
                    ImageDetailsW = f.ImageDetailsW,
                    ImageDetailsH = f.ImageDetailsH
                })
                .ToListAsync();

            return floatBalls;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get enabled float ball configurations");
            throw;
        }
    }
}
