using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RobotNet.MapManager.Data;
using RobotNet.MapShares.Dtos;
using RobotNet.Shares;

namespace RobotNet.MapManager.Controllers;

[Route("api/[controller]")]
[ApiController]
[Authorize]
public class MapsSettingController(MapEditorDbContext MapDb) : ControllerBase
{
    [HttpGet]
    [Route("{id}")]
    public async Task<MessageResult<MapSettingDefaultDto>> GetMapSetting(Guid id)
    {
        var map = await MapDb.Maps.FirstOrDefaultAsync(m => m.Id == id);
        if (map == null) return new(false, $"Không tìm thấy map {id}");

        return new(true)
        {
            Data = new MapSettingDefaultDto()
            {
                Id = map.Id,
                EdgeStraightMaxSpeed = map.EdgeStraightMaxSpeedDefault,
                EdgeCurveMaxSpeed = map.EdgeCurveMaxSpeedDefault,
                EdgeMaxHeight = map.EdgeMaxHeightDefault,
                EdgeMinHeight = map.EdgeMinHeightDefault,
                EdgeMinLength = map.EdgeMinLengthDefault,
                EdgeDirectionAllowed = map.EdgeDirectionAllowedDefault,
                EdgeMaxRotationSpeed = map.EdgeMaxRotationSpeedDefault,
                EdgeRotationAllowed = map.EdgeRotationAllowedDefault,
                EdgeAllowedDeviationXy = map.EdgeAllowedDeviationXyDefault,
                EdgeAllowedDeviationTheta = map.EdgeAllowedDeviationThetaDefault,

                NodeAllowedDeviationTheta = map.NodeAllowedDeviationThetaDefault,
                NodeAllowedDeviationXy = map.NodeAllowedDeviationXyDefault,
                NodeNameAutoGenerate = map.NodeNameAutoGenerate,
                NodeNameTemplate = map.NodeNameTemplateDefault,

                ZoneMinSquare = map.ZoneMinSquareDefault,
            },
        };
    }


    [HttpPut]
    [Route("")]
    public async Task<MessageResult> Update([FromBody] MapSettingDefaultDto mapSetting)
    {
        var map = await MapDb.Maps.FirstOrDefaultAsync(m => m.Id == mapSetting.Id);
        if (map == null) return new(false, $"Không tìm thấy map {mapSetting.Id}");

        map.EdgeStraightMaxSpeedDefault = mapSetting.EdgeStraightMaxSpeed;
        map.EdgeCurveMaxSpeedDefault = mapSetting.EdgeCurveMaxSpeed;
        map.EdgeMaxHeightDefault = mapSetting.EdgeMaxHeight;
        map.EdgeMinHeightDefault = mapSetting.EdgeMinHeight;
        map.EdgeMinLengthDefault = mapSetting.EdgeMinLength;
        map.EdgeDirectionAllowedDefault = mapSetting.EdgeDirectionAllowed;
        map.EdgeMaxRotationSpeedDefault = mapSetting.EdgeMaxRotationSpeed;
        map.EdgeRotationAllowedDefault = mapSetting.EdgeRotationAllowed;
        map.EdgeAllowedDeviationXyDefault = mapSetting.EdgeAllowedDeviationXy;
        map.EdgeAllowedDeviationThetaDefault = mapSetting.EdgeAllowedDeviationTheta;

        map.NodeAllowedDeviationThetaDefault = mapSetting.NodeAllowedDeviationTheta;
        map.NodeAllowedDeviationXyDefault = mapSetting.NodeAllowedDeviationXy;
        map.NodeNameAutoGenerate = mapSetting.NodeNameAutoGenerate;
        map.NodeNameTemplateDefault = mapSetting.NodeNameTemplate;

        map.ZoneMinSquareDefault = mapSetting.ZoneMinSquare;

        MapDb.Maps.Update(map);
        await MapDb.SaveChangesAsync();

        return new(true);
    }
}
