using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace server.Controllers;

/// <summary>
/// [TODO:description]
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class DDSController : 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="channelNum">[TODO:parameter]</param>
    /// <param name="waveNum">[TODO:parameter]</param>
    /// <returns>[TODO:return]</returns>
    [HttpPost("SetWaveNum")]
    [EnableCors("Users")]
    [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ArgumentException), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
    public async ValueTask<IResult> SetWaveNum(string address, int port, int channelNum, int waveNum)
    {
        var dds = new Peripherals.DDSClient.DDS(address, port);

        var ret = await dds.SetWaveNum(channelNum, waveNum);
        if (ret.IsSuccessful)
        {
            logger.Info($"Device {address} set output wave num successfully");
            return TypedResults.Ok(ret.Value);
        }
        else
        {
            logger.Error(ret.Error);
            return TypedResults.InternalServerError(ret.Error);
        }
    }

    /// <summary>
    /// [TODO:description]
    /// </summary>
    /// <param name="address">[TODO:parameter]</param>
    /// <param name="port">[TODO:parameter]</param>
    /// <param name="channelNum">[TODO:parameter]</param>
    /// <param name="waveNum">[TODO:parameter]</param>
    /// <param name="step">[TODO:parameter]</param>
    /// <returns>[TODO:return]</returns>
    [HttpPost("SetFreq")]
    [EnableCors("Users")]
    [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ArgumentException), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
    public async ValueTask<IResult> SetFreq(string address, int port, int channelNum, int waveNum, UInt32 step)
    {
        var dds = new Peripherals.DDSClient.DDS(address, port);

        var ret = await dds.SetFreq(channelNum, waveNum, step);
        if (ret.IsSuccessful)
        {
            logger.Info($"Device {address} set output freqency successfully");
            return TypedResults.Ok(ret.Value);
        }
        else
        {
            logger.Error(ret.Error);
            return TypedResults.InternalServerError(ret.Error);
        }
    }

    /// <summary>
    /// [TODO:description]
    /// </summary>
    /// <param name="address">[TODO:parameter]</param>
    /// <param name="port">[TODO:parameter]</param>
    /// <param name="channelNum">[TODO:parameter]</param>
    /// <param name="waveNum">[TODO:parameter]</param>
    /// <param name="phase">[TODO:parameter]</param>
    /// <returns>[TODO:return]</returns>
    [HttpPost("SetPhase")]
    [EnableCors("Users")]
    [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ArgumentException), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
    public async ValueTask<IResult> SetPhase(string address, int port, int channelNum, int waveNum, int phase)
    {
        var dds = new Peripherals.DDSClient.DDS(address, port);

        var ret = await dds.SetPhase(channelNum, waveNum, phase);
        if (ret.IsSuccessful)
        {
            logger.Info($"Device {address} set output phase successfully");
            return TypedResults.Ok(ret.Value);
        }
        else
        {
            logger.Error(ret.Error);
            return TypedResults.InternalServerError(ret.Error);
        }

    }

}
