using System;
using System.Threading;
using CityOfHeroes;
using HeroStats.StatsEngine;

namespace ExtendEngineConsole_CS
{
   /// <summary>
   /// Demo class for using the statistics engine in your own application.
   /// 
   /// Note that this project has a post-build event that copies ChatMessageregexs.dat
   /// into the run directory. That file needs to be in the same directory as the
   /// executable in order to get the regular expressions that the ChatMessage class
   /// uses to parse the messages that come in from the CoH client.
   /// </summary>
   class ConsoleMain
    {
      private static ManualResetEvent logoutEvent = new ManualResetEvent(false);

        [STAThread]
        static void Main()
        {
         // When using the StatsEngine object, you'll use a singleton and just
         // hook its events
         // To use this object, you'll need StatsEngine.dll and CityOfHeroesData.dll referenced
           Singletons.Statistics.OnHeroLogin += new HeroStats.StatsEngine.StatsEngine.HeroLoginHandler(Statistics_OnHeroLogin);
         Singletons.Statistics.OnHeroLogout += new HeroStats.StatsEngine.StatsEngine.HeroLogoutHandler(Statistics_OnHeroLogout);
         
         // the Singletons class exposes other objects you can hook, like the scanner
         // To use this, you'll need the CityOfHeroes.dll referenced
         Singletons.Scanner.OnChatMessage += new CityOfHeroes.Scanner.ChatMessageEventHandler(Scanner_OnChatMessage);

         // let's also hook into the "heartbeat" timer, so we can periodically display
         // some information that may change in the background
         Singletons.Statistics.OnGameTimerTick += new HeroStats.StatsEngine.StatsEngine.GameTimerTickHandler(Statistics_OnGameTimerTick);
         
         // Make sure to start the ball rolling
         Singletons.Statistics.Scanning = true;
         // alternatively, could have used Singletons.Scanner.Start()

         // for this example, we'll wait for a logout and then terminate
         logoutEvent.WaitOne();

         // not absolutely necessary to turn off the scanner, but it's polite
         Singletons.Statistics.Scanning = false;
      }

      private static void Statistics_OnHeroLogin(CityOfHeroes.HeroData hero)
      {
         Console.WriteLine("*** {0} Logged In", hero.Name);
      }

      private static void Statistics_OnHeroLogout(CityOfHeroes.HeroData hero)
      {
         Console.WriteLine("*** {0} Logged Out", hero.Name);
         logoutEvent.Set();
      }

      private static void Scanner_OnChatMessage(CityOfHeroes.ChatMessage message)
      {
         Console.WriteLine("At {0}: {1}", message.Timestamp, message.Message);
      }

      private static void Statistics_OnGameTimerTick(double elapsedSeconds)
      {
         // let's just output the current experience for our online hero. If we're
         // not online, output nothing

         // grab a reference once, since a call to OnlineHero will recreate a new
         // HeroData object everytime it's called
         HeroData onlineHero = Singletons.OnlineHero;

         if (onlineHero != null)
            Console.WriteLine("--- {0} has {1} experience", onlineHero.Name, onlineHero.Experience);
      }
   }
}