using EVE;
using EVE.EVEObjects;
using EVE.EVEShaders;
using EVE.EVECameras;
using EVE.EVEOctrees;
using EVE.EVEAssets;
using EVE.EVEGpgpu;
using EVE.EVELogic;
using EVE.EVELights;

using System.Windows.Forms;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using Keys = Microsoft.Xna.Framework.Input.Keys;
using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;

using System.CodeDom;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using System.Reflection;
using Microsoft.CSharp;
using System.Security.Permissions;

using System;

using System.Threading;

namespace EVEDemo
{
    public class EVEDemo
    {
        static EVEGame game = new EVEGame();

        public static void Main()
        {
            //game.Input.OnUpdate += new UpdateMethod(GoFullScreen);
            game.Input.OnUpdate += new UpdateMethod(CameraControls);
            game.Input.OnUpdate += new UpdateMethod(KeyboardUpdate);

            SetupScene();
            game.Run();
        }

        private static void SetupScene()
        {          
            /*
            int uID;

            EVEEffect shader = new EVEEffect(@"Content/Shaders/BasicShader");
            EVEShaderManager.AddEffect("BS", shader);

            shader = new EVEEffect(@"Content/Shaders/TransformTexture");
            EVEShaderManager.AddEffect("SkyShader", shader);

            string[] SkySides = new string[6];
            SkySides[0] = @"Content/Textures/sky5";
            SkySides[1] = @"Content/Textures/sky2";

            SkySides[2] = @"Content/Textures/sky4";
            SkySides[3] = @"Content/Textures/sky1";

            SkySides[4] = @"Content/Textures/sky6";
            SkySides[5] = @"Content/Textures/sky3";

            EVESkybox sky = new EVESkybox(SkySides);
            sky.SetShader("SkyShader");

            uID = EVEGlobalManager.getUID();

            //game.Renderer.Scene.AddObject(uID,sky);
            //uID++;

            EVEExposer.SetupExposedObjects();

            //EVEOctreeManager.DrawOctNodes = true;

            EVEModel model;
            EVEUnit unit;

            Random rand = new Random();

            EVEModelManager.AddModel("tiger", @"Content/Models/tiger");
            EVEModelManager.AddModel("table", @"Content/Models/table");
            EVEModelManager.AddModel("ball", @"Content/Models/ball");
            
            model = new EVEModel("ball");
            model.SetShader("BS");
            model.Position = new Vector3(0 , 0 , 20);
            model.Scaling = Vector3.One;
            model.DiffuseColor = new Vector3(0.85f, 0.1f, 0.1f);
            model.WorldMatrix = Matrix.CreateTranslation(model.Position);

            unit = new EVEUnit(uID);
            unit.position = model.Position;

            game.Updater.addUnit(uID, unit);
            game.Renderer.Scene.AddObject(uID, model);

            for (int i = 0; i < 14; i++)
                for (int j = 0; j < 14; j++)
            {
                uID = EVEGlobalManager.getUID();
                model = new EVEModel("tiger");
                model.SetShader("BS");
                model.Position = new Vector3(3 * (i - 5), 0, 3 * (j - 10));
                model.Scaling = Vector3.One;
                model.WorldMatrix = Matrix.CreateTranslation(model.Position);
                model.DiffuseColor = new Vector3((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble());
                    
                unit = new EVEUnit(uID);
                unit.position = model.Position;

                game.Updater.addUnit(uID, unit);
                game.Renderer.Scene.AddObject(uID, model);
           }

            uID = EVEGlobalManager.getUID();
            model = new EVEModel("table");
            model.SetShader("BS");
            model.Position = new Vector3(0, -1, 0);
            model.Scaling = Vector3.One;
            model.WorldMatrix = Matrix.CreateTranslation(model.Position);

            unit = new EVEUnit(uID);
            unit.position = model.Position;

            unit.isPhysical = false;
            unit.isStatic = true;

            game.Updater.addUnit(uID, unit);
            game.Renderer.Scene.AddObject(uID, model);
            */

            EVEGpuSupport.Instance.addTask(EVEGpTask_Blur.Instance);
        }

        public static void GoFullScreen()
        {
            if (game.Input.Keys.Contains(Keys.F))
            {
                game.Graphics.ToggleFullScreen();
            }

            if (game.Input.Keys.Contains(Keys.O))
            {
                //EVEOctreeManager.DrawOctNodes = !EVEOctreeManager.DrawOctNodes;
            }
        }

        public static void CameraControls()
        {
            Vector2 mouseMove = game.Input.MouseMoved;
            ButtonList MouseButtons = game.Input.MouseButtons;

            if (MouseButtons[1] == ButtonState.Pressed)
            {
                EVECameraManager.ActiveCamera.Revolve(Vector3.Zero, new Vector3(1, 0, 0), mouseMove.Y * 0.01f);
                EVECameraManager.ActiveCamera.Revolve(Vector3.Zero, new Vector3(0, 1, 0), mouseMove.X * 0.01f);
            }
            
            // Check the Left Mouse Button
            if (MouseButtons[0] == ButtonState.Pressed)
            {
                EVECameraManager.ActiveCamera.Translate(new Vector3(0, 0, -0.3f));
            }

            // Check the Right Mouse Button
            if (MouseButtons[2] == ButtonState.Pressed)
            {
                EVECameraManager.ActiveCamera.Translate(new Vector3(0, 0, 0.3f));
            }
        }

        public static void KeyboardUpdate()
        {
            foreach (Keys k in game.Input.Keys)
            {
                switch (k)
                {
                    case Keys.Escape:
                        game.Exit();
                        break;
                    case Keys.F2:
                        game.Graphics.ToggleFullScreen();
                        break;
                }

            }
        }
    }
}