using System.Collections;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace server.Controllers;

/// <summary>
/// 矩阵键控制器，用于管理矩阵键的启用、禁用和状态设置
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class PowerController : ControllerBase
{
    private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

    /// <summary>
    /// [TODO:description]
    /// </summary>
    /// <param name="address">[TODO:parameter]</param>
    /// <param name="port">[TODO:parameter]</param>
    /// <param name="enable">[TODO:parameter]</param>
    /// <returns>[TODO:return]</returns>
    [HttpPost("SetPowerOnOff")]
    [EnableCors("Users")]
    [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
    public async ValueTask<IResult> SetPowerOnOff(string address, int port, bool enable)
    {
        var powerCtrl = new Peripherals.PowerClient.Power(address, port);
        var ret = await powerCtrl.SetPowerOnOff(enable);

        if (ret.IsSuccessful)
        {
            var powerStatus = enable ? "ON" : "OFF";
            logger.Info($"Set device {address}:{port.ToString()} power {powerStatus} finished: {ret.Value}.");
            return TypedResults.Ok(ret.Value);
        }
        else
        {
            logger.Error(ret.Error);
            return TypedResults.InternalServerError(ret.Error);
        }
    }

}

