package net.mgsx.game.plugins.core.tools;

import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

import net.mgsx.game.core.EditorScreen;
import net.mgsx.game.core.components.Movable;
import net.mgsx.game.core.helpers.NativeService;
import net.mgsx.game.core.helpers.NativeService.DefaultCallback;
import net.mgsx.game.core.storage.EntityGroupStorage;
import net.mgsx.game.core.storage.LoadConfiguration;
import net.mgsx.game.core.tools.ClickTool;

public class ImportPatchTool extends ClickTool {
	private FileHandle file;

	public ImportPatchTool(EditorScreen editor) {
		super("Import", editor);
	}

	@Override
	protected void activate() {
		NativeService.instance.openSaveDialog(new DefaultCallback() {
			@Override
			public void selected(FileHandle selectedFile) {
				file = selectedFile;
			}
			@Override
			public boolean match(FileHandle file) {
				return file.extension().equals("json");
			}
			@Override
			public String description() {
				return "Patch files (json)";
			}
		});
	}

	@Override
	protected void create(final Vector2 position) 
	{
		LoadConfiguration config = new LoadConfiguration();
		config.assets = editor.assets;
		config.registry = editor.registry;
		config.engine = editor.entityEngine;
		for(Entity entity : EntityGroupStorage.loadForEditing(file.path(), config)){
			Movable movable = entity.getComponent(Movable.class);
			if(movable != null){
				movable.move(entity, new Vector3(position.x, position.y, 0)); // sprite plan
			}
		}
		// TODO update things in GUI ?
		
	}
}
@@ -544,16 +544,63 @@
 ickTool;
+%0Aimport net.mgsx.game.plugins.btree.BTreeModel;
 %0A%0Apublic
@@ -1545,16 +1545,18 @@
 ble.move
+To
 (entity,
@@ -1617,16 +1617,151 @@
 an%0A%09%09%09%7D%0A
+%09%09%09BTreeModel btree = BTreeModel.components.get(entity);%0A%09%09%09if(btree != null)%7B%0A%09%09%09%09btree.enabled = true;%0A%09%09%09%09btree.remove = true;%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09//

enable btree when importing