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

namespace MarketDataServer.Controllers
{
  [ApiController]
  [Route("api/[controller]/[action]")]
  public class HeadlinesController : ControllerBase
  {
    [HttpGet(Name = "GetLatestHeadlines")]  
    public IEnumerable<Headline> GetLatestHeadlines(String token)
    {
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[{0:G}][HeadlinesController::GetLatestHeadlines]", DateTime.Now));
      if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
      return HeadlinesDA.GetLatestHeadlines();
    }

    [HttpGet(Name = "GetHeadlineDates")]  
    public IEnumerable<String> GetHeadlineDates(String token)
    {
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[{0:G}][HeadlinesController::GetHeadlineDates]", DateTime.Now));
      if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
      List<string> headlineDates = HeadlinesDA.GetHeadlineDates();
      if(headlineDates.Count>0)headlineDates=headlineDates.Take(252).ToList();
      return headlineDates;
    }
    
    [HttpGet(Name = "GetHeadlines")]    
    public IEnumerable<Headline> GetHeadlines(String token,DateTime headlineDate)
    {
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[{0:G}][HeadlinesController::GetHeadliness(DateTime headlineDate)]", DateTime.Now));
      if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
      return HeadlinesDA.GetHeadlines(headlineDate);
    }
  }
}
