/*
 * 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 q31_SecretBuriedInTheSwamp extends Quest
{
	private final static int[] NPC = {31555, 31661, 31662, 31663, 31664, 31665};
	private final static int KRORINS_JOURNAL = 7252;
	private final static String DEFAULT = "<html><body>You are either not carrying out your quest or don't meet the criteria.</body></html>";

	private State CREATED;
	private State STARTED;
	private State COMPLETED;
	
	public q31_SecretBuriedInTheSwamp(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(NPC[0]);
		for(int NPC_ID : NPC)
			addTalkId(NPC_ID);
	}

	public String onEvent(String event, QuestState st)
	{
		String htmltext = event;
		int cond = st.getInt("cond");
		State id = st.getState();
		if(event.equalsIgnoreCase("31555-1.htm") && id == CREATED)
		{
			st.set("cond", "1");
			st.setState(STARTED);
			st.playSound("ItemSound.quest_accept");
		}
		else if(event.equalsIgnoreCase("31665-1.htm") && cond == 1)
		{
			st.set("cond","2");
			st.playSound("ItemSound.quest_itemget");
			st.giveItems(KRORINS_JOURNAL,1);
		}
		else if(event.equalsIgnoreCase("31555-4.htm") && cond == 2)
			st.set("cond","3");
		else if(event.equalsIgnoreCase("31661-1.htm") && cond == 3)
			st.set("cond","4");
		else if(event.equalsIgnoreCase("31662-1.htm") && cond == 4)
			st.set("cond","5");
		else if(event.equalsIgnoreCase("31662-1.htm") && cond == 5)
			st.set("cond","6");
		else if(event.equalsIgnoreCase("31662-1.htm") && cond == 6)
		{
			st.set("cond","7");
			st.playSound("ItemSound.quest_middle");
		}
		else if(event.equalsIgnoreCase("31555-7.htm") && cond == 7)
		{
			st.takeItems(KRORINS_JOURNAL,-1);
			st.addExpAndSp(130000,0);
			st.giveItems(57,40000);
			st.playSound("ItemSound.quest_finish");
			st.setState(COMPLETED);
		}
		else if(!event.equalsIgnoreCase("31663-0a.htm"))
			htmltext = DEFAULT;
		return htmltext;
	}

	public String onTalk(L2NpcInstance npc, L2PcInstance talker)
	{
		QuestState st = talker.getQuestState(getName());
		String htmltext = DEFAULT;
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
		if(st == null) return htmltext;
		State id = st.getState();
		if(id == COMPLETED)
			htmltext = "<html><body>This quest has already been completed.</body></html>";
		else if(npcId == NPC[0])
		{
			if(cond == 0)
			{
				if(talker.getLevel() >= 66)
					htmltext = "31555-0.htm";
				else
				{
					htmltext = "31555-0a.htm";
					st.exitQuest(true);
				}
			}
			else if(cond == 1)
				htmltext = "31555-2.htm";
			else if(cond == 2)
				htmltext = "31555-3.htm";
			else if(cond == 3)
				htmltext = "31555-5.htm";
			else if(cond == 7)
				htmltext = "31555-6.htm";
		}
		else if(id == STARTED)
		{
			if(npcId == NPC[5])
			{
				if(cond == 1)
					htmltext = "31665-0.htm";
				else if(cond == 2)
					htmltext = "31665-2.htm";
			}
			else if(npcId == NPC[1])
			{
				if(cond == 3)
					htmltext = "31661-0.htm";
				else if(cond > 3)
					htmltext = "31661-2.htm";
			}
			else if(npcId == NPC[2])
			{
				if(cond == 4)
					htmltext = "31662-0.htm";
				else if(cond > 4)
					htmltext = "31662-2.htm";
			}
			else if(npcId == NPC[3])
			{
				if(cond == 5)
					htmltext = "31663-0.htm";
				else if(cond > 5)
					htmltext = "31663-2.htm";
			}
			else if(npcId == NPC[4])
			{
				if(cond == 6)
					htmltext = "31664-0.htm";
				else if(cond > 6)
					htmltext = "31664-2.htm";
			}
		}
		return htmltext;
	}

	public static void main(String[] args)
	{
		_log.info("31_SecretBuriedInTheSwamp:     load");
		new q31_SecretBuriedInTheSwamp(31, "31_SecretBuriedInTheSwamp", "Secret Buried In The Swamp");    	
	}
}