package org.helioviewer.jhv.renderable.components;

import java.awt.Component;

import org.helioviewer.base.Region;
import org.helioviewer.base.math.GL3DMat4d;
import org.helioviewer.jhv.display.Displayer;
import org.helioviewer.jhv.layers.Layers;
import org.helioviewer.jhv.renderable.gui.Renderable;
import org.helioviewer.jhv.renderable.gui.RenderableType;
import org.helioviewer.jhv.renderable.viewport.GL3DViewport;
import org.helioviewer.viewmodel.view.View;

import com.jogamp.opengl.GL2;

public class RenderableMiniview implements Renderable {

    private final RenderableType type;
    private boolean isVisible = true;

    public RenderableMiniview(RenderableMiniviewType renderableMiniviewType) {
        type = renderableMiniviewType;
    }

    private void drawCircle(GL2 gl, double x, double y, double r, int segments) {
        gl.glDisable(GL2.GL_TEXTURE_2D);
        {
            gl.glBegin(GL2.GL_TRIANGLE_FAN);
            gl.glVertex2d(x, y);
            for (int n = 0; n <= segments; ++n) {
                double t = 2 * Math.PI * n / segments;
                gl.glVertex2d(x + Math.sin(t) * r, y + Math.cos(t) * r);
            }
            gl.glEnd();
        }
        gl.glEnable(GL2.GL_TEXTURE_2D);
    }

    private void drawRectangle(GL2 gl, double x0, double y0, double w, double h) {
        double x1 = x0 + w;
        double y1 = y0 + h;
        gl.glDisable(GL2.GL_TEXTURE_2D);
        {
            gl.glBegin(GL2.GL_QUADS);
            gl.glVertex2d(x0, -y0);
            gl.glVertex2d(x0, -y1);
            gl.glVertex2d(x1, -y1);
            gl.glVertex2d(x1, -y0);
            gl.glEnd();
        }
        gl.glEnable(GL2.GL_TEXTURE_2D);
    }

    @Override
    public void render(GL2 gl, GL3DViewport vp) {
    }

    @Override
    public void renderMiniview(GL2 gl, GL3DViewport vp) {
        GL3DMat4d cameraMatrix = vp.getCamera().getLocalRotation().toMatrix();
        gl.glDepthRange(0.f, 0.f);
        gl.glPushMatrix();
        {
            gl.glMultMatrixd(cameraMatrix.transpose().m, 0);
            gl.glColor4d(0, 0, 0, 1);
            drawCircle(gl, 0, 0, 10, 100);
            gl.glColor4d(1, 0, 0, 1);
            drawCircle(gl, 0, 0, 1, 100);
            gl.glColor4d(0, 1, 0, 0.5);
            View v = Layers.getActiveView();
            if (v != null) {
                Region r = v.getImageData().getRegion();
                drawRectangle(gl, r.getLowerLeftCorner().x, r.getLowerLeftCorner().y, r.getWidth(), r.getHeight());
            }
        }
        gl.glPopMatrix();
        gl.glDepthRange(0.f, 1.f);
    }

    @Override
    public void remove(GL2 gl) {
        dispose(gl);
    }

    @Override
    public RenderableType getType() {
        return type;
    }

    @Override
    public Component getOptionsPanel() {
        return null;
    }

    @Override
    public String getName() {
        return "Miniview";
    }

    @Override
    public boolean isVisible() {
        return isVisible;
    }

    @Override
    public void setVisible(boolean isVisible) {
        this.isVisible = isVisible;
        Displayer.getMiniview().setVisible(isVisible);
    }

    @Override
    public String getTimeString() {
        return null;
    }

    @Override
    public boolean isDeletable() {
        return false;
    }

    @Override
    public boolean isActiveImageLayer() {
        return false;
    }

    @Override
    public void init(GL2 gl) {
    }

    @Override
    public void dispose(GL2 gl) {
    }

}

@@ -241,16 +241,66 @@
 Layers;%0A
+import org.helioviewer.jhv.layers.LayersListener;%0A
 import o
@@ -345,16 +345,16 @@
 erable;%0A
-
 import o
@@ -593,16 +593,32 @@
 nderable
+, LayersListener
  %7B%0A%0A    
@@ -805,24 +805,64 @@
 niviewType;%0A
+        Layers.addLayersListener(this);%0A
     %7D%0A%0A    p
@@ -2206,31 +2206,37 @@
 draw
-Circ
+Rectang
 le(gl, 
+-3
 0, 
+-3
 0, 
-10, 10
+60, 6
 0);%0A
@@ -3549,32 +3549,32 @@
 %0A%0A    @Override%0A
-
     public void 
@@ -3598,10 +3598,266 @@
 %0A    %7D%0A%0A
+    @Override%0A    public void layerAdded(View view) %7B%0A        activeLayerChanged(view);%0A%0A    %7D%0A%0A    @Override%0A    public void activeLayerChanged(View view) %7B%0A        if (view != null)%0A            Displayer.getMiniview().getCamera().zoomToFit(view);%0A    %7D%0A%0A
 %7D%0A

call zoom to fit miniview active layer changed/layer added