package view.editor.gameeditor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import api.IEntity;
import api.ILevel;
import datamanagement.XMLWriter;
import javafx.collections.ObservableList;
import view.enums.DefaultStrings;
import view.enums.Indexes;


public class GameSaver {

	
	@SuppressWarnings("unchecked")
	public void saveGame(ObservableList<ILevel> levels, ObservableList<IEntity> entityList, List<String> details){
		String fileName = getDirectory(details);
		new XMLWriter<List<String>>().writeToFile(fileName + DefaultStrings.METADATA_LOC.getDefault(), details);
		new XMLWriter<List<IEntity>>().writeToFile(fileName+DefaultStrings.ENTITIES_LOC.getDefault(), new ArrayList<IEntity>(entityList));
		saveLevels(levels, fileName);
	}


	private void saveLevels(ObservableList<ILevel> levels, String fileName) {
		File levelFile = new File(fileName + DefaultStrings.LEVELS_LOC.getDefault());
		levelFile.mkdirs();
		for(ILevel level: levels){
			new XMLWriter<ILevel>().writeToFile(fileName + DefaultStrings.LEVELS_LOC.getDefault() + level.getName() + DefaultStrings.XML.getDefault(), level);
		}
	}

	
	private String getDirectory(List<String> details) {
		String fileName = DefaultStrings.CREATE_LOC.getDefault() + details.get(Indexes.GAME_NAME.getIndex());
		File file = new File(fileName);
		if(file.exists()){
			file.delete();
		}
		file.mkdirs();
		return fileName;
	}
}

@@ -448,16 +448,144 @@
 tails)%7B%0A
+%09%09ArrayList%3CString%3E metaData = new ArrayList%3C%3E(details);%0A%09%09if(!levels.isEmpty())%7B%0A%09%09%09metaData.add(levels.get(0).getName());%0A%09%09%7D%0A
 %09%09String
@@ -716,23 +716,24 @@
 ault(), 
-details
+metaData
 );%0A%09%09new

save first level