using MarketData.DataAccess;
using MarketData.MarketDataModel;
using MarketData.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarketData.Generator.CMTrend
{
  public class CMTTrendGenerator
  {
    private CMTTrendGenerator()
    {
    }
    public static CMTGeneratorResult GenerateCMTCandidates(DateTime tradeDate,CMTParams cmtParams,List<String> symbolsHeld)
    {
      CMTGeneratorResult cmtGeneratorResult=new CMTGeneratorResult();
      try
      {
        List<String> symbols=PricingDA.GetSymbols();
        for(int index=0;index<symbols.Count;index++)
        {
          String symbol=symbols[index];
          if(0==(index%1000)) Console.WriteLine("Processing item {0} of {1}",index+1,symbols.Count);
          CMTCandidate cmtCandidate=CMTCandidateGenerator.GenerateCandidate(symbol,tradeDate,cmtParams,symbolsHeld);
          if(null==cmtCandidate) continue;
          if(cmtCandidate.Violation) cmtGeneratorResult.CMTCandidatesWithViolation.Add(cmtCandidate);
          else cmtGeneratorResult.CMTCandidates.Add(cmtCandidate);
        }
        if(null==cmtGeneratorResult.CMTCandidates||0==cmtGeneratorResult.CMTCandidates.Count)
        {
          MDTrace.WriteLine(LogLevel.DEBUG,String.Format("GenerateCMTCandidates.  No candidates generated for trade date: {0}",tradeDate));
          cmtGeneratorResult.Success=true;
          return cmtGeneratorResult;
        }
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Generated {0} candidates.",cmtGeneratorResult.CMTCandidates.Count));
        cmtGeneratorResult.CMTCandidates.Sort();
        MDTrace.WriteLine(LogLevel.DEBUG,CMTCandidate.Header());
        foreach(CMTCandidate candidate in cmtGeneratorResult.CMTCandidates) MDTrace.WriteLine(LogLevel.DEBUG,candidate.ToString());
        cmtGeneratorResult.Success=true;
        return cmtGeneratorResult;
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
        cmtGeneratorResult.Success=false;
        cmtGeneratorResult.Messages.Add(exception.ToString());
        return cmtGeneratorResult;
      }
    }
  }
}

