using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Input;
using OpenTK.Graphics.OpenGL;
using bbiwarg.InputProviders;
using bbiwarg.Utility;

namespace bbiwarg.Graphics
{
    class OutputWindow : GameWindow
    {
        private InputHandler inputHandler;
        private InputProvider inputProvider;

        private uint textureID;

        public OutputWindow(InputProvider inputProvider, InputHandler inputHandler)
            : base((int)(Constants.OutputScaleFactor * Math.Max(1, Math.Min(Constants.OutputNumImagesPerRow, Constants.OutputNumImages)) * inputProvider.ImageWidth),
                   (int)(Constants.OutputScaleFactor * (1 + (Constants.OutputNumImages - 1) / Constants.OutputNumImagesPerRow) * inputProvider.ImageHeight))
        {
            this.inputProvider = inputProvider;
            this.inputHandler = inputHandler;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Title = Constants.OutputTitle;
            GL.ClearColor(Color.Black);

            // transparency
            GL.Enable(EnableCap.Blend);
            GL.BlendEquation(BlendEquationMode.Max);

            // Depth Test 
            GL.Enable(EnableCap.DepthTest);

            // Texture
            GL.Enable(EnableCap.Texture2D);
            GL.GenTextures(1, out textureID);
            GL.BindTexture(TextureTarget.Texture2D, textureID);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            // video controls
            if (inputProvider is VideoInputProvider)
            {
                Keyboard.KeyDown += HandleKeyDown;
                Logger.log("video controls enabled (space=pause, right=nextFrame, left=previousFrame)", LogSubject.VideoControls);
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            int screenWidth = ClientRectangle.Width;
            int screenHeight = ClientRectangle.Height;

            int imageWidth = inputProvider.ImageWidth;
            int imageHeight = inputProvider.ImageHeight;
            float imageAspectRatio = (float)imageWidth / (float)imageHeight;

            int numRows = 1 + (Constants.OutputNumImages - 1) / Constants.OutputNumImagesPerRow;
            int numCols = Math.Min(Constants.OutputNumImages, Constants.OutputNumImagesPerRow);
            int heightForWidth = (int)((float)screenWidth / ((float)numCols * imageAspectRatio) * (float)numRows);

            GL.Viewport(0, (screenHeight - heightForWidth) / 2, screenWidth, heightForWidth);

            // top left at (0,0) every image from (i, j) to (i + 1, j + 1)
            Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, numCols, numRows, 0, 0.001f, 10f);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            Timer.start("onRenderFrame");

            base.OnRenderFrame(e);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview);

            inputHandler.updateFrame();

            Title = Constants.OutputTitle + " (Frame: " + inputProvider.CurrentFrame + ")";

            Timer.start("buildTextures");
            GL.Enable(EnableCap.Texture2D);

            int imageIndex = 0;
            foreach (OutputImage image in inputHandler.OutputImages)
            {
                int x = imageIndex % Constants.OutputNumImagesPerRow;
                int y = imageIndex / Constants.OutputNumImagesPerRow;
                GL.BindTexture(TextureTarget.Texture2D, textureID);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, inputProvider.ImageWidth, inputProvider.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
                GL.Begin(PrimitiveType.Quads);
                GL.Color3(1.0, 1.0, 1.0);
                GL.TexCoord2(0, 0); GL.Vertex3(0 + x, 0 + y, -1);
                GL.TexCoord2(1, 0); GL.Vertex3(1 + x, 0 + y, -1);
                GL.TexCoord2(1, 1); GL.Vertex3(1 + x, 1 + y, -1);
                GL.TexCoord2(0, 1); GL.Vertex3(0 + x, 1 + y, -1);
                GL.End();
                ++imageIndex;
            }

            Timer.stop("buildTextures");

            Timer.start("swapBuffers");
            SwapBuffers();
            Timer.stop("swapBuffers");

            Timer.stop("buildTextures");
            Timer.stop("onRenderFrame");

            Timer.outputAll();
        }

        private void HandleKeyDown(object sender, KeyboardKeyEventArgs e)
        {
            VideoInputProvider vip = (VideoInputProvider)inputProvider;
            switch (e.Key)
            {
                case Key.Space:
                    if (vip.IsPaused)
                        vip.play();
                    else
                        vip.pause();
                    break;
                case Key.Right:
                    if (vip.IsPaused)
                        vip.nextFrame();
                    break;
                case Key.Left:
                    if (vip.IsPaused)
                        vip.previousFrame();
                    break;
            }
        }
    }
}
