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

import java.util.EventListener;

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.artemis.utils.ImmutableBag;
import com.badlogic.gdx.physics.box2d.Body;
import com.custardgames.hotel.entities.ecs.components.Box2dBodyComponent;
import com.custardgames.hotel.entities.ecs.components.Box2dSpriteComponent;
import com.custardgames.hotel.entities.ecs.components.FogComponent;
import com.custardgames.hotel.events.BeginContactEvent;
import com.custardgames.hotel.managers.EventManager;

public class FogSystem extends EntityProcessingSystem implements EventListener
{
	@Mapper
	ComponentMapper<Box2dBodyComponent> bodyComponents;
	@Mapper
	ComponentMapper<Box2dSpriteComponent> spriteComponents;

	@SuppressWarnings("unchecked")
	public FogSystem()
	{
		super(Aspect.getAspectForAll(Box2dBodyComponent.class, Box2dSpriteComponent.class, FogComponent.class));
		EventManager.get_instance().register(BeginContactEvent.class, this);
	}

	@Override
	protected void process(Entity arg0)
	{

	}

	public void handleBeginContactEvent(BeginContactEvent e)
	{
		ImmutableBag<Entity> entities = getActives();
		for (int x = 0; x < entities.size(); x++)
		{
			Body body = bodyComponents.get(entities.get(x)).getBody();
			if (e.getFa().getBody() == body || e.getFb().getBody() == body)
			{
				if (e.getFa().getBody() == body)
				{
					if (e.getFb().getUserData().equals("player"))
					{
						Box2DSprite sprite = spriteComponents.get(entities.get(x)).getSprite();
						sprite.setAlpha(0);
					}
				}
				else
				{
					if (e.getFa().getUserData().equals("player"))
					{
						Box2DSprite sprite = spriteComponents.get(entities.get(x)).getSprite();
						sprite.setAlpha(0);
					}
				}
			}
		}
	}

}
