package net.mgsx.game.plugins.box2d.editors;

import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.physics.box2d.Filter;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;

import net.mgsx.game.core.annotations.Editable;
import net.mgsx.game.core.annotations.EnumType;
import net.mgsx.game.core.plugins.EntityEditorPlugin;
import net.mgsx.game.core.ui.EntityEditor;
import net.mgsx.game.plugins.box2d.components.Box2DBodyModel;
import net.mgsx.game.plugins.box2d.components.Box2DFixtureModel;

public class Box2DBodyEditorPlugin implements EntityEditorPlugin
{
	@Editable
	public static class FixtureEditor
	{
		private Fixture fixture;
		
		public FixtureEditor(Fixture fixture) {
			super();
			this.fixture = fixture;
			Filter filter = fixture.getFilterData();
			categoryBits = filter.categoryBits;
			maskBits = filter.maskBits;
			groupIndex = filter.groupIndex;
			
		}

		@Editable(type=EnumType.BITS)
		public short categoryBits;
		
		@Editable(type=EnumType.BITS)
		public short maskBits;
		
		@Editable
		public short groupIndex;
		
		@Editable
		public void applyFilter(){
			Filter filter = fixture.getFilterData();
			filter.categoryBits = categoryBits;
			filter.maskBits = maskBits;
			filter.groupIndex = groupIndex;
			fixture.setFilterData(filter);
		}
	}
	
	@Override
	public Actor createEditor(Entity entity, Skin skin) 
	{
		Box2DBodyModel model = entity.getComponent(Box2DBodyModel.class);
		
		Table table = new Table(skin);
		
		table.add(new EntityEditor(model.body, skin));
		
		for(final Box2DFixtureModel fix : model.fixtures){
			table.row();
			
			FixtureEditor fixtureEditor = new FixtureEditor(fix.fixture);
			
			table.add(new EntityEditor(fixtureEditor, true, skin));
			
			table.row();
			table.add(new EntityEditor(fix, skin));
		}
		
		return table;
		
	}

}

@@ -129,55 +129,8 @@
 er;%0A
-import com.badlogic.gdx.physics.box2d.Fixture;%0A
 impo
@@ -716,31 +716,39 @@
 private 
+Box2D
 Fixture
- fixture
+Model model
 ;%0A%09%09%0A%09%09p
@@ -771,23 +771,31 @@
 tor(
+Box2D
 Fixture
- fixture
+Model model
 ) %7B%0A
@@ -818,25 +818,21 @@
 his.
-fixture = fixture
+model = model
 ;%0A%09%09
@@ -840,32 +840,38 @@
 Filter filter = 
+model.
 fixture.getFilte
@@ -1219,16 +1219,22 @@
 ilter = 
+model.
 fixture.
@@ -1358,16 +1358,22 @@
 dex;%0A%09%09%09
+model.
 fixture.
@@ -1399,16 +1399,206 @@
 r);%0A%09%09%7D%0A
+%09%09@Editable%0A%09%09public void applyFilterDef()%7B%0A%09%09%09Filter filter = model.def.filter;%0A%09%09%09filter.categoryBits = categoryBits;%0A%09%09%09filter.maskBits = maskBits;%0A%09%09%09filter.groupIndex = groupIndex;%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09@O
@@ -1948,24 +1948,16 @@
 itor(fix
-.fixture
 );%0A%09%09%09%0A%09

store box2D filters