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

namespace MarketDataServer.Controllers
{
  [ApiController]
  [Route("api/[controller]/[action]")]
//http://localhost:8000/api/Price
//http://localhost:8000/api/Price/GetPrices?&symbol=MIDD&days=5
//http://73.245.214.234:8000/api/Price/GetPrices?&symbol=MIDD&days=5

  public class PriceController : ControllerBase
  {
    [HttpGet(Name = "GetPrices")]
    public IEnumerable<Price> GetPrices(String token, String symbol, int days)
    {
      Profiler profiler = new Profiler();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
        if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
        return PricingDA.GetPrices(symbol, days);
      }
      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 = "GetBollingerBands")]
    public BollingerBands GetBollingerBands(String token,String symbol,int dayCount)
    {
      Profiler profiler = new Profiler();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
        if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
        Prices prices=PricingDA.GetPrices(symbol,dayCount);
        BollingerBands bollingerBands=BollingerBandGenerator.GenerateBollingerBands(prices);
        return bollingerBands;
      }
      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 = "GetLatestPricingDate")]
    public DateTime GetLatestPricingDate(String token)
    {
      Profiler profiler = new Profiler();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
        if (!Authorizations.GetInstance().IsAuthorized(token)) return DateTime.MinValue;
        DateTime latestPricingDate=PricingDA.GetLatestDate();
        return latestPricingDate;
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Exception:{exception.ToString()}");
        return Utility.Epoch;
      }	      
      finally
      {
        MDTrace.WriteLine(LogLevel.DEBUG,$"Done, total took {profiler.End()} (ms)");
      }      
    }

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