using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ReallifeGamemode.Database.Models;

namespace ReallifeGamemode.DataService.Controllers
{
  [ApiController]
  [Authorize]
  [Produces("application/json")]
  [Route("DataService/[controller]")]
  public class LocationController : ControllerBase
  {
    private readonly DatabaseContext dbContext;

    public LocationController(DatabaseContext dbContext)
    {
      this.dbContext = dbContext;
    }

    [HttpGet("GetLocations")]
    public ActionResult<IEnumerable<string>> GetLocations()
    {
      var list = this.dbContext.Locations.OrderByDescending(l => l.Id);

      List<string> locations = new List<string>();

      foreach (var location in list)
      {
        string description = location.Description;
        description = Regex.Replace(description, @"<[^>]*(>|$)", string.Empty);
        description = Regex.Replace(description, @"[\s\r\n]+", " ").Trim();
        locations.Add($"{description.PadRight(20)} - {Math.Round(location.X, 2).ToString(CultureInfo.InvariantCulture)}, {Math.Round(location.Y, 2).ToString(CultureInfo.InvariantCulture)}, {Math.Round(location.Z, 2).ToString(CultureInfo.InvariantCulture)} - Richtung: {Math.Round(location.Heading, 2)}");
      }

      return locations;
    }
  }
}
