using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Camera;
using RecrownedAthenaeum.Render;
using System;
namespace CameraTest
{
    /// 
    /// This is the main type for your game.
    /// 
    public class CameraTest : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        PrimitiveBatch pb;
        Camera2D camera;
        RectangleRenderer rr;
        BasicEffect be;
        public CameraTest()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        /// 
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// 
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();
        }
        /// 
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// 
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            camera = new Camera2D(graphics.GraphicsDevice);
            spriteBatch = new SpriteBatch(GraphicsDevice);
            rr = new RectangleRenderer(camera, GraphicsDevice);
            pb = new PrimitiveBatch(camera, GraphicsDevice);
            be = new BasicEffect(GraphicsDevice);
            be.World = camera.worldMatrix;
            be.View = camera.ViewMatrix;
            be.Projection = camera.projectionMatrix;
            // TODO: use this.Content to load your game content here
        }
        /// 
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// 
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            rr.Dispose();
        }
        /// 
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// 
        /// Provides a snapshot of timing values.
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                Console.WriteLine("moving..");
                camera.MoveCamera(new Vector2(-5, 0));
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                Console.WriteLine("moving..");
                camera.MoveCamera(new Vector2(5, 0));
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                Console.WriteLine("moving..");
                camera.MoveCamera(new Vector2(0, 5));
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                Console.WriteLine("moving..");
                camera.MoveCamera(new Vector2(0, -5));
            }
            camera.Apply();
            base.Update(gameTime);
        }
        /// 
        /// This is called when the game should draw itself.
        /// 
        /// Provides a snapshot of timing values.
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            rr.Draw(25, 25, 70, 70, Color.Purple);
            /*
             *             float rotation = 0;
                        double width = 20;
                        double height = 20;
                        int x = 50;
                        int y = 50;
                        double topRightAngleFromOrig = Math.Atan(height / width);
                        pb.Begin(PrimitiveType.LineList);
                        Vector2 bottomLeft = new Vector2(x, y);
                        Vector2 bottomRight = new Vector2(x + (float)(Math.Cos(rotation) * width), y + (float)(Math.Sin(rotation) * width));
                        float origDiagonalHypotenuse = (float)Math.Sqrt(width * width + height * height);
                        Vector2 topRight = new Vector2(x + (float)Math.Cos(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse, y + (float)Math.Sin(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse);
                        Vector2 topLeft = new Vector2(x - (float)(Math.Cos((Math.PI / 2f) - rotation) * height), y + (float)(Math.Sin((Math.PI / 2f) - rotation) * height));
                        verts[0] = bottomLeft;
                        verts[1] = bottomRight;
                        verts[2] = topRight;
                        verts[3] = topLeft;
                        vertposcol[0] = new VertexPositionColor(new Vector3(verts[0], 0), Color.White);
                        vertposcol[1] = new VertexPositionColor(new Vector3(verts[1], 0), Color.White);
                        vertposcol[2] = new VertexPositionColor(new Vector3(verts[2], 0), Color.White);
                        vertposcol[3] = new VertexPositionColor(new Vector3(verts[3], 0), Color.White);
                        EffectTechnique effectTechnique = be.Techniques[0];
                        EffectPassCollection effectPassCollection = effectTechnique.Passes;
                        foreach (EffectPass pass in effectPassCollection)
                        {
                            pass.Apply();
                            GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, vertposcol, 0, 4);
                        }
                        pb.End(); 
             */
            base.Draw(gameTime);
        }
    }
}