package l2.universe.scripts.hellbound;

import l2.universe.gameserver.instancemanager.HellboundManager;
import l2.universe.gameserver.model.actor.L2Npc;
import l2.universe.gameserver.model.actor.instance.L2PcInstance;
import l2.universe.gameserver.model.quest.Quest;
import l2.universe.gameserver.model.quest.QuestState;

public class Natives extends Quest
{
	private static final int Native = 32357;
	private static final int Nativ = 32362;
	private static final int Nati = 32363;

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

	public Natives(int id, String name, String descr)
	{
		super(id, name, descr);
		addStartNpc(Native);
		addTalkId(Native);
		addFirstTalkId(Native);
		addFirstTalkId(Nativ);
		addFirstTalkId(Nati);
		addTalkId(Nativ);
		addTalkId(Nati);
	}

	@Override
    public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = "";
		QuestState st = player.getQuestState(getName());
		if (st == null)
			st = newQuestState(player);

		if (event.equalsIgnoreCase("FreeSlaves"))
		{
			final long badges = st.getQuestItemsCount(9674);
			if (badges >= 5 && HellboundManager.getInstance().getTrust() < 1064000)
			{
				st.takeItems(9674, 5);
				HellboundManager.getInstance().increaseTrust(5);
				npc.decayMe();
                startQuestTimer("resp", 25000, npc, null);
				htmltext = "32357-4.htm";
				if (HellboundManager.getInstance().getTrust() >= 1064000)
                {
					htmltext = "32357-5.htm";
                    cancelQuestTimer("resp", null, null);
                }
			}
			else
				htmltext = "32357-3.htm";
		}
        else if (event.equalsIgnoreCase("resp") && npc != null)
			npc.spawnMe();
		return htmltext;
	}

	@Override
    public String onFirstTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = "";
		QuestState st = player.getQuestState(getName());
		if (st == null)
			st = newQuestState(player);

		final int hellboundLevel = HellboundManager.getInstance().getLevel();
		final int x = npc.getX();
		final int y = npc.getY();

		if (hellboundLevel == 9 && npc.getNpcId() == Native)
		{
			boolean xIsOk = false;
			for (int i = 4475; i <= 10960; i++)
				if (i == x)
					xIsOk = true;
			boolean yIsOk = false;
			for (int i = 247925; i <= 254415; i++)
				if (i == y)
					yIsOk = true;
			if (xIsOk && yIsOk)
				htmltext = "32357.htm";
		}
		else if (hellboundLevel < 9 && npc.getNpcId() == Native)
			htmltext = "32357-1.htm";
		else if (hellboundLevel > 9 && npc.getNpcId() == Native)
			htmltext = "32357-2.htm";
		else if (npc.getNpcId() == Nati)
		{
			if (hellboundLevel <= 5)
				htmltext = "32363-1.htm";
			if (hellboundLevel > 5)
				htmltext = "32363-2.htm";
		}
		else if (npc.getNpcId() == Nativ)
		{
			if (hellboundLevel <= 5)
				htmltext = "32362-1.htm";
			if (hellboundLevel > 5)
				htmltext = "32362-2.htm";
		}

		return htmltext;
	}
}
