package org.helioviewer.jhv.layers;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.annotation.Nullable;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import org.helioviewer.jhv.base.Colors;
import org.helioviewer.jhv.camera.Camera;
import org.helioviewer.jhv.gui.ComponentUtils;
import org.helioviewer.jhv.gui.components.base.WheelSupport;
import org.helioviewer.jhv.display.Display;
import org.helioviewer.jhv.display.Viewport;
import org.helioviewer.jhv.math.MathUtils;
import org.helioviewer.jhv.opengl.GLInfo;
import org.helioviewer.jhv.opengl.GLText;
import org.helioviewer.jhv.opengl.text.JhvTextRenderer;
import org.json.JSONObject;

import com.jogamp.opengl.GL2;

public class TimestampLayer extends AbstractLayer {

    private static final int MIN_SCALE = 50;
    private static final int MAX_SCALE = 200;
    private int scale = 100;

    private final JPanel optionsPanel;

    @Override
    public void serialize(JSONObject jo) {
        jo.put("scale", scale);
    }

    public TimestampLayer(JSONObject jo) {
        if (jo != null)
            scale = MathUtils.clip(jo.optInt("scale", scale), MIN_SCALE, MAX_SCALE);
        optionsPanel = optionsPanel();
    }

    @Override
    public void render(Camera camera, Viewport vp, GL2 gl) {
    }

    @Override
    public void renderFloat(Camera camera, Viewport vp, GL2 gl) {
        if (!isVisible[vp.idx])
            return;

        String text = camera.getViewpoint().time.toString(); //Movie.getTime().toString();
        if (Display.multiview) {
            ImageLayer im = ImageLayers.getImageLayerInViewport(vp.idx);
            if (im != null) {
                text = im.getTimeString() + ' ' + im.getName();
            }
        }

        int delta = (int) (vp.height * 0.01);
        int size = (int) (vp.height * (scale * 0.01 * 0.015));

        if (GLInfo.pixelScale[1] == 1) //! nasty
            size *= 2;

        JhvTextRenderer renderer = GLText.getRenderer(size);
        renderer.beginRendering(vp.width, vp.height);
        renderer.setColor(GLText.shadowColor);
        renderer.draw(text, delta + GLText.shadowOffset[0], delta + GLText.shadowOffset[1]);
        renderer.setColor(Colors.LightGrayFloat);
        renderer.draw(text, delta, delta);
        renderer.endRendering();
    }

    @Override
    public void init(GL2 gl) {
    }

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

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

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

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

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

    @Override
    public void dispose(GL2 gl) {
    }

    private JPanel optionsPanel() {
        JSlider slider = new JSlider(JSlider.HORIZONTAL, MIN_SCALE, MAX_SCALE, scale);
        slider.addChangeListener(e -> {
            scale = slider.getValue();
            MovieDisplay.display();
        });
        WheelSupport.installMouseWheelSupport(slider);

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c0 = new GridBagConstraints();
        c0.anchor = GridBagConstraints.LINE_END;
        c0.weightx = 1.;
        c0.weighty = 1.;
        c0.gridy = 0;
        c0.gridx = 0;
        panel.add(new JLabel("Size", JLabel.RIGHT), c0);
        c0.anchor = GridBagConstraints.LINE_START;
        c0.gridx = 1;
        panel.add(slider, c0);

        ComponentUtils.smallVariant(panel);
        return panel;
    }

}

@@ -159,16 +159,46 @@
 llable;%0A
+import javax.swing.JCheckBox;%0A
 import j
@@ -270,16 +270,51 @@
 JSlider;
+%0Aimport javax.swing.SwingConstants;
 %0A%0Aimport
@@ -973,16 +973,17 @@
  = 200;%0A
+%0A
     priv
@@ -1002,16 +1002,49 @@
 e = 100;
+%0A    private boolean top = false;
 %0A%0A    pr
@@ -1174,160 +1174,307 @@
     
-%7D%0A%0A    public TimestampLayer(JSONObject jo) %7B%0A        if (jo != null)%0A            scale = MathUtils.clip(jo.optInt(%22scale%22, scale), MIN_SCALE, MAX_SCALE
+    jo.put(%22top%22, top);%0A    %7D%0A%0A    private void deserialize(JSONObject jo) %7B%0A        scale = MathUtils.clip(jo.optInt(%22scale%22, scale), MIN_SCALE, MAX_SCALE);%0A        top = jo.optBoolean(%22top%22, top);%0A    %7D%0A%0A    public TimestampLayer(JSONObject jo) %7B%0A        if (jo != null)%0A            deserialize(jo
 );%0A 
@@ -2053,54 +2053,8 @@
  %7D%0A%0A
-        int delta = (int) (vp.height * 0.01);%0A
     
@@ -2112,17 +2112,16 @@
 .015));%0A
-%0A
         
@@ -2185,16 +2185,162 @@
  *= 2;%0A%0A
+        int deltaX = (int) (vp.height * 0.01);%0A        int deltaY = top ? (int) (vp.height - GLInfo.pixelScale%5B1%5D * deltaX - size) : deltaX; //!%0A%0A
         
@@ -2522,24 +2522,25 @@
 (text, delta
+X
  + GLText.sh
@@ -2559,16 +2559,17 @@
 %5D, delta
+Y
  + GLTex
@@ -2671,23 +2671,25 @@
 t, delta
+X
 , delta
+Y
 );%0A     
@@ -3966,24 +3966,376 @@
 ider, c0);%0A%0A
+        c0.gridx = 2;%0A        c0.anchor = GridBagConstraints.LINE_END;%0A        JCheckBox showTop = new JCheckBox(%22Top%22, top);%0A        showTop.setHorizontalTextPosition(SwingConstants.LEFT);%0A        showTop.addActionListener(e -%3E %7B%0A            top = showTop.isSelected();%0A            MovieDisplay.display();%0A        %7D);%0A        panel.add(showTop, c0);%0A%0A
         Comp

Add option to put timestamp on top