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

namespace MarketDataServer.Controllers
{
  [ApiController]
  [Route("api/[controller]/[action]")]
  public class PriceIndexController : ControllerBase
	{
//http://localhost:8000/api/PriceIndex/GetDistnctPriceIndices
		[HttpGet(Name = "GetDistinctPriceIndices")]
		public IEnumerable<String> GetDistinctPriceIndices(String token)
		{
		  Profiler profiler = new Profiler();
		  try
			{
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
				if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
				return ConsumerPriceIndexDA.GetDistinctIndices();
			}
      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 = "GetConsumerPriceIndex")]
		public IEnumerable<PriceIndex> GetConsumerPriceIndex(String token,String indexCode)
		{
		  Profiler profiler = new Profiler();
		  try
			{
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
				if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
				PriceIndices priceIndices=ConsumerPriceIndexDA.GetConsumerPriceIndex(indexCode);
				return priceIndices;
			}
			catch(Exception exception)
			{
        MDTrace.WriteLine(LogLevel.DEBUG,$"Exception:{exception.ToString()}");
        return null;
			}
      finally
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Done, total took {profiler.End()} (ms)");
      }			
		} 
	}
}
