using System;
using Interfaces;
using Nancy;

namespace RaceLapTimer.ApiControllers
{
    public class LapTrackApiModule : NancyModule
    {
        private readonly IDbProvider _dbProvider;
        public LapTrackApiModule(IDbProvider provider) : base("/api/v1/lap_track")
        {
            _dbProvider = provider;
            Post["/create"] = args => LogLapTime();
        }

        private dynamic LogLapTime()
        {
            var transponderToken = Convert.ToInt32(Request.Query["transponder_token"]);
            var lapTimeInMs = Convert.ToInt32(Request.Query["lap_time_in_ms"]);
            var log = new SatelliteLog
            {
                TransponderToken = transponderToken,
                LapTimeMs = lapTimeInMs
            };
            _dbProvider.StoreTransponderLog(log);
            return HttpStatusCode.OK;
        }
        /*
         a non-competition race will result in: 66f92adaf43f8f6ad96204ec being returned?
         a competition race will result in:
            {
              "id": 7,
              "created_at": "2017-05-10T19:31:27.517Z",
              "created_at_timestamp": 1494444687,
              "lap_num": 2,
              "lap_time": 95000,
              "pilot_id": 2,
              "pilot": {
                "name": "test2",
                "quad": "testste",
                "team": "tetststs"
              },
              "race_session_id": 6,
              "race_session": {
                "title": "Competition 1",
                "mode": "competition"
              }
            }

         */

    }
}
