package l2.universe.scripts.hellbound;

import javolution.util.FastMap;
import l2.universe.gameserver.ai.CtrlIntention;
import l2.universe.gameserver.datatables.ItemTable;
import l2.universe.gameserver.instancemanager.HellboundManager;
import l2.universe.gameserver.model.L2CharPosition;
import l2.universe.gameserver.model.L2ItemInstance;
import l2.universe.gameserver.model.L2Object;
import l2.universe.gameserver.model.L2World;
import l2.universe.gameserver.model.actor.L2Attackable;
import l2.universe.gameserver.model.actor.L2Npc;
import l2.universe.gameserver.model.actor.instance.L2FriendlyMobInstance;
import l2.universe.gameserver.model.actor.instance.L2PcInstance;
import l2.universe.gameserver.model.quest.QuestState;
import l2.universe.gameserver.network.serverpackets.CreatureSay;
import l2.universe.util.Rnd;
import l2.universe.scripts.ai.L2AttackableAIScript;

public class NativeQuarySlaves extends L2AttackableAIScript
{
    private final int[] SlaveWalkTo = {-25451, 252291, -3252};
    private final int[] NativeSlave = {22322, 22323};
    private final int QuarrySlave = 32299;
    private final int[] SlaveMasters = {22320, 22321};
    private final int[] QuarryMasters = {22344, 22345, 22346, 22347};

    private final int[] QuarryDropItems = {1876, 1885, 9628};
    private final FastMap<Integer, L2Npc> slaves = new FastMap<Integer, L2Npc>();

    private final FastMap<Integer, L2Npc> slaves2 = new FastMap<Integer, L2Npc>();

    public NativeQuarySlaves(int id, String name, String descr)
    {
        super(id, name, descr);
        registerMobs(new int[] {22320, 22321,QuarrySlave});
        addStartNpc(QuarrySlave);
        addTalkId(QuarrySlave);
        addFirstTalkId(QuarrySlave);
    }

    @Override
    public String onAdvEvent (String event, L2Npc npc, L2PcInstance player)
    {
        if (event.equals("Delete") && npc != null)
        {
            npc.setIsInvul(false);
            npc.deleteMe();
        }
        else if (event.equals("followCheck") && npc != null)
        {
            if (npc.isDead()) return null;
            startQuestTimer("followCheck", 1000, npc, null);
            if (npc.getY() > 249000 && npc.getY() < 249880 && npc.getX() > -6230 && npc.getX() < -5230)
            {
                npc.broadcastPacket(new CreatureSay(npc.getObjectId(), 0, npc.getName(), "Thank you for saving me! Here is a small gift"));
                for (int i=0; i<Rnd.get(3); i++)
                {
                    final L2ItemInstance item = ItemTable.getInstance().createItem("Gift", QuarryDropItems[Rnd.get(QuarryDropItems.length)], 1, null, null);
                    item.dropMe(npc, npc.getX()+35 - Rnd.get(70), npc.getY()+35 - Rnd.get(70), npc.getZ());
                }
                npc.doDie(npc);
                npc.decayMe();
                HellboundManager.getInstance().increaseTrust(10);
                HellboundManager.getInstance().increaseCond(1);
                return null;
            }
            for (final L2Object obj : L2World.getInstance().getVisibleObjects(npc, 350))
				if (obj instanceof L2Attackable)
                {
                    final L2Attackable mob = (L2Attackable) obj;
                    if (contains(QuarryMasters, mob.getNpcId()) && !mob.isDead())
                    {
                        mob.setTarget(npc);
                        mob.addDamageHate(npc, 0, 999);
                        mob.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, npc);
                        mob.setRunning();
                    }
                }
        }
        else if (event.equals("FollowMe"))
        {
            npc.getAI().setIntention(CtrlIntention.AI_INTENTION_FOLLOW, player);
            startQuestTimer("followCheck", 1000, npc, null);
        }
        return null;
    }

    @Override
    public String onFirstTalk (L2Npc npc, L2PcInstance player)
    {
        QuestState st = player.getQuestState(getName());
		if (st == null)
			st = newQuestState(player);
        String htmltext = "<html><body>"+npc.getName()+":<br>Who are you ? Do not interrupting me from work.</body></html>";
        if (HellboundManager.getInstance().getLevel() == 5)
        {
            htmltext = "<html><body>"+npc.getName()+":<br>";
            htmltext += "Ermm, are you here to rescue me?<br><br>";
            htmltext += "<a action=\"bypass -h Quest NativeQuarySlaves FollowMe\">Yes, follow me quietly</a><br1>";
            htmltext += "</body></html>";
        }
        return htmltext;
    }

    @Override
    public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
    {
        if (contains(SlaveMasters,npc.getNpcId()))
        {
            final L2Npc slave1 = slaves.get(npc.getObjectId());
            final L2Npc slave2 = slaves2.get(npc.getObjectId());
            if (slave1 != null)
            {
                slave1.setIsInvul(true);
                slave1.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(SlaveWalkTo[0], SlaveWalkTo[1], SlaveWalkTo[2], 3500));
                startQuestTimer("Delete", 3000, slave1, null);
            }
            if (slave2 != null)
            {
                slave2.setIsInvul(true);
                slave2.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(SlaveWalkTo[0], SlaveWalkTo[1], SlaveWalkTo[2], 3500));
                startQuestTimer("Delete", 3000, slave2, null);
            }
        }
        return null;
    }

    @Override
    public String onSpawn(L2Npc npc)
    {
        if (HellboundManager.getInstance().getLevel() >= 5)
            return null;
        if (contains(SlaveMasters,npc.getNpcId()))
        {
            final L2Npc slave1 = addSpawn(NativeSlave[0], npc.getX() + 50 - Rnd.get(100), npc.getY() + 50 - Rnd.get(100), npc.getZ(), 0, false, 0, false);
            slave1.getAI().setIntention(CtrlIntention.AI_INTENTION_FOLLOW, npc);
            slaves.put(npc.getObjectId(), slave1);
            final L2Npc slave2 = addSpawn(NativeSlave[1], npc.getX() + 50 - Rnd.get(100) + 20, npc.getY() + 50 - Rnd.get(100) + 10, npc.getZ(), 0, false, 0, false);
            slave2.getAI().setIntention(CtrlIntention.AI_INTENTION_FOLLOW, npc);
            slaves2.put(npc.getObjectId(), slave2);
        }
        else if (npc.getNpcId() == QuarrySlave)
            ((L2FriendlyMobInstance)npc).setAllowAttack(false);
        return null;
    }
    
    public static void main(String[] args)
	{
		new NativeQuarySlaves(-1,"NativeQuarySlaves","ai");
	}
}
