using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using MarketData.MarketDataModel;
using MarketData.DataAccess;
using MarketDataServer.Authorization;
using MarketData.Generator;
using MarketData.Generator.GainLoss;

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
  {
    public class PortfolioController : ApiController
    {
			public IEnumerable<PositionWithDescription> GetOpenPositionsWithDescription(String token)
			{
        Console.WriteLine(String.Format("[{0:G}][PortfolioController::GetOpenPositionsWithDescriptionAsOf]",DateTime.Now));
        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;
			} 
      public IEnumerable<String> GetAccounts(String token)
      {
        Console.WriteLine(String.Format("[{0:G}][PortfolioController::GetAccounts]",DateTime.Now));
        if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
        return PortfolioDA.GetAccounts();
      }
      public IEnumerable<String> GetAccountsWithOpenTrades(String token)
      {
        Console.WriteLine(String.Format("[{0:G}][PortfolioController::GetAccountsWithOpenTrades]", DateTime.Now));
        if (!Authorizations.GetInstance().IsAuthorized(token)) return null;
        return PortfolioDA.GetAccountsWithOpenTrades();
      }
      public StopLimit GetStopLimit(String token,String symbol)
      {
        Console.WriteLine(String.Format("[{0:G}][PortfolioController::GetStopLimit]",DateTime.Now));
        if(!Authorizations.GetInstance().IsAuthorized(token)) return null;
        StopLimit stopLimit=PortfolioDA.GetStopLimit(symbol);
        return stopLimit;
      }
      public PortfolioTradesWithParityPrice GetPortfolioTradesWithParityPrice(String token, String symbol)
      {
        Console.WriteLine(String.Format("[{0:G}][PortfolioController::GetPortfolioTradesWithParityPrice]", DateTime.Now));
        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);
      }
    }
  }
}
