package quests.java;

import com.l2jcore.Config;
import com.l2jcore.Game.model.actor.instance.L2NpcInstance;
import com.l2jcore.Game.model.actor.instance.L2PcInstance;
import com.l2jcore.Game.model.quest.QuestState;
import com.l2jcore.Game.model.quest.State;
import com.l2jcore.Game.model.quest.Quest;
 
public class q645_GhostsOfBatur extends Quest
{
	public static final int KARUDA = 32017;
	public static final int ITEM = 8089;
	private State CREATED;
	public static final String qn = "645_GhostsOfBatur";
	
	private static final int[] MOBS =
	{
		22007,22009,22010,22011,22012,22013,22014,22015,22016
	};
	
	public q645_GhostsOfBatur(int id, String name, String descr)
	{
		super(id,name,descr);
		
		addStartNpc(KARUDA);
		addTalkId(KARUDA);

		for (int i : MOBS)
			addKillId(i);
			
	}
	
	public String onAdvEvent (String event, L2NpcInstance npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState(qn);
		String htmltext = "";
		if (st != null)
		{
			if (event.equalsIgnoreCase("32017-03.htm"))
			{
				if (player.getLevel() < 80)
				{
					htmltext = "32017-02.htm";
					st.exitQuest(true);
				}
				else
				{
					st.set("cond","1");
					CREATED = new State("Start", this);
					this.setInitialState(CREATED);
					st.playSound("ItemSound.quest_accept");
				}
			}
			else if (event.equalsIgnoreCase("Reward"))
			{
				htmltext = "32017-06.htm";
			}
			else
			{
				if (event.equalsIgnoreCase("RDB"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9968,1);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("RDP"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9969,1);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("RDW"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9970,1);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("RDK"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9971,1);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("RDH"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9972,1);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("RDC"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9973,1);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("RDM"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9974,1);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("RDN"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9975,1);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("LEO"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9628,62);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("ADA"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9629,33);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
				else if (event.equalsIgnoreCase("ORI"))
				{
					if (st.getQuestItemsCount(ITEM) >= 500)
					{
						st.takeItems(ITEM,500);
						st.giveItems(9630,41);
						st.playSound("ItemSound.quest_finish");
						st.exitQuest(true);
						htmltext = "32017-07.htm";
					}
					else
						htmltext = "32017-04.htm";
				}
			}
		}
		return htmltext;
	}
	
	public String onTalk(L2NpcInstance npc,L2PcInstance player)
	{
		String htmltext = "<html><body>You are either not on a quest that involves this NPC, or you don't meet this NPC's minimum quest requirements.</body></html>";
		QuestState st = player.getQuestState(qn);
		
		if (st != null)
		{
			int cond = st.getInt("cond");
			
			if (cond == 0)
				htmltext = "32017-01.htm";
			else if (cond == 1)
				htmltext = "32017-04.htm";
			else if (cond == 2)
			{
				if (st.getQuestItemsCount(ITEM) >= 500)
					htmltext = "32017-05.htm";
				else
					htmltext = "32017-01.htm";
			}
		}
		return htmltext;
	}
	
	public String onKill(L2NpcInstance npc,L2PcInstance player, boolean isPet)
	{
		long chance = 35;
		int npcId = npc.getNpcId();
		int qty = 0;
		QuestState st = player.getQuestState(qn);
		
		if (st != null)
		{
			for (int id : MOBS)
			{
				if (npcId == id)
				{
					if (npcId == 22705)
						chance = 40;
						
					qty =(int) (chance * Config.RATE_DROP_QUEST) / 100;
					
					if (qty < 1)
						qty = 1;
					
					if (st.getRandom(100) <= chance)
					{
						st.giveItems(ITEM, qty);
				
						if (st.getQuestItemsCount(ITEM) >= 500)
						{
							st.playSound("ItemSound.quest_middle");
							st.set("cond","2");
						}
						else
							st.playSound("ItemSound.quest_itemget");
					}
				}
			}
		}
		return super.onKill(npc, player, isPet);
	}
	
	public static void main(String[] args)
	{
		_log.info("q645_GhostsOfBatur:     load");
		new q645_GhostsOfBatur(645, qn, "Ghosts of Batur");
	}
}