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
{

//http://localhost:8000/api/Portfolio/GetTradesSymbol?&symbol=SPY
//http://73.245.214.234:8000/api/Portfolio/GetTradesSymbol?&symbol=SPY
  namespace MarketDataServer.Controllers
  {
    [ApiController]
    [Route("api/[controller]/[action]")]  
    public class PortfolioController : ControllerBase
    {
      [HttpGet(Name = "GetOpenPositionsWithDescription")]
			public IEnumerable<PositionWithDescription> GetOpenPositionsWithDescription(String token)
			{
        Profiler profiler = new Profiler();
        try
        {
          MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
          if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
          List<PositionWithDescription> positionsWithDescription=new List<PositionWithDescription>();
          PortfolioTrades openTrades=PortfolioDA.GetOpenTrades();
          List<Position> positions=openTrades.GetPositions(DateTime.Now);
          foreach(Position position in positions)
          {
            CompanyProfile companyProfile=CompanyProfileDA.GetCompanyProfile(position.Symbol);
            if(null==companyProfile.Description)companyProfile.Description="Description unvailable";
            positionsWithDescription.Add(new PositionWithDescription(position,companyProfile.CompanyName, companyProfile.Description));
          }
          return positionsWithDescription;
        }
        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 = "GetAccounts")]
      public IEnumerable<String> GetAccounts(String token)
      {
        Profiler profiler = new Profiler();
        try
        {
          MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
          if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
          return PortfolioDA.GetAccounts();
        }
        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 = "GetAccountsWithOpenTrades")]
      public IEnumerable<String> GetAccountsWithOpenTrades(String token)
      {
        Profiler profiler = new Profiler();
        try
        {
          MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
          if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
          return PortfolioDA.GetAccountsWithOpenTrades();
        }
        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 = "GetStopLimit")]
      public StopLimit GetStopLimit(String token,String symbol)
      {
        Profiler profiler = new Profiler();
        try
        {
          MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
          if(!Authorizations.GetInstance().IsAuthorized(token)) return null;
          StopLimit stopLimit=PortfolioDA.GetStopLimit(symbol);
          return stopLimit;
        }
        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 = "GetPortfolioTradesWithParityPrice")]
      public PortfolioTradesWithParityPrice GetPortfolioTradesWithParityPrice(String token, String symbol)
      {
        Profiler profiler = new Profiler();
        try
        {
          MDTrace.WriteLine(LogLevel.DEBUG,$"Start");
          if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
          Price zeroPrice=null;
          DateTime latestPricingDate=PricingDA.GetLatestDate(symbol);
          Price latestPrice = PricingDA.GetPrice(symbol, latestPricingDate);
          PortfolioTrades portfolioTrades = PortfolioDA.GetTradesSymbol(symbol);
          if (null == portfolioTrades) return null;
          portfolioTrades = portfolioTrades.GetOpenTrades();
          if (null == portfolioTrades) return null;
          PortfolioTrades portfolioTradesLots = LotAggregator.CombineLots(portfolioTrades);
          zeroPrice=ParityGenerator.GenerateGainLossValue(portfolioTrades,latestPrice);
          return new PortfolioTradesWithParityPrice(portfolioTradesLots,zeroPrice);
        }
        catch(Exception exception)
        {
          MDTrace.WriteLine(LogLevel.DEBUG,$"Exception:{exception.ToString()}");
          return null;
        }		
        finally
        {
          MDTrace.WriteLine(LogLevel.DEBUG,$"Done, total took {profiler.End()} (ms)");
        }        
      }
    }
  }
}
