package org.helioviewer.viewmodel.view.jp2view;

import java.awt.Dimension;
import java.awt.EventQueue;

import org.helioviewer.base.Region;
import org.helioviewer.base.Viewport;
import org.helioviewer.base.datetime.ImmutableDateTime;
import org.helioviewer.viewmodel.view.jp2view.J2KRender.RenderReasons;
import org.helioviewer.viewmodel.view.jp2view.image.JP2ImageParameter;
import org.helioviewer.viewmodel.view.jp2view.image.ResolutionSet.ResolutionLevel;
import org.helioviewer.viewmodel.view.jp2view.image.SubImage;

public class JHVJP2CallistoView extends JHVJP2View {

    private boolean viewportSet;
    private boolean regionSet;
    private JHVJP2CallistoViewDataHandler dataHandler;

    public JHVJP2CallistoView() {
        region = new Region(0, 0, 86400, 380);
        viewport = new Viewport(2700, 12);
        viewportSet = false;
        regionSet = false;
    }

    @Override
    public boolean setViewport(Viewport v) {
        boolean viewportChanged = (viewport == null ? v == null : !viewport.equals(v));
        viewportSet = true;
        viewport = v;

        if (regionSet) {
            if (setImageViewParams(calculateParameter())) {
                return true;
            } else if (viewportChanged && imageViewParams.resolution.getZoomLevel() == jp2Image.getResolutionSet().getMaxResolutionLevels()) {
                renderRequestedSignal.signal(RenderReasons.OTHER);
                return true;
            }
        }

        return viewportChanged;
    }

    @Override
    public boolean setRegion(Region r) {
        boolean changed = region == null ? r == null : !region.equals(r);
        region = r;
        regionSet = true;
        if (viewportSet) {
            changed |= setImageViewParams(calculateParameter());
        }

        return changed;
    }

    @Override
    public void render() {
    }

    public void setJHVJP2CallistoViewDataHandler(JHVJP2CallistoViewDataHandler dataHandler) {
        this.dataHandler = dataHandler;
    }

    @Override
    protected JP2ImageParameter calculateParameter() {
        return this.calculateParameter(getCurrentNumQualityLayers(), 0);
    }

    @Override
    protected JP2ImageParameter calculateParameter(int numQualityLayers, int frameNumber) {
        int maxHeight = jp2Image.getResolutionSet().getResolutionLevel(0).getResolutionBounds().height;
        int maxWidth = jp2Image.getResolutionSet().getResolutionLevel(0).getResolutionBounds().width;
        ResolutionLevel res = jp2Image.getResolutionSet().getClosestResolutionLevel(new Dimension(
                        (int) Math.ceil(viewport.getWidth() / region.getWidth() * maxWidth),
                        2 * (int) Math.ceil(viewport.getHeight() / region.getHeight() * maxHeight)));

        SubImage subImage = new SubImage(
                        (int) (region.getLowerLeftCorner().x / (double) maxWidth * res.getResolutionBounds().width),
                        (int) (region.getLowerLeftCorner().y / (double) maxHeight * res.getResolutionBounds().height),
                        (int) (region.getWidth() / (double) maxWidth * res.getResolutionBounds().width),
                        (int) (region.getHeight() / (double) maxHeight * res.getResolutionBounds().height));
        return new JP2ImageParameter(subImage, res, numQualityLayers, 0);
    }

    @Override
    protected void fireFrameChanged(JHVJP2View aView, ImmutableDateTime aDateTime) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                if (dataHandler != null) {
                    dataHandler.handleData(JHVJP2CallistoView.this);
                }
            }
        });
    }

    public void removeJHVJP2DataHandler() {
        dataHandler = null;
    }

}

@@ -228,16 +228,57 @@
 teTime;%0A
+import org.helioviewer.base.logging.Log;%0A
 import o
@@ -615,72 +615,8 @@
  %7B%0A%0A
-    private boolean viewportSet;%0A    private boolean regionSet;%0A
     
@@ -795,64 +795,8 @@
 2);%0A
-        viewportSet = false;%0A        regionSet = false;%0A
     
@@ -869,404 +869,22 @@
     
-boolean viewportChanged = (viewport == null ? v == null : !viewport.equals(v));%0A        viewportSet = true;%0A        viewport = v;%0A%0A        if (regionSet) %7B%0A            if (setImageViewParams(calculateParameter())) %7B%0A                return true;%0A            %7D else if (viewportChanged && imageViewParams.resolution.getZoomLevel() == jp2Image.getResolutionSet().getMaxResolutionLevels()) %7B%0A        
+viewport = v;%0A
     
@@ -938,24 +938,16 @@
 OTHER);%0A
-        
         
@@ -963,65 +963,8 @@
 ue;%0A
-            %7D%0A        %7D%0A%0A        return viewportChanged;%0A
     
@@ -1033,169 +1033,27 @@
     
-boolean changed = region == null ? r == null : !region.equals(r);%0A        region = r;%0A        regionSet = true;%0A        if (viewportSet) %7B%0A            changed %7C=
+region = r;%0A       
  set
@@ -1103,33 +1103,19 @@
     
-%7D%0A%0A        return changed
+return true
 ;%0A  
@@ -1871,33 +1871,8 @@
 ion(
-%0A                        
 (int
@@ -1939,32 +1939,8 @@
 th),
-%0A                       
  2 *
@@ -2060,33 +2060,8 @@
 age(
-%0A                        
 (int
@@ -2094,25 +2094,16 @@
 er().x /
- (double)
  maxWidt
@@ -2143,32 +2143,8 @@
 th),
-%0A                       
  (in
@@ -2178,25 +2178,16 @@
 er().y /
- (double)
  maxHeig
@@ -2229,32 +2229,8 @@
 ht),
-%0A                       
  (in
@@ -2248,33 +2248,24 @@
 getWidth() /
- (double)
  maxWidth * 
@@ -2301,32 +2301,8 @@
 th),
-%0A                       
  (in
@@ -2329,17 +2329,8 @@
 () /
- (double)
  max
@@ -2652,24 +2652,61 @@
 oid run() %7B%0A
+                Log.debug(%22Do run%22);%0A
             
@@ -2760,54 +2760,179 @@
     
-dataHandler.handleData(JHVJP2CallistoView.this
+Log.debug(%22Handle data%22);%0A                    dataHandler.handleData(JHVJP2CallistoView.this);%0A                %7D else %7B%0A                    Log.debug(%22Data handle is null%22
 );%0A 

Fix radio images not drawn in some cases