/*
 * Main.fx
 *
 * Created on 8 Jul, 2009, 6:14:16 PM
 */

package draw;


import javafx.scene.shape.ClosePath;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.io.Storage;
import javafx.data.pull.PullParser;
import javafx.scene.shape.PathElement;

/**
 @author Rakesh Menon
 */

public class PathUtil {

    public var elements : PathElement[];

    var entry = Storage {
        source: "TerrainPath.dat"
    }

    init {
        entry.resource.maxLength = 262144;
    }

    public function save() : Void {

        var resource = entry.resource;
        var os = resource.openOutputStream(true);
        var out = new java.io.PrintStream(os);

        var count = 0;
        out.println("<path>");
        count += 7;
        for(pathElement in elements) {
            if(pathElement instanceof MoveTo) {
                var xmlElement = "<element type=\"MoveTo\" "
                "x=\"{(pathElement as MoveTo).x}\" "
                "y=\"{(pathElement as MoveTo).y}\" />";
                count += xmlElement.length();
                if(count < 262000) { out.println(xmlElement)}
            else if(pathElement instanceof LineTo) {
                var xmlElement = "<element type=\"LineTo\" "
                "x=\"{(pathElement as LineTo).x}\" "
                "y=\"{(pathElement as LineTo).y}\" />";
                count += xmlElement.length();
                if(count < 262000) { out.println(xmlElement)}
            else if(pathElement instanceof ClosePath) {
                out.println("<element type=\"ClosePath\" />");
            }
        }
        out.println("</path>");

        out.close();
    }

    public function load() : Void {

        var resource = entry.resource;
        var in = resource.openInputStream();

        var pullParser = PullParser {

            input: in

            onEvent: function(e) {
                if(e.type == PullParser.START_ELEMENT) {
                    var type = e.getAttributeValue("type");
                    if(type.equals("MoveTo")) {
                        var x = e.getAttributeValue("x");
                        var y = e.getAttributeValue("y");
                        insert MoveTo {
                            x: Number.parseFloat(x)
                            y: Number.parseFloat(y)
                        into elements;
                    else if(type.equals("LineTo")) {
                        var x = e.getAttributeValue("x");
                        var y = e.getAttributeValue("y");
                        insert LineTo {
                            x: Number.parseFloat(x)
                            y: Number.parseFloat(y)
                        into elements;
                    else if(type.equals("ClosePath")) {
                        insert ClosePath { } into elements;
                    }
                }
            }
        };

        try {
            pullParser.parse();
        catch (e : java.lang.Exception) {
            //No Storage Resource!
            if((sizeof elements== 0) {
                insert MoveTo x: 50 y: 280into elements;
                insert LineTo x: 400 y: 280into elements;
            }
            insert ClosePath { } into elements;
        }

        in.close();
    }
}