using adas_core.Application.Exceptions;
using adas_core.Application.Services.Interfaces;
using adas_core.Authentication.Attributes;
using adas_core.Domain.Enums;
using adas_core.Domain.Models.Filter;
using adas_core.Domain.Models.MongoModels;
using adas_core.Logging;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;

namespace adas_core.Controllers;

[Route("beacons")]
public class LightBeaconController : ControllerBase
{
    private readonly ILightBeaconService _lightBeaconService;
    private readonly IPatientService _patientService;


    public LightBeaconController(ILightBeaconService lightBeaconService, IPatientService patientService)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        _lightBeaconService = lightBeaconService;
        _patientService = patientService;
    }
    
    [HttpPost("paginated")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> GetPaginatedUnits([FromBody] PaginationFilter request)
    {
        if (request == null) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        var beacons = await _lightBeaconService.GetPaginatedBeacons(request);
        return Ok(beacons);
    }
    [HttpPost]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> CreateCamera([FromBody] LightBeacon beac)
    {
        var beacon = await _lightBeaconService.InsertOne(beac);
        return Ok(beacon);
    }
    [HttpPut("{id}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> UpdateCamera(string id, [FromBody] LightBeacon beacon)
    {
        if (!ObjectId.TryParse(id, out var idParsed)) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        beacon.Id  = idParsed;
        var cameraUpdated = await _lightBeaconService.UpdateOne(beacon);
        return Ok(cameraUpdated);
    }
    [HttpGet("{textToSearch}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> GetSearchByName(string textToSearch)
    {
        var beacon = await _lightBeaconService.GetSearchByName(textToSearch);
        return Ok(beacon);
    }
    [HttpPost("on/{color}")]
    public async Task<IActionResult> On([FromBody] string patientId, string color)
    {
        var parsed = ObjectId.TryParse(patientId, out var patientObjectId);
        if (!parsed) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestInvalidFormat);
        var patient = await _patientService.FindById(patientObjectId);
        if (patient == null || !patient.PointOfCareId.HasValue)
            throw new NotFoundException(HttpEnum.ErrorMessage.NotFoundResourceMissing);
        switch (color.ToUpper())
        {
            case "YELLOW":
                await _lightBeaconService.SendColor(patient.PointOfCareId.Value, LightBeaconColor.Yellow);
                break;
            case "BLUE":
                await _lightBeaconService.SendColor(patient.PointOfCareId.Value, LightBeaconColor.Blue);
                break;
            case "RED":
                await _lightBeaconService.SendColor(patient.PointOfCareId.Value, LightBeaconColor.Red);
                break;
            case "OFF":
                await _lightBeaconService.SendColor(patient.PointOfCareId.Value, LightBeaconColor.Off);
                break;
            case "GREEN":
                await _lightBeaconService.SendColor(patient.PointOfCareId.Value, LightBeaconColor.Green);
                break;
            case "CYAN":
                await _lightBeaconService.SendColor(patient.PointOfCareId.Value, LightBeaconColor.Cyan);
                break;
            case "MAGENTA":
                await _lightBeaconService.SendColor(patient.PointOfCareId.Value, LightBeaconColor.Magenta);
                break;
            case "WHITE":
                await _lightBeaconService.SendColor(patient.PointOfCareId.Value, LightBeaconColor.White);
                break;
        }

        return Ok();
    }


    [HttpPost("off")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public IActionResult Off([FromBody] string pocId)
    {
        var isParse = ObjectId.TryParse(pocId, out var pocObjectId);
        if (!isParse) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestInvalidFormat);
        _lightBeaconService.PowerOffLed(pocObjectId);

        return Ok();
    }
    [HttpPost("create")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> Create([FromBody] adas_core.Domain.Models.MongoModels.LightBeacon beacon)
    {
        var beaconInserted = await _lightBeaconService.InsertOne(beacon);
        if(beaconInserted == null) return Conflict();

        return Ok(beaconInserted);
    }
}