using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Canon;
using System.Runtime.InteropServices;
using System.Threading;

namespace ChtiCanon
{
    public static class Program
    {
        [DllImport("kernel32")]
        static extern bool AllocConsole();
        [DllImport("kernel32")]
        static extern bool QueryPerformanceFrequency(out ulong value);
        [DllImport("kernel32")]
        static extern bool QueryPerformanceCounter(out ulong value);

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            CanonAudio.Initialize();
            Application.Run(new frmConnect());
        }

        public class GameThread
        {
            private ConnectionManager cm;
            private Shared.Game g;
            public ConnectionManager Connection { get { return cm; } set { cm = value; } }
            public Shared.Game Game { get { return g; } }
            private int gamerId;
            private string[] names;
            public int GamerId
            {
                get { return gamerId; }
            }

            public GameThread(ConnectionManager cm, Shared.Game g, int gamerId, string[] names)
            {
                this.cm = cm;
                this.g = g;
                this.gamerId = gamerId;
                this.names = names;
                new Thread(new ThreadStart(start)).Start();
            }

            // Démarre le jeu
            private void start()
            {
                frmChtiCanon form = new frmChtiCanon(this); // Create the form
                ulong frequency, currentTime, lastTime, frameTime, lastAvgTime;
                int avgFrames = 0;
                AllocConsole();

                form.Show();
                QueryPerformanceFrequency(out frequency);
                QueryPerformanceCounter(out lastTime);
                frameTime = frequency / 60;
                lastAvgTime = lastTime;

                while (form.Created)
                {
                    bool doRender = false;
                    QueryPerformanceCounter(out currentTime);
                    while (currentTime - lastTime >= frameTime)
                    {
                        form.handle();
                        doRender = true;
                        lastTime += frameTime;
                    }

                    if (lastTime - lastAvgTime > frequency * 4)
                    {
                        Console.WriteLine("Framerate: " + (avgFrames / 4.0f));
                        lastAvgTime += frequency * 4;
                        avgFrames = 0;
                    }

                    if (doRender)
                    {
                        // Pour monitorer le temps moyen par frame
                        form.render();
                        CanonAudio.Update();
                        avgFrames++;
                    }
                    Application.DoEvents();
                }
            }

            public string PlayerName(int i) { return names[i]; }
        }
    }
}
