package Actions.LevelMaintenance;

import Entities.Entity;
import Models.WorldModel;
import Util.Layer;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Flash
 */
public class SaveLevel extends AbstractAction {

    private WorldModel world;
    private File file;

    public SaveLevel(File file, WorldModel world) {
        this.file = file;
        this.world = world;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        Element root = new Element("Level1");
        Element size = new Element("Size");
        size.setAttribute("width", String.valueOf(world.getSize().width));
        size.setAttribute("height", String.valueOf(world.getSize().height));

        Entity[][] sea = world.getSea();
        for (int x = 0; x < sea.length; x++) {
            for (int y = 0; y < sea[x].length; y++) {
                Entity s = sea[x][y];
                if (s != null) {
                    Element obstacle = new Element("Sea");
                    obstacle.setAttribute("type", s.toString());
                    obstacle.setAttribute("x", String.valueOf(x));
                    obstacle.setAttribute("y", String.valueOf(y));
                    root.addContent(obstacle);
                }
            }
        }

        Entity[][] background = world.getBackground();
        for (int x = 0; x < background.length; x++) {
            for (int y = 0; y < background[x].length; y++) {
                Entity bg = background[x][y];
                if (bg != null) {
                    Element obstacle = new Element("Background");
                    obstacle.setAttribute("type", bg.toString());
                    obstacle.setAttribute("x", String.valueOf(x));
                    obstacle.setAttribute("y", String.valueOf(y));
                    root.addContent(obstacle);
                }
            }
        }

        Entity[][] obstacles = world.getObstacles();
        for (int x = 0; x < obstacles.length; x++) {
            for (int y = 0; y < obstacles[x].length; y++) {
                Entity obs = obstacles[x][y];
                if (obs != null) {
                    Element obstacle = new Element("Obstacle");
                    obstacle.setAttribute("type", obs.toString());
                    obstacle.setAttribute("x", String.valueOf(x));
                    obstacle.setAttribute("y", String.valueOf(y));
                    root.addContent(obstacle);
                }
            }
        }
        
        try {
            Document document = new Document(root);
            XMLOutputter serializer = new XMLOutputter();
            FileWriter writer;
            writer = new FileWriter(file);
            serializer.setFormat(Format.getPrettyFormat());
            serializer.output(document, writer);
        } catch (IOException ex) {
            Logger.getLogger(SaveLevel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

@@ -82,27 +82,8 @@
 el;%0A
-import Util.Layer;%0A
 impo
@@ -193,34 +193,8 @@
 on;%0A
-import java.util.HashMap;%0A
 impo
@@ -712,17 +712,16 @@
 nEvent e
-v
 ) %7B%0A    

Test changes that I forgot to undo