using System;
using Interfaces;
using Nancy;

namespace RaceLapTimer.ApiControllers
{
    public class SatelliteApiModule:NancyModule
    {
        private readonly IDbProvider _provider;
        private readonly ILoggerService _logger;

        public SatelliteApiModule(IDbProvider provider, ILoggerService logger) : base("/api/v1")
        {
            _provider = provider;
            _logger = logger;
            //Post["/lap_track/create"] = args => LogNewLapSatelliteTime();
            Get["/lap_track/create"] = args => LogNewLapSatelliteTime();
        }

        private dynamic LogNewLapSatelliteTime()
        {
            var transponderToken = Convert.ToInt32(Request.Query["transponder_token"].Value);
            var lapTimeMs = Convert.ToInt32(Request.Query["lap_time_in_ms"].Value);
            var logObj = new SatelliteLog
            {
                TransponderToken = transponderToken,
                LapTimeMs = lapTimeMs
            };
            _logger.Trace("New Lap Time for Transponder Token: {0}. Lap Time: {1}", transponderToken, lapTimeMs);
            _provider.StoreTransponderLog(logObj);
            return HttpStatusCode.OK;
        }
    }
}
