package school_project.Parsers;

import school_project.Map;

import java.io.*;

public class SerializeParser implements FileParser{

    @Override
    public Map getLevel(File file, boolean saved_data) throws IOException {
        // saved_data is ignored in this case because the file is serialized data and it already knows if should have saved_data or not at this point
        FileInputStream fileStream = new FileInputStream(file);
        ObjectInputStream objectStream = new ObjectInputStream(fileStream);
        try {
            return (Map) objectStream.readObject();
        } catch (ClassNotFoundException e) {
            throw new IOException("the serialized file format has not found any object in the file");
        }
    }

    @Override
    public void saveLevel(File file, Map levelData, boolean save_data) throws IOException {
        FileOutputStream fileStream = new FileOutputStream(file);
        ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
        objectStream.writeObject(save_data ? levelData : levelData.getCleanedMap());

        objectStream.close();
        fileStream.close();
    }


}
