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
{
  [ApiController]
  [Route("api/[controller]/[action]")]
  public class HeadlinesController : ControllerBase
  {
    [HttpGet(Name = "GetLatestHeadlines")]  
    public IEnumerable<Headline> GetLatestHeadlines(String token)
    {
      Profiler profiler = new Profiler();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
        if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
        return HeadlinesDA.GetLatestHeadlines();
      }
      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 = "GetHeadlineDates")]  
    public IEnumerable<String> GetHeadlineDates(String token)
    {
      Profiler profiler = new Profiler();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
        if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
        List<string> headlineDates = HeadlinesDA.GetHeadlineDates();
        if(headlineDates.Count>0)headlineDates=headlineDates.Take(252).ToList();
        return headlineDates;
      }
      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 = "GetHeadlines")]    
    public IEnumerable<Headline> GetHeadlines(String token,DateTime headlineDate)
    {
      Profiler profiler = new Profiler();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
        if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
        return HeadlinesDA.GetHeadlines(headlineDate);
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Exception:{exception.ToString()}");
        return null;
      }
      finally
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Done, total took {profiler.End()} (ms)");
      }    }
  }
}
