package org.helioviewer.jhv.gui.components.statusplugins;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import org.helioviewer.jhv.base.math.Vec2;
import org.helioviewer.jhv.camera.Camera;
import org.helioviewer.jhv.camera.CameraHelper;
import org.helioviewer.jhv.display.Displayer;
import org.helioviewer.jhv.display.Viewport;
import org.helioviewer.jhv.gui.ImageViewerGui;
import org.helioviewer.jhv.gui.components.StatusPanel;
import org.helioviewer.jhv.gui.controller.InputControllerPlugin;

@SuppressWarnings("serial")
public class PositionStatusPanel extends StatusPanel.StatusPlugin implements MouseMotionListener, InputControllerPlugin {

    private static String rhoFormat = " | \u03c1 : %.2f R\u2299";
    private static String emptyPos = "(\u03C6, \u03B8) : (--\u00B0, --\u00B0)";
    private static String xyFormat = "(x, y) : ( %5d\u2033, %5d\u2033) ";

    private static Camera camera;

    public PositionStatusPanel() {
        setText(emptyPos + String.format(rhoFormat, 0.));
    }

    private void update(Point position) {
        Viewport vp = Displayer.getActiveViewport();
        Vec2 coord = ImageViewerGui.getRenderableGrid().gridPoint(camera, vp, position);

        if (Displayer.mode == Displayer.DisplayMode.LATITUDINAL) {
            setText(String.format("(\u03C6, \u03B8) : (%.2f\u00B0,%.2f\u00B0)", coord.x, coord.y));
        } else if (Displayer.mode == Displayer.DisplayMode.POLAR || Displayer.mode == Displayer.DisplayMode.LOGPOLAR) {
            setText(String.format("\u03B8 : %.2f\u00B0", coord.x) + String.format(rhoFormat, coord.y));
        } else {
            double radius = CameraHelper.getRadiusFromSphereAlt(camera, vp, position);
            double x = CameraHelper.computeUpX(camera, vp, position.x);
            double y = CameraHelper.computeUpY(camera, vp, position.y);
            double d = camera.getViewpoint().distance;
            int px = (int) Math.round((3600 * 180 / Math.PI) * Math.atan2(x, d));
            int py = (int) Math.round((3600 * 180 / Math.PI) * Math.atan2(y, d));
            String xyPos = String.format(xyFormat, px, py);

            if (coord == null)
                setText(xyPos + emptyPos + String.format(rhoFormat, radius));
            else
                setText(xyPos + String.format("(\u03C6, \u03B8) : (%.2f\u00B0,%.2f\u00B0)", coord.x, coord.y) + String.format(rhoFormat, radius));
        }
    }

    @Override
    public void setCamera(Camera _camera) {
        camera = _camera;
    }

    @Override
    public void setComponent(Component _component) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        update(e.getPoint());
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        update(e.getPoint());
    }

}

@@ -746,113 +746,38 @@
 tic 
+final 
 String 
-rhoFormat = %22 %7C %5Cu03c1 : %25.2f R%5Cu2299%22;%0A    private static String emptyPos = %22(%5Cu03C6, %5Cu03B8) : (
+nullCoordStr = %22-
 --%5Cu
@@ -785,17 +785,17 @@
 0B0,
- 
+-
 --%5Cu00B0
 )%22;%0A
@@ -794,83 +794,8 @@
 00B0
-)%22;%0A    private static String xyFormat = %22(x, y) : ( %255d%5Cu2033, %255d%5Cu2033) 
 %22;%0A%0A
@@ -884,46 +884,33 @@
 ext(
-emptyPos + String.format(rhoFormat
+formatOrtho(null, 0, 0
 , 0
-.
 ));%0A
@@ -1203,33 +1203,32 @@
 format(%22(%5Cu03C6,
- 
 %5Cu03B8) : (%25.2f%5C
@@ -1426,17 +1426,27 @@
 at(%22
+(
 %5Cu03B8
+,%5Cu03c1)
  : 
+(
 %25.2f
@@ -1455,45 +1455,31 @@
 00B0
-%22, coord.x) + String.format(rhoFormat
+,%25.2fR%5Cu2299)%22, coord.x
 , co
@@ -1950,24 +1950,25 @@
 an2(y, d));%0A
+%0A
             
@@ -1971,69 +1971,179 @@
     
-String xyPos =
+setText(formatOrtho(coord, radius, px, py));%0A        %7D%0A    %7D%0A%0A    private
  String
-.
+ 
 format
-(xyFormat, px, py);%0A%0A     
+Ortho(Vec2 coord, double radius, int px, int py) %7B%0A        String coordStr;%0A
        
+ 
 if (
@@ -2148,17 +2148,17 @@
  (coord 
-=
+!
 = null)%0A
@@ -2173,122 +2173,146 @@
     
-    setText(xyPos + emptyPos + String.format(rhoFormat, radius));%0A            else%0A                setText(xyPos +
+coordStr = String.format(%22%25+7.2f%5Cu00B0,%25+7.2f%5Cu00B0%22, coord.x, coord.y);%0A        else%0A            coordStr = nullCoordStr;%0A%0A        return
  Str
@@ -2331,17 +2331,16 @@
 (%5Cu03C6,
- 
 %5Cu03B8) 
@@ -2347,96 +2347,96 @@
 : (%25
-.2f%5Cu00B0,
+s) %7C %5Cu03c1 : 
 %25.2f
+R
 %5Cu
-00B0)%22, coord.x, coord.y) + String.format(rhoFormat, radius));%0A        %7D
+2299 %7C (x,y) : (%25+5d%5Cu2033,%25+5d%5Cu2033)%22, coordStr, radius, px, py);
 %0A   

Better formatting of coordinate string