using System;
using System.Windows.Forms;
using CityOfHeroes;
using HeroStats.StatsEngine;

namespace ExtendEngineWinForms_CS
{
   /// <summary>
   /// Demo form 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>
   public class MainForm : System.Windows.Forms.Form
    {
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.ListBox listBox1;
        private System.ComponentModel.Container components = null;

        public MainForm()
        {
            InitializeComponent();

         // 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);
         
         // Since we're a WinForms application, we need to set a Control object as the
         // synchronizing element for inter-thread message processing
         Singletons.SynchronizingUIElement = this;

         // Make sure to start the ball rolling
         Singletons.Statistics.Scanning = true;
         // alternatively, could have used Singletons.Scanner.Start()
      }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
         this.label1 = new System.Windows.Forms.Label();
         this.listBox1 = new System.Windows.Forms.ListBox();
         this.SuspendLayout();
         // 
         // label1
         // 
         this.label1.Dock = System.Windows.Forms.DockStyle.Top;
         this.label1.Location = new System.Drawing.Point(0, 0);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(368, 23);
         this.label1.TabIndex = 0;
         this.label1.Text = "Output:";
         this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
         // 
         // listBox1
         // 
         this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
         this.listBox1.Location = new System.Drawing.Point(0, 23);
         this.listBox1.Name = "listBox1";
         this.listBox1.Size = new System.Drawing.Size(368, 251);
         this.listBox1.TabIndex = 1;
         // 
         // MainForm
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(368, 278);
         this.Controls.Add(this.listBox1);
         this.Controls.Add(this.label1);
         this.Name = "MainForm";
         this.Text = "Sample Form";
         this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
         this.ResumeLayout(false);

      }
        #endregion

        [STAThread]
        static void Main() 
        {
            Application.Run(new MainForm());
        }

      private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      {
         // not absolutely necessary to turn off the scanner, but it's polite
         Singletons.Statistics.Scanning = false;
      }

      private void Statistics_OnHeroLogin(CityOfHeroes.HeroData hero)
      {
         listBox1.Items.Add(string.Format("*** {0} Logged In", hero.Name));
      }

      private void Statistics_OnHeroLogout(CityOfHeroes.HeroData hero)
      {
         listBox1.Items.Add(string.Format("*** {0} Logged Out", hero.Name));
      }

      private void Scanner_OnChatMessage(CityOfHeroes.ChatMessage message)
      {
         listBox1.Items.Add(string.Format("At {0}: {1}", message.Timestamp, message.Message));
      }

      private 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)
            listBox1.Items.Add(string.Format("--- {0} has {1} experience", onlineHero.Name, onlineHero.Experience));
      }
   }
}