using System;
using System.Collections.Generic;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using EZGL2D;

namespace EZGL2DexampleBasic
{
    public class GL2Dexample : GameWindow  // inherit the GameWindow class
    {
        const int SCREEN_WIDTH = 800;       // screen dimension
        const int SCREEN_HEIGHT = 600;

        GL2D.SpriteGL plantSprite;                // our sprite
        
        
        // Init screen
        public GL2Dexample()
            : base(SCREEN_WIDTH, SCREEN_HEIGHT)
        {

        }

        // this is where you would initialize everything
        // like loading textures, setting up OpenGL screen, etc.
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            VSync = VSyncMode.On;                           // force vertical synching
            Title = "EZGL2D is too easy to use it hurts!";  // set the title
            //WindowBorder = WindowBorder.Hidden;             // hide the window

            // Load our sprite
            plantSprite = GL2D.LoadSprite("data/EZGL2DexampleBasic/plant.png", (int)TextureMinFilter.Linear);

        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            GL2D.Screen2D(Width, Height);  // reset the screen every resize
        }

        // this is where all your game logic
        // should be done. (Input, Entity movement, collisions, etc)
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            // get input
            if (Keyboard[OpenTK.Input.Key.Escape])
            {
                this.Exit();
                return;
            }
        }

        // This is where all rendering/drawing should be done
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL2D.ClearScreen();   // clear the screen
            GL2D.EnableAntialias(true);   // smoothen the primitives
            GL2D.Begin2D();         // start 2D drawing mode

            // Draw boxes that get smaller continuously
            GL.Color4(0.0f, 0.7f, 0.5f, 1.0f);     //cyan-ish Box
            for (int i = 0; i < 30; i++)
            {
                GL2D.Box(10 + i * 10, 10 + i * 10,
                     (Width - 10) - i * 10, (Height - 10) - i * 10);   
            }

            // Draw a little box on the upper-left corner
            GL.Color4(1.0f, 0.7f, 0.5f, 1.0f);     
            GL2D.BoxFilled(100, 100, 30, 40);   

            // Draw a triangle on the lower-right corner
            GL.Color4(0.0f, 0.0f, 1.0f, 1.0f);     
            GL2D.TriangleFilled(600, 400, 750, 500, 500, 550);   

            // Draw some blue colored spokes
            GL.Color4(0.0f, 1.0f, 0.0f, 1.0f);     
            for (int i = 0; i < 360; i += 10)
            {
                double angle = i * Math.PI/180;
                int x = (int)(Math.Cos(angle) * 200);
                int y = (int)(Math.Sin(angle) * 200);
                int cx = Width/2;
                int cy = Height/2;

                GL2D.Line(cx + x, cy + y, cx - x, cy - y);
                
            }

            // draw two simple sprites
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);
            GL2D.BlendMode(GL2D.BLEND_MODE.GL2D_ALPHA);
            GL2D.SpriteRotateScale(85, 500, 0, 1, 1, GL2D.SpriteGL.FLIP_H, plantSprite);
            GL2D.SpriteRotateScale(Width-85, 100, 0, 1, 1, GL2D.SpriteGL.FLIP_NONE, plantSprite);

            GL2D.End2D();
    
            this.SwapBuffers();     // copy what we draw to screen

            Thread.Sleep(1);        // sleep so that we don't hog CPU

        }


        [STAThread]
        public static void Main()
        {
            using (GL2Dexample example = new GL2Dexample())
            {
                example.Run(60);    // run the game at 60 frames per second
            }
        }

    }
}
