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 OpenTK.Graphics;
using bbiwarg.Input.InputHandling;
using bbiwarg.Input.InputProviding;
using bbiwarg.Utility;

namespace bbiwarg.Output.DebugOutput
{
    public enum PalmGridControlFocus
    {
        Rows,
        Columns
    }

    class DebugWindow : GameWindow
    {
        private InputHandler inputHandler;
        private InputProvider inputProvider;
        private PalmGridControlFocus palmGridControlFocus;

        private uint textureID;

        private int currentFrameID;
        private DebugImages debugImages;

        public DebugWindow(InputProvider inputProvider, InputHandler inputHandler)
            : base((int)(Parameters.DebugWindowScaleFactor * Math.Max(1, Math.Min(Parameters.DebugWindowNumImagesPerRow, Parameters.DebugWindowNumImages)) * Parameters.ImageWidth),
                   (int)(Parameters.DebugWindowScaleFactor * (1 + (Parameters.DebugWindowNumImages - 1) / Parameters.DebugWindowNumImagesPerRow) * Parameters.ImageHeight))
        {
            this.inputProvider = inputProvider;
            this.inputHandler = inputHandler;

            TouchEventVisualizer touchEventVisualizer = new TouchEventVisualizer();
            inputHandler.NewProcessedFrameEvent += touchEventVisualizer.handleNewFrameData;
            debugImages = new DebugImages(touchEventVisualizer);
        }

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

            // 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);

            palmGridControlFocus = PalmGridControlFocus.Rows;
            Keyboard.KeyDown += HandleKeyDownPalmGridControls;
            if (inputProvider is VideoInputProvider)
                Keyboard.KeyDown += HandleKeyDownVideoControls;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);

            inputProvider.stop();
        }

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

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

            int numRows = 1 + (Parameters.DebugWindowNumImages - 1) / Parameters.DebugWindowNumImagesPerRow;
            int numCols = Math.Min(Parameters.DebugWindowNumImages, Parameters.DebugWindowNumImagesPerRow);
            int heightForWidth = (int)((float)screenWidth / ((float)numCols * Parameters.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 OnUpdateFrame(FrameEventArgs e)
        {
            Timer.start("DebugWindow.OnUpdateFrame");
            base.OnUpdateFrame(e);

            if (!inputProvider.IsActive)
                Exit();

            FrameData frameData = inputHandler.FrameData;
            if (frameData != null)
            {
                lock (frameData)
                {
                    if (currentFrameID != frameData.FrameID)
                    {
                        currentFrameID = frameData.FrameID;

                        Timer.start("DebugWindow.OnUpdateFrame::updateImages");
                        debugImages.updateImages(frameData);
                        Timer.stop("DebugWindow.OnUpdateFrame::updateImages");
                    }
                }
            }

            Timer.stop("DebugWindow.OnUpdateFrame");
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            Timer.start("DebugWindow.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);

            Title = Parameters.DebugWindowTitle + " (Frame: " + currentFrameID + ")";

            GL.Enable(EnableCap.Texture2D);

            Timer.start("DebugWindow.OnRenderFrame::drawImages");
            int imageIndex = 0;
            foreach (OutputImage image in debugImages.Images)
            {
                int column = imageIndex % Parameters.DebugWindowNumImagesPerRow;
                int row = imageIndex / Parameters.DebugWindowNumImagesPerRow;
                GL.BindTexture(TextureTarget.Texture2D, textureID);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.ImageWidth, Parameters.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 + column, 0 + row, -1);
                GL.TexCoord2(1, 0); GL.Vertex3(1 + column, 0 + row, -1);
                GL.TexCoord2(1, 1); GL.Vertex3(1 + column, 1 + row, -1);
                GL.TexCoord2(0, 1); GL.Vertex3(0 + column, 1 + row, -1);
                GL.End();
                imageIndex++;
            }
            Timer.stop("DebugWindow.OnRenderFrame::drawImages");

            Timer.start("DebugWindow.OnRenderFrame::SwapBuffers");
            SwapBuffers();
            Timer.stop("DebugWindow.OnRenderFrame::SwapBuffers");

            Timer.stop("DebugWindow.OnRenderFrame");
        }

        private void HandleKeyDownVideoControls(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.goToNextFrame();
                    }
                    break;
                case Key.Left:
                    if (vip.IsPaused)
                    {
                        vip.goToPreviousFrame();
                    }
                    break;
            }
        }


        private void HandleKeyDownPalmGridControls(object sender, KeyboardKeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.R:
                    palmGridControlFocus = PalmGridControlFocus.Rows;
                    break;
                case Key.C:
                    palmGridControlFocus = PalmGridControlFocus.Columns;
                    break;
                case Key.Plus:
                case Key.KeypadPlus:
                case Key.BracketRight: //fix
                    if (palmGridControlFocus == PalmGridControlFocus.Rows)
                        Parameters.setPalmGridParameters(Parameters.PalmGridNumRows + 1, Parameters.PalmGridNumColumns);
                    else
                        Parameters.setPalmGridParameters(Parameters.PalmGridNumRows, Parameters.PalmGridNumColumns + 1);
                    break;
                case Key.Minus:
                case Key.KeypadMinus:
                case Key.Slash: //fix
                    if (palmGridControlFocus == PalmGridControlFocus.Rows)
                        Parameters.setPalmGridParameters(Math.Max(1, Parameters.PalmGridNumRows - 1), Parameters.PalmGridNumColumns);
                    else
                        Parameters.setPalmGridParameters(Parameters.PalmGridNumRows, Math.Max(1, Parameters.PalmGridNumColumns - 1));
                    break;
            }
        }
    }
}
