package leigh.mud.engine.io;

import leigh.mud.engine.MudEngine;
import leigh.mud.engine.Room;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;

/**
 * Incomplete implementation of a world exporter. This implementation is just enough to output the rooms without
 * any extended information.
 *
 * @author Alex Leigh
 */
public class DataExporter {
    private final Collection<Room> rooms;

    public DataExporter(Collection<Room> rooms) {
        this.rooms = rooms;
    }

    public void exportRooms(Path dst) throws IOException {
        BufferedWriter writer = Files.newBufferedWriter(dst);
        try (CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
                .withHeader("ROOM_ID", "TITLE", "DESC"))) {
            for (Room r : rooms) {
                csvPrinter.printRecord(r.getId(), r.getTitle(), r.getDesc());
            }
        }
    }

    public static void main(String args[]) throws IOException {
        MudEngine engine = new MudEngine();
        DataExporter exp = new DataExporter(engine.getRooms().values());
        exp.exportRooms(Paths.get("world_rooms.csv"));
    }
}
