/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package quests.Q162_CurseOfTheUndergroundFortress;

import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.base.Race;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;
import net.sf.l2j.util.Rnd;

public class Q162_CurseOfTheUndergroundFortress extends Quest
{
	private static final String qn = "Q162_CurseOfTheUndergroundFortress";
	
	// NPC
	private static final int Unoren = 30147;
	
	// Monsters
	private static final int Shade_Horror = 20033;
	private static final int Dark_Terror = 20345;
	private static final int Mist_Terror = 20371;
	private static final int Dungeon_Skeleton_Archer = 20463;
	private static final int Dungeon_Skeleton = 20464;
	private static final int Dread_Soldier = 20504;
	
	// Items
	private static final int BONE_FRAGMENT = 1158;
	private static final int ELF_SKULL = 1159;
	
	// Rewards
	private static final int ADENA = 57;
	private static final int BONE_SHIELD = 625;
	
	public Q162_CurseOfTheUndergroundFortress(int questId, String name, String descr)
	{
		super(questId, name, descr);
		
		questItemIds = new int[]
		{
			BONE_FRAGMENT,
			ELF_SKULL
		};
		
		addStartNpc(Unoren);
		addTalkId(Unoren);
		
		addKillId(Shade_Horror, Dark_Terror, Mist_Terror, Dungeon_Skeleton_Archer, Dungeon_Skeleton, Dread_Soldier);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (event.equalsIgnoreCase("30147-04.htm"))
		{
			st.set("cond", "1");
			st.setState(STATE_STARTED);
			st.playSound(QuestState.SOUND_ACCEPT);
		}
		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState(qn);
		String htmltext = getNoQuestMsg();
		if (st == null)
			return htmltext;
		
		switch (st.getState())
		{
			case STATE_CREATED:
				if (player.getRace() == Race.DarkElf)
				{
					htmltext = "30147-00.htm";
					st.exitQuest(true);
				}
				else if (player.getLevel() >= 12)
					htmltext = "30147-02.htm";
				else
				{
					htmltext = "30147-01.htm";
					st.exitQuest(true);
				}
				break;
			
			case STATE_STARTED:
				int cond = st.getInt("cond");
				if (cond == 1)
					htmltext = "30147-05.htm";
				else if (cond == 2 && st.getQuestItemsCount(ELF_SKULL) >= 3 && st.getQuestItemsCount(BONE_FRAGMENT) >= 10)
				{
					htmltext = "30147-06.htm";
					st.takeItems(ELF_SKULL, -1);
					st.takeItems(BONE_FRAGMENT, -1);
					st.rewardItems(ADENA, 24000);
					st.giveItems(BONE_SHIELD, 1);
					st.exitQuest(false);
					st.playSound(QuestState.SOUND_FINISH);
				}
				break;
			
			case STATE_COMPLETED:
				htmltext = getAlreadyCompletedMsg();
				break;
		}
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = checkPlayerCondition(player, npc, "cond", "1");
		if (st == null)
			return null;
		
		if (Rnd.get(100) < 25)
		{
			switch (npc.getNpcId())
			{
				case Dungeon_Skeleton:
				case Dungeon_Skeleton_Archer:
				case Dread_Soldier:
					if (st.getQuestItemsCount(BONE_FRAGMENT) < 10)
					{
						st.giveItems(BONE_FRAGMENT, 1);
						
						if (st.getQuestItemsCount(BONE_FRAGMENT) >= 10 && st.getQuestItemsCount(ELF_SKULL) >= 3)
						{
							st.playSound(QuestState.SOUND_MIDDLE);
							st.set("cond", "2");
						}
						else
							st.playSound(QuestState.SOUND_ITEMGET);
					}
					break;
				
				case Shade_Horror:
				case Dark_Terror:
				case Mist_Terror:
					if (st.getQuestItemsCount(ELF_SKULL) < 3)
					{
						st.giveItems(ELF_SKULL, 1);
						
						if (st.getQuestItemsCount(BONE_FRAGMENT) >= 10 && st.getQuestItemsCount(ELF_SKULL) >= 3)
						{
							st.playSound(QuestState.SOUND_MIDDLE);
							st.set("cond", "2");
						}
						else
							st.playSound(QuestState.SOUND_ITEMGET);
					}
					break;
			}
		}
		return null;
	}
	
	public static void main(String[] args)
	{
		new Q162_CurseOfTheUndergroundFortress(162, qn, "Curse of the Underground Fortress");
	}
}