/*
 * Copyright (C) 2013 United States Government as represented by the Administrator of the
 * National Aeronautics and Space Administration.
 * All Rights Reserved.
 */

package gov.nasa.worldwindx.examples.multiwindow;

import com.jogamp.common.nio.Buffers;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.*;
import java.awt.*;
import java.nio.FloatBuffer;

/**
 * @author tag
 * @version $Id$
 */
public class JOGLTabbedPaneUsage extends JFrame
{
    int[] bufferId;

    private class WWPanel extends JPanel implements GLEventListener
    {
        GLCanvas canvas;

        public WWPanel(GLCanvas shareWith, int width, int height)
        {
            canvas = shareWith != null ? new GLCanvas(getCaps(), shareWith.getContext()) : new GLCanvas();
            canvas.setSize(new java.awt.Dimension(width, height));

            setLayout(new BorderLayout(5, 5));
            add(canvas, BorderLayout.CENTER);
            setOpaque(false);

            canvas.setAutoSwapBufferMode(false);
            canvas.addGLEventListener(this);
        }

        @Override
        public void init(GLAutoDrawable glAutoDrawable)
        {
//            glAutoDrawable.setGL(new DebugGL2(glAutoDrawable.getGL().getGL2()));
        }

        @Override
        public void dispose(GLAutoDrawable glAutoDrawable)
        {
        }

        @Override
        public void display(GLAutoDrawable glAutoDrawable)
        {
            if (bufferId == null)
                makeVBO(glAutoDrawable);

            drawFrame(glAutoDrawable);
            glAutoDrawable.swapBuffers();
        }

        @Override
        public void reshape(GLAutoDrawable glAutoDrawable, int i, int i1, int i2, int i3)
        {
            int w = getWidth();
            int h = getHeight();

            GL2 gl = glAutoDrawable.getGL().getGL2();

            gl.glViewport(0, 0, w, h);
            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrtho(0, 1, 0, 1, -1, 1);
            gl.glMatrixMode(GL2.GL_MODELVIEW);
            gl.glLoadIdentity();

            gl.glClearColor(0, 0, 0, 1);
            gl.glShadeModel(GL2.GL_FLAT);
        }

        protected void drawFrame(GLAutoDrawable drawable)
        {
            GL2 gl = drawable.getGL().getGL2();

            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

            gl.glColor3f(1, 1, 1);

            gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
            gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
            gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufferId[0]);
            gl.glDrawArrays(GL2.GL_LINES, 0, 2);

            gl.glFlush();
        }
    }

    protected void makeVBO(GLAutoDrawable drawable)
    {
        GL2 gl = drawable.getGL().getGL2();

        bufferId = new int[1];
        gl.glGenBuffers(1, bufferId, 0);
        gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufferId[0]);

        FloatBuffer vertices = Buffers.newDirectFloatBuffer(6);
        vertices.put(0).put(0).put(0);
        vertices.put(1).put(1).put(0);
        gl.glBufferData(GL2.GL_ARRAY_BUFFER, vertices.capacity() * 4, vertices.rewind(), GL2.GL_STATIC_DRAW);
    }

    public JOGLTabbedPaneUsage()
    {
        try
        {
            // Create the application frame and the tabbed pane and add the pane to the frame.
            JTabbedPane tabbedPanel = new JTabbedPane();
            this.add(tabbedPanel, BorderLayout.CENTER);

            // Create two World Windows that share resources.
            WWPanel wwpA = new WWPanel(null, 600, 600);
            WWPanel wwpB = new WWPanel(wwpA.canvas, wwpA.getWidth(), wwpA.getHeight());

            tabbedPanel.add(wwpA, "Window A");
            tabbedPanel.add(wwpB, "Window B");

            // Add the card panel to the frame.
            this.add(tabbedPanel, BorderLayout.CENTER);

            // Position and display the frame.
            setTitle("Multi-Window Tabbed Pane");
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            pack();
            setResizable(true);
            setVisible(true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    protected static GLCapabilities getCaps()
    {
        GLCapabilities caps = new GLCapabilities(GLProfile.getMaxFixedFunc(true));

        caps.setAlphaBits(8);
        caps.setRedBits(8);
        caps.setGreenBits(8);
        caps.setBlueBits(8);
        caps.setDepthBits(24);
        caps.setDoubleBuffered(true);

        return caps;
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JOGLTabbedPaneUsage();
            }
        });
    }
}
