using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Diagnostics;

using EVE.EVEThreading;
using EVE.EVEObjects;
using EVE.EVEOctrees;
using EVE.EVEScene;
using EVE.EVECameras;
using Microsoft.Xna.Framework.Graphics;

namespace EVE.EVERenderer
{
    public class EVERenderManager
    {
        protected GraphicsDevice myGraphicsDevice;

        protected EVEDoubleBuffer doubleBuffer;
        protected GameTime gameTime;

        protected EVEMessageBuffer messageBuffer;
        protected EVEGame game;

        protected EVESceneGraph myScene;
        public EVESceneGraph Scene { get { return myScene; } }

        protected EVEOctree myOctree;
        public EVEOctree Octree { get { return myOctree; } }

        public Stopwatch FrameWatch { get; set; }

        public EVERenderManager(EVEDoubleBuffer doubleBuffer, EVEGame game)
        {
            this.doubleBuffer = doubleBuffer;
            this.game = game;
            FrameWatch = new Stopwatch();
            FrameWatch.Reset();

            //myOctree = new EVEOctree();
            myScene = new EVESceneGraph();
        }

        public void UpdateRenderData()
        {
            foreach (EVEMessage msg in messageBuffer.Messages)
            {
                switch (msg.MessageType)
                {
                    case EVEMessageType.UpdateCameraView:
                        EVECameraManager.ActiveCamera.SetView(msg.CameraViewMatrix);
                        break;
                    case EVEMessageType.UpdateWorldMatrix:
                        myScene.getObjectNode(msg.ID).myObject.WorldMatrix = msg.WorldMatrix;
                        break;
                    case EVEMessageType.CreateNewRenderData:
                        EVEModel newModel = new EVEModel("ball");
                        newModel.SetShader("BS");
                        newModel.DiffuseColor = msg.Color;
                        newModel.WorldMatrix = Matrix.CreateTranslation(msg.Position);
                        myScene.AddObject(msg.ID, newModel);
                         break;
                    default:
                        break;
                }
            }
        }

        public virtual void LoadContent()
        {
        }

        public void DoFrame()
        {
            doubleBuffer.StartRenderProcessing(out messageBuffer, out gameTime);
            this.Draw();
            doubleBuffer.SubmitRender();
        }

        public virtual void Draw()
        {
        }
    }
}
