using MarketData.MarketDataModel;
using MarketData.DataAccess;
using MarketDataServer.Authorization;
using MarketData;
using LogLevel = MarketData.LogLevel;
using Microsoft.AspNetCore.Mvc;
using MarketData.Utils;

namespace MarketDataServer.Controllers
{
  //http://localhost:8000/api/PreMarket/GetAvailableMarketDates?&market=S&P
  [ApiController]
  [Route("api/[controller]/[action]")]    
  public class PreMarketController: ControllerBase
  {
    [HttpGet(Name = "GetAvailableMarketDates")]  
    public IEnumerable<DateTime> GetAvailableMarketDates(String token,String market)
    {
      Profiler profiler = new Profiler();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
        if(!Authorizations.GetInstance().IsAuthorized(token)) return null;
        return PremarketDA.GetAvailableMarketDates(market);
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Exception:{exception.ToString()}");
        return null;
      }	
      finally
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Done, total took {profiler.End()} (ms)");
      }      
    }

    [HttpGet(Name = "GetAvailableMarkets")] 
    public IEnumerable<String> GetAvailableMarkets(String token)
    {
      Profiler profiler = new Profiler();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
        if(!Authorizations.GetInstance().IsAuthorized(token)) return null;
        return PremarketDA.GetDistinctMarkets();
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Exception:{exception.ToString()}");
        return null;
      }	
      finally
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Done, total took {profiler.End()} (ms)");
      }      
    }

    [HttpGet(Name = "GetLatestPremarketData")] 
    public IEnumerable<PremarketElement> GetLatestPremarketData(String token,String market,DateTime marketDate)
    {
      Profiler profiler = new Profiler();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
        if(!Authorizations.GetInstance().IsAuthorized(token)) return null;
        return PremarketDA.GetLatestPremarketData(market,marketDate);
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Exception:{exception.ToString()}");
        return null;
      }	
      finally
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Done, total took {profiler.End()} (ms)");
      }      
    }
  }
}

