using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RobotNet.RobotManager.Services;
using RobotNet.RobotManager.Services.OpenACS;
using RobotNet.RobotShares.OpenACS;
using RobotNet.Shares;

namespace RobotNet.RobotManager.Controllers;

[Route("api/[controller]")]
[ApiController]
[Authorize]
public class OpenACSSettingsController(OpenACSManager OpenACSManager, LoggerController<OpenACSSettingsController> Logger) : ControllerBase
{
    [HttpGet]
    public Task<MessageResult<OpenACSSettingsDto>> GetOpenACSSettings()
    {
        try
        {
            var settings = new OpenACSSettingsDto
            {
                PublishSetting = new(OpenACSManager.PublishEnable, OpenACSManager.PublishURL, OpenACSManager.PublishURLUsed, OpenACSManager.PublishInterval),
                TrafficSetting = new(OpenACSManager.TrafficEnable, OpenACSManager.TrafficURL, OpenACSManager.TrafficURLUsed)
            };
            return Task.FromResult<MessageResult<OpenACSSettingsDto>>(new(true, "") { Data = settings });
        }
        catch (Exception ex)
        {
            Logger.Warning($"Lấy cấu hình OpenACS xảy ra lỗi: {ex.Message}");
            return Task.FromResult<MessageResult<OpenACSSettingsDto>>(new(false, "Hệ thống có lỗi xảy ra"));
        }
    }

    [HttpPost]
    [Route("publish")]
    public async Task<MessageResult<OpenACSPublishSettingDto>> UpdatePublishSetting([FromBody] OpenACSPublishSettingModel settingModel)
    {
        try
        {
            await OpenACSManager.UpdatePublishURL(settingModel.Url);
            await OpenACSManager.UpdatePublishInterval(settingModel.Interval);
            return new(true, "") { Data = new(OpenACSManager.PublishEnable, OpenACSManager.PublishURL, OpenACSManager.PublishURLUsed, OpenACSManager.PublishInterval) };
        }
        catch (Exception ex)
        {
            Logger.Warning($"Cập nhật cấu hình publish OpenACS xảy ra lỗi: {ex.Message}");
            return new(false, "Hệ thống có lỗi xảy ra");
        }
    }

    [HttpPost]
    [Route("traffic")]
    public async Task<MessageResult<OpenACSTrafficSettingDto>> UpdateTrafficSetting([FromBody] OpenACSTrafficSettingModel settingModel)
    {
        try
        {
            await OpenACSManager.UpdateTrafficURL(settingModel.Url);
            return new(true, "") { Data = new(OpenACSManager.TrafficEnable, OpenACSManager.TrafficURL, OpenACSManager.TrafficURLUsed) };
        }
        catch (Exception ex)
        {
            Logger.Warning($"Cập nhật cấu hình traffic OpenACS xảy ra lỗi: {ex.Message}");
            return new(false, "Hệ thống có lỗi xảy ra");
        }
    }

    [HttpGet]
    [Route("publish")]
    public async Task<MessageResult> UpdatePublishEnable([FromQuery] bool enable)
    {
        try
        {
            await OpenACSManager.UpdatePublishEnable(enable);
            return new(true, "");
        }
        catch (Exception ex)
        {
            Logger.Warning($"Cập nhật cấu hình publish OpenACS xảy ra lỗi: {ex.Message}");
            return new(false, "Hệ thống có lỗi xảy ra");
        }
    }

    [HttpGet]
    [Route("publish/count")]
    [AllowAnonymous]
    public int GetStatusPublishCount([FromServices] OpenACSPublisher publisher) => publisher.PublishCount;

    [HttpGet]
    [Route("traffic")]
    public async Task<MessageResult> UpdateTrafficEnable([FromQuery] bool enable)
    {
        try
        {
            await OpenACSManager.UpdateTrafficEnable(enable);
            return new(true, "");
        }
        catch (Exception ex)
        {
            Logger.Warning($"Cập nhật cấu hình publish OpenACS xảy ra lỗi: {ex.Message}");
            return new(false, "Hệ thống có lỗi xảy ra");
        }
    }
}
