/*
 * 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.java;

import com.l2jcore.Game.model.actor.instance.L2NpcInstance;
import com.l2jcore.Game.model.actor.instance.L2PcInstance;
import com.l2jcore.Game.model.quest.Quest;
import com.l2jcore.Game.model.quest.QuestState;
import com.l2jcore.Game.model.quest.State;

public class q16_TheComingDarkness extends Quest
{
	//npc
	private final static int HIERARCH = 31517;
	//ALTAR_LIST (MOB_ID, cond)
	private final static int[][] ALTAR_LIST = {{31512, 1}, {31513, 2}, {31514, 3}, {31515, 4}, {31516, 5}};
	//items
	private final static int CRYSTAL_OF_SEAL = 7167;
	
	private State CREATED;
	private State STARTED;
	private State COMPLETED;

	public q16_TheComingDarkness(int questId, String name, String descr)
	{
		super(questId, name, descr);
		CREATED = new State("Start", this);
		STARTED = new State("Started", this);
		COMPLETED = new State("Completed", this);
		this.setInitialState(CREATED);

		addStartNpc(HIERARCH);
		for(int[] element : ALTAR_LIST)
			addTalkId(element[0]);
	}

	public String onEvent(String event, QuestState st)
	{
		if(event.equalsIgnoreCase("31517-02.htm"))
		{
			st.setState(STARTED);
			st.set("cond", "1");
			st.giveItems(CRYSTAL_OF_SEAL, 5);
			st.playSound("ItemSound.quest_accept");
		}
		for(int[] element : ALTAR_LIST)
			if(event.equalsIgnoreCase(String.valueOf(element[0]) + "-02.htm"))
			{
				st.takeItems(CRYSTAL_OF_SEAL, 1);
				st.set("cond", String.valueOf(element[1] + 1));
				st.playSound("ItemSound.quest_middle");
			}
		return event;
	}

	public String onTalk(L2NpcInstance npc, L2PcInstance talker)
	{
		QuestState st = talker.getQuestState(getName());
		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>";
		
		if( st.getState() == COMPLETED)
		{
			htmltext = "<html><body>This quest has already been completed.</body></html>";
			return htmltext;
		}
		
		if(st == null) return htmltext;
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
		if(npcId == 31517)
			if(cond < 1)
			{
				if(st.getPlayer().getLevel() < 61)
				{
					htmltext = "31517-00.htm";
					st.exitQuest(true);
				}
				else
					htmltext = "31517-01.htm";
			}
			else if(cond > 0 && cond < 6 && st.getQuestItemsCount(CRYSTAL_OF_SEAL) > 0)
				htmltext = "31517-02r.htm";
			else if(cond > 0 && cond < 6 && st.getQuestItemsCount(CRYSTAL_OF_SEAL) < 1)
			{
				htmltext = "31517-proeb.htm";
				st.exitQuest(false);
			}
			else if(cond > 5 && st.getQuestItemsCount(CRYSTAL_OF_SEAL) < 1)
			{
				htmltext = "31517-03.htm";
				st.addExpAndSp(221958, 0);
				st.playSound("ItemSound.quest_finish");
				st.exitQuest(false);
			}
		for(int[] element : ALTAR_LIST)
			if(npcId == element[0])
				if(cond == element[1])
				{
					if(st.getQuestItemsCount(CRYSTAL_OF_SEAL) > 0)
						htmltext = String.valueOf(element[0]) + "-01.htm";
					else
						htmltext = String.valueOf(element[0]) + "-03.htm";
				}
				else if(cond == element[1] + 1)
					htmltext = String.valueOf(element[0]) + "-04.htm";
		return htmltext;
	}
	
	public static void main(String[] args)
	{
		_log.info("16_TheComingDarkness:     load");
		new q16_TheComingDarkness(16, "16_TheComingDarkness", "The Coming Darkness");    	
	}
}
