package scratchlib.project;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import scratchlib.objects.ScratchObject;
import scratchlib.objects.ScratchObjectStore;
import scratchlib.writer.ScratchOutputStream;


/**
 * Represents a Scratch project file that has version information, metadata, a
 * stage with sprites, etc.
 */
public class ScratchProject
{
    private final ScratchVersion version;
    private ScratchObjectStore info = new ScratchObjectStore(ScratchObject.NIL);
    private ScratchObjectStore stage = new ScratchObjectStore(
            ScratchObject.NIL);

    /**
     * @param version This project's version.
     */
    public ScratchProject(ScratchVersion version)
    {
        this.version = version;
    }

    /**
     * @return This project's version.
     */
    public ScratchVersion getVersion()
    {
        return version;
    }

    /**
     * @return The object that is the project's info section.
     */
    public ScratchObjectStore getInfoSection()
    {
        return info;
    }

    /**
     * Sets the project's info section.
     * 
     * @param info The new info section.
     */
    public void setInfoSection(ScratchObjectStore info)
    {
        this.info = info;
    }

    /**
     * @return The object that is the project's stage / content section.
     */
    public ScratchObjectStore getStageSection()
    {
        return stage;
    }

    /**
     * Sets the project's stage / content section.
     * 
     * @param stage The new stage section.
     */
    public void setStageSection(ScratchObjectStore stage)
    {
        this.stage = stage;
    }

    /**
     * Writes this project to the given output stream.
     * 
     * @param out The stream to write to.
     * @throws IOException
     */
    public void writeTo(ScratchOutputStream out) throws IOException
    {
        // write header
        out.writeString(version.getHeader());

        // write info section
        ByteArrayOutputStream infoBytes = getSectionBytes(info);
        out.write32bitUnsignedInt(infoBytes.size());
        infoBytes.writeTo(out);

        // write stage section
        stage.writeTo(out, this);
    }

    /**
     * Writes the given object store into a byte array output stream.
     * 
     * @param section The section to write.
     * @return A {@code ByteArrayOutputStream} containing the written bytes.
     * @throws IOException
     */
    private ByteArrayOutputStream getSectionBytes(ScratchObjectStore section)
            throws IOException
    {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ScratchOutputStream out = new ScratchOutputStream(bout);

        section.writeTo(out, this);

        return bout;
    }
}

@@ -176,16 +176,262 @@
 tStore;%0A
+import scratchlib.objects.fixed.collections.ScratchObjectDictionary;%0Aimport scratchlib.objects.fixed.data.ScratchObjectString;%0Aimport scratchlib.objects.fixed.data.ScratchObjectUtf8;%0Aimport scratchlib.objects.user.morphs.ScratchObjectStageMorph;%0A
 import s
@@ -975,32 +975,1295 @@
 rsion = version;
+%0A%0A        // populate info section%0A%0A        ScratchObjectDictionary dict = new ScratchObjectDictionary();%0A        %7B%0A            dict.put(new ScratchObjectString(%22os-version%22),%0A                    new ScratchObjectString(%22NT%22));%0A            dict.put(new ScratchObjectString(%22platform%22),%0A                    new ScratchObjectString(%22Win32%22));%0A            dict.put(new ScratchObjectString(%22language%22),%0A                    new ScratchObjectString(%22en%22));%0A            dict.put(new ScratchObjectString(%22history%22),%0A                    new ScratchObjectUtf8(%22%5Cr%22));%0A            dict.put(new ScratchObjectString(%22scratch-version%22),%0A                    new ScratchObjectString(version.getVersionString()));%0A            // thumbnail omitted (-%3E ColorForm)%0A            dict.put(new ScratchObjectString(%22comment%22), new ScratchObjectUtf8(%0A                    %22https://github.com/JangoBrick/scratchlib%22));%0A            dict.put(new ScratchObjectString(%22author%22),%0A                    new ScratchObjectUtf8(%22%22));%0A            // keepOnStage omitted (-%3E Boolean)%0A            // penTrails omitted (-%3E Form)%0A        %7D%0A        info.set(dict);%0A%0A        // populate stage section%0A%0A        ScratchObjectStageMorph stageMorph = new ScratchObjectStageMorph();%0A%0A        stage.set(stageMorph);
 %0A    %7D%0A%0A    /**%0A

Add defaults for project dictionary, stage section