package com.custardgames.hotel.entities.ecs.systems;

import net.dermetfan.gdx.graphics.g2d.Box2DSprite;

import com.artemis.Aspect;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.annotations.Mapper;
import com.artemis.systems.EntityProcessingSystem;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.custardgames.hotel.entities.ecs.components.Box2dBodyComponent;
import com.custardgames.hotel.entities.ecs.components.Box2dInteractionComponent;
import com.custardgames.hotel.entities.ecs.components.Box2dSpriteComponent;
import com.custardgames.hotel.entities.ecs.components.ObjectDataComponent;
import com.custardgames.hotel.entities.ecs.components.ObjectInteractComponent;

public class ObjectInteractSystem extends EntityProcessingSystem
{

	@Mapper
	ComponentMapper<Box2dInteractionComponent> interactionComponents;
	@Mapper
	ComponentMapper<ObjectInteractComponent> objectInteractComponents;
	@Mapper
	ComponentMapper<ObjectDataComponent> objectDataComponents;

	private AssetManager assetManager;
	
	@SuppressWarnings("unchecked")
	public ObjectInteractSystem(AssetManager assetManager)
	{
		super(Aspect.getAspectForAll(ObjectInteractComponent.class, ObjectDataComponent.class, Box2dInteractionComponent.class));
		this.assetManager = assetManager;
	}

	@Override
	protected void process(Entity e)
	{
		Box2dInteractionComponent interactionComponent = interactionComponents.get(e);
		ObjectDataComponent objectDataComponent = objectDataComponents.get(e);
		ObjectInteractComponent objectInteractComponent = objectInteractComponents.get(e);

		for (Fixture f : interactionComponent.getCollisions())
		{
			if (f.getBody().getUserData() != null)
			{
				if (f.getBody().getUserData() instanceof Entity)
				{
					ObjectDataComponent otherObjectDataComponent = ((Entity) f.getBody().getUserData()).getComponent(ObjectDataComponent.class);
					ObjectInteractComponent otherObjectInteractComponent = ((Entity) f.getBody().getUserData()).getComponent(ObjectInteractComponent.class);

					if (otherObjectInteractComponent != null && otherObjectDataComponent != null)
					{
						if (!objectDataComponent.isTaken() && !otherObjectDataComponent.isTaken())
						{
							if (objectInteractComponent.getObjectName().equals("radio") && otherObjectInteractComponent.getObjectName().equals("cd"))
							{
								e.getComponent(Box2dSpriteComponent.class).setSprite(new Box2DSprite(new Sprite((Texture) assetManager.get("images/radio2.png"))));
								((Entity) f.getBody().getUserData()).getComponent(Box2dBodyComponent.class).getBody().setTransform(e.getComponent(Box2dBodyComponent.class).getBody().getPosition(), 0);
								((Entity) f.getBody().getUserData()).getComponent(Box2dSpriteComponent.class).setSprite(new Box2DSprite(new Sprite((Texture) assetManager.get("images/glass.png"))));
								objectInteractComponent.setObjectName("broken");
								otherObjectInteractComponent.setObjectName("glass");
							}
							if (objectInteractComponent.getObjectName().equals("cake") && otherObjectInteractComponent.getObjectName().equals("matches"))
							{
								e.getComponent(Box2dSpriteComponent.class).setSprite(new Box2DSprite(new Sprite((Texture) assetManager.get("images/cake2.png"))));
								((Entity) f.getBody().getUserData()).getComponent(Box2dBodyComponent.class).getBody().setTransform(e.getComponent(Box2dBodyComponent.class).getBody().getPosition(), 0);
								((Entity) f.getBody().getUserData()).getComponent(Box2dSpriteComponent.class).setSprite(new Box2DSprite(new Sprite((Texture) assetManager.get("images/cd.png"))));
								objectInteractComponent.setObjectName("broken");
								otherObjectInteractComponent.setObjectName("cd");
							}
							if (objectInteractComponent.getObjectName().equals("poo") && otherObjectInteractComponent.getObjectName().equals("plunger"))
							{
								e.getComponent(Box2dSpriteComponent.class).setSprite(new Box2DSprite(new Sprite((Texture) assetManager.get("images/toilet-water.png"))));
								((Entity) f.getBody().getUserData()).getComponent(Box2dBodyComponent.class).getBody().setTransform(e.getComponent(Box2dBodyComponent.class).getBody().getPosition(), 0);
								((Entity) f.getBody().getUserData()).getComponent(Box2dSpriteComponent.class).getSprite().setAlpha(0);
								objectInteractComponent.setObjectName("water");
								otherObjectDataComponent.setMoveable(false);
								otherObjectInteractComponent.setObjectName("broken");
							}
							if (objectInteractComponent.getObjectName().equals("water") && otherObjectInteractComponent.getObjectName().equals("glass"))
							{
								e.getComponent(Box2dSpriteComponent.class).setSprite(new Box2DSprite(new Sprite((Texture) assetManager.get("images/toilet.png"))));
								((Entity) f.getBody().getUserData()).getComponent(Box2dBodyComponent.class).getBody().setTransform(e.getComponent(Box2dBodyComponent.class).getBody().getPosition(), 0);
								((Entity) f.getBody().getUserData()).getComponent(Box2dSpriteComponent.class).setSprite(new Box2DSprite(new Sprite((Texture) assetManager.get("images/glass2.png"))));
								objectInteractComponent.setObjectName("broken");
								otherObjectInteractComponent.setObjectName("glass2");
							}
							if (objectInteractComponent.getObjectName().equals("plant") && otherObjectInteractComponent.getObjectName().equals("glass2"))
							{
								((Entity) f.getBody().getUserData()).getComponent(Box2dBodyComponent.class).getBody().setTransform(e.getComponent(Box2dBodyComponent.class).getBody().getPosition(), 0);
								((Entity) f.getBody().getUserData()).getComponent(Box2dSpriteComponent.class).setSprite(new Box2DSprite(new Sprite((Texture) assetManager.get("images/key.png"))));
								objectInteractComponent.setObjectName("broken");
								otherObjectInteractComponent.setObjectName("key");
							}
							if (objectInteractComponent.getObjectName().equals("locked-door") && otherObjectInteractComponent.getObjectName().equals("key"))
							{
								e.getComponent(Box2dSpriteComponent.class).getSprite().setAlpha(0);
								e.getComponent(Box2dBodyComponent.class).getBody().setTransform(0, 0, 0);
								((Entity) f.getBody().getUserData()).getComponent(Box2dBodyComponent.class).getBody().setTransform(e.getComponent(Box2dBodyComponent.class).getBody().getPosition(), 0);
								((Entity) f.getBody().getUserData()).getComponent(Box2dSpriteComponent.class).getSprite().setAlpha(0);
								objectInteractComponent.setObjectName("broken");
								otherObjectInteractComponent.setObjectName("broken");
							}
						}
					}
				}
			}
		}
	}

}