package etomica.modules.glass;

import etomica.action.activity.Controller;
import etomica.atom.IAtom;
import etomica.graphics.DisplayBox;
import etomica.graphics.DisplayBoxCanvas2D;
import etomica.space.Space;
import etomica.space.Vector;

import java.awt.*;

public class DisplayBoxCanvas2DGlass extends DisplayBoxCanvas2D implements DisplayBoxCanvasGlass {

    protected ConfigurationStorage configStorage;
    protected int configIndex;
    protected final Vector dr;
    protected boolean drawDisplacement, flipDisplacement;

    public DisplayBoxCanvas2DGlass(DisplayBox _box, Space _space, Controller controller, ConfigurationStorage configStorage) {
        super(_box, _space, controller);
        dr = _space.makeVector();
        this.configStorage = configStorage;
        configIndex = 100;
    }

    public void setConfigIndex(int idx) {
        configIndex = idx;
        repaint();
    }

    public int getConfigIndex() {
        return configIndex;
    }

    public void setDrawDisplacement(boolean doDrawDisplacement) {
        this.drawDisplacement = doDrawDisplacement;
    }

    public boolean getDrawDisplacement() {
        return drawDisplacement;
    }

    public void setConfigStorage(ConfigurationStorage configStorage){
        this.configStorage = configStorage;
    }

    public ConfigurationStorage getConfigStorage(){ return configStorage;}


    protected void drawAtom(Graphics g, int[] origin, IAtom a) {
        if (!drawDisplacement) {
            super.drawAtom(g, origin, a);
            return;
        }
        int idx = configIndex;
        int lastIndex = configStorage.getLastConfigIndex();
        if (idx > lastIndex) idx = lastIndex;
        Vector[] oldPositions = idx == -1 ? null : configStorage.getSavedConfig(idx);
        Vector r = idx == -1 ? a.getPosition() : configStorage.getSavedConfig(0)[a.getLeafIndex()];
        Vector rOld = idx == -1 ? r : configStorage.getSavedConfig(idx)[a.getLeafIndex()];
        int sigmaP, xP, yP, baseXP, baseYP;

        g.setColor(displayBox.getColorScheme().getAtomColor(a));

        double toPixels = pixel.toPixels() * displayBox.getScale();

        baseXP = origin[0] + (int) (toPixels * r.getX(0));
        baseYP = origin[1] + (int) (toPixels * r.getX(1));
        /* Draw the core of the atom, specific to the dimension */
        double sigma = displayBox.getDiameterHash().getDiameter(a);
        // default diameter
        if (sigma == -1) sigma = 1;
        sigmaP = (int) (toPixels * sigma);
        sigmaP = (sigmaP == 0) ? 1 : sigmaP;
        xP = baseXP - (sigmaP >> 1);
        yP = baseYP - (sigmaP >> 1);
        g.fillOval(xP, yP, sigmaP, sigmaP);
        /* Draw the orientation line, if any */

        dr.Ev1Mv2(r, rOld);
        int dxy = (int) (toPixels * 0.5 * sigma);
        int dx = (int) (toPixels * dr.getX(0));
        int dy = (int) (toPixels * dr.getX(1));
        xP += dxy;
        yP += dxy;
        g.drawLine(xP, yP, xP - dx, yP - dy);
    }

    public void setFlipDisplacement(boolean flipDisplacement) {
        this.flipDisplacement = flipDisplacement;
    }

    public boolean getFlipDisplacement() {
        return flipDisplacement;
    }

}

@@ -2620,100 +2620,8 @@
  1);
-%0A        g.fillOval(xP, yP, sigmaP, sigmaP);%0A        /* Draw the orientation line, if any */
 %0A%0A  
@@ -2804,32 +2804,50 @@
     
-xP += dxy;%0A        yP +=
+int xP1 = xP + dxy;%0A        int yP1 = yP +
  dxy
@@ -2873,16 +2873,19 @@
 e(xP
+1
 , yP
+1
 , xP
+1
  - d
@@ -2893,16 +2893,17 @@
 , yP
+1
  - dy);%0A
     
@@ -2898,16 +2898,148 @@
  - dy);%0A
+%0A        if (flipDisplacement) %7B%0A            xP -= dx;%0A            yP -= dy;%0A        %7D%0A        g.fillOval(xP, yP, sigmaP, sigmaP);%0A%0A
     %7D%0A%0A 

Allow displacement to be flipped for 2D too