package ai.zones.hellbound;

import l2.universe.scripts.ai.L2AttackableAIScript;
import l2.universe.gameserver.datatables.ItemTable;
import l2.universe.gameserver.instancemanager.HellboundManager;
import l2.universe.gameserver.model.L2ItemInstance;
import l2.universe.gameserver.model.L2Object;
import l2.universe.gameserver.model.L2Skill;
import l2.universe.gameserver.model.actor.L2Npc;
import l2.universe.gameserver.model.actor.instance.L2PcInstance;
import l2.universe.util.Rnd;

public class Chimeras extends L2AttackableAIScript
{
	private static final int[] NPC_IDS =
	{
		22349, 22350, 22351, 22352
	};

	private static final int CELTUS = 22353;
  // Temp fix  - http://l2universe.info/
	private static final int[][] locs = {
		{ 
			4276, 237245, -3310 
		}, 
		{ 
			11437, 236788, -1949 
		}, 
		{ 
			7647, 235672, -1977
		}, 
		{ 
			1882, 233520, -3315 
		}
	};
	
		
	public Chimeras(int questId, String name, String descr)
	{
		super(questId, name, descr);
		for (int i : NPC_IDS)
			addSkillSeeId(i);
		
		addSkillSeeId(CELTUS);
		
		if (HellboundManager.getInstance().getLevel() >= 7)
		{
			int[] loc = locs[Rnd.get(locs.length)];
			int respawnTime = (72 + Rnd.get(72)) * 100;
			HellboundManager.getInstance().addSpawn(CELTUS, loc[0], loc[1], loc[2], 0, respawnTime);
		}		
	}

	private void dropItem(L2PcInstance player, L2Npc npc, int itemId, int count)
	{
		L2ItemInstance item = ItemTable.getInstance().createItem("Forces", itemId, count, player);
		item.dropMe(player, npc.getX(), npc.getY(), npc.getZ());
	}

	@Override
    public String onSkillSee(L2Npc npc, L2PcInstance player, L2Skill skill, L2Object[] targets, boolean isPet)
	{
		for (L2Object obj : targets)
		{
			if (npc != obj)
				return null;
		}
		
		final int maxHp = npc.getMaxHp();
		final double currentHp = npc.getStatus().getCurrentHp();
		if (HellboundManager.getInstance().getLevel() >= 7 && skill.getId() == 2359	&& currentHp < maxHp * 0.1)
		{
			final int npcId = npc.getNpcId();
			if (contains(NPC_IDS, npcId))
			{
				final int rnd = Rnd.get(100);
				if (rnd < 20)
					dropItem(player, npc, 9681, 1);
				else if (rnd < 100)
					dropItem(player, npc, 9680, 1);
				npc.onDecay();
			}
			else if (npcId == CELTUS)
			{
				dropItem(player, npc, 9682, 1);
				npc.onDecay();
			}
		}

		return super.onSkillSee(npc, player, skill, targets, isPet);
	}

	public static void main(String[] args)
	{
		new Chimeras(-1, "Chimeras", "ai");
	}
}
