using System;

namespace ExtendScannerConsole_CS
{
    /// <summary>
    /// Demo class for using the scanner 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
    {
      // our scanner variable. Could have been declared in Main, but let's put it
      // here to mirror the VB.NET example
      private static CityOfHeroes.Scanner.CohScanner scanner;

        [STAThread]
        static void Main()
        {
         // first, instantiate the scanner object
           scanner = new CityOfHeroes.Scanner.CohScanner();

         // now initialize the chat message subsystem. Note that these last two 
         // items don't have to be done in this order
         //
         // Also note that, strictly speaking, this doesn't have to be called. If
         // you fail to do so, the chat message subsystem will initialize the first
         // time it's needed. Unfortunately, that can take some time (up to 10 seconds
         // or so), so it's better to initialize it up front when you're doing the rest
         // of your application setup so as to not have an ugly pause in the processing
         // of your first chat message
           CityOfHeroes.ChatMessage.Initialize();

         // by default, the scanner will scan the chat channels that contain the 
         // information it needs. You can add additional channels as desired
         //
         // e.g., Broadcast is not selected by default, but let's get its messages too.
         scanner.BroadcastChannel = true;

         // now let's register for some events
         scanner.OnHeroLogin += new CityOfHeroes.Scanner.HeroLoginHandler(scanner_OnHeroLogin);
         scanner.OnHeroLogout += new CityOfHeroes.Scanner.HeroLogoutHandler(scanner_OnHeroLogout);
         scanner.OnScanningError += new CityOfHeroes.Scanner.ScanningErrorHandler(scanner_OnScanningError);
         scanner.OnScanningPaused += new CityOfHeroes.Scanner.ScanningInactiveHandler(scanner_OnScanningPaused);
         scanner.OnScanningResumed += new CityOfHeroes.Scanner.ScanningActiveHandler(scanner_OnScanningResumed);
         scanner.OnScanningStart += new CityOfHeroes.Scanner.ScanningStartHandler(scanner_OnScanningStart);
         scanner.OnScanningStop += new CityOfHeroes.Scanner.ScanningStopHandler(scanner_OnScanningStop);
         scanner.OnChatMessage += new CityOfHeroes.Scanner.ChatMessageEventHandler(scanner_OnChatMessage);

         // we're all set up. Start the scanner
         scanner.Start();

         // let's manipulate the scanner a bit now
         Console.WriteLine("Hit return to pause the scanner...");
         Console.ReadLine();
         scanner.Pause();

         Console.WriteLine("Hit return to resume scanning...");
         Console.ReadLine();
         scanner.Resume();

         Console.WriteLine("Hit return to quit...");
         Console.ReadLine();
         scanner.Stop();
      }

      #region Scanner Event Handlers
      /// <summary>
      /// Handles an event that gets fired when a new hero logs into CoH
      /// </summary>
      /// <param name="hero">Information about the hero logging in</param>
      private static void scanner_OnHeroLogin(CityOfHeroes.HeroData hero)
      {
         Console.WriteLine("Hero '{0}' just logged in", hero.Name);
      }

      /// <summary>
      /// Handles an event that gets fired when a new hero logs out of CoH
      /// </summary>
      /// <param name="hero">Information about the hero logging out</param>
      private static void scanner_OnHeroLogout(CityOfHeroes.HeroData hero)
      {
         Console.WriteLine("Hero '{0}' just logged out", hero.Name);
      }

      /// <summary>
      /// Handles an event that gets fired when an error occurs in the scanner
      /// </summary>
      /// <param name="msg">Details of the error. Also stored in CohScanner.Error</param>
      private static void scanner_OnScanningError(string msg)
      {
         Console.WriteLine("A scanning error occurred. {0}", msg);
      }

      /// <summary>
      /// Handles an event that gets fired when the scanner is programmatically paused
      /// </summary>
      private static void scanner_OnScanningPaused()
      {
         Console.WriteLine("The scanner has been paused");
      }

      /// <summary>
      /// Handles an event that gets fired when the scanner is programmatically resumed
      /// after a pause
      /// </summary>
      private static void scanner_OnScanningResumed()
      {
         Console.WriteLine("Scanning is resumed");
      }

      /// <summary>
      /// Handles an event that gets fired when the scanner has started. Its first action
      /// is to try to find the CoH client. Once it finds it, it switches to reading the
      /// chat messages. Note that both of these activities are termed "scanning"
      /// </summary>
      private static void scanner_OnScanningStart()
      {
         Console.WriteLine("The scanner has been started");
      }

      /// <summary>
      /// Handles an event that gets fired when the scanner has been told to stop scanning.
      /// </summary>
      private static void scanner_OnScanningStop()
      {
         Console.WriteLine("The scanner has been stopped");
      }

      /// <summary>
      /// Handles an event that gets fired when a chat message has been detected.
      /// </summary>
      /// <param name="message">The incoming message</param>
      private static void scanner_OnChatMessage(CityOfHeroes.ChatMessage message)
      {
         Console.WriteLine("ChatMessage from {0} at {1}: {2}", message.Channel, message.Timestamp, message.Message);
         Console.WriteLine("   Category: {0}", message.Category);
      }

      #endregion
   }
}