/*
 * 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 q30_ChestCaughtWithABaitOfFire extends Quest
{
	private final static int[] NPC = {31577, 30629};
	private final static int[] ITEM = {6511, 7628, 916};
	
	private State CREATED;
	private State STARTED;
	private State COMPLETED;
	
	public q30_ChestCaughtWithABaitOfFire(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;
		if(event.equalsIgnoreCase("31577-02.htm"))
		{
			st.set("cond", "1");
			st.setState(STARTED);
			st.playSound("ItemSound.quest_accept");
		}
		else if(event.equalsIgnoreCase("31577-04a.htm") && st.getQuestItemsCount(ITEM[0]) != 0)
		{
			htmltext = "31577-04.htm";
			st.playSound("ItemSound.quest_middle");
			st.giveItems(ITEM[1], 1);
			st.takeItems(ITEM[0],-1);
			st.set("cond","2");
		}
		else if(event.equalsIgnoreCase("30629-02.htm") && st.getQuestItemsCount(ITEM[1]) != 0)
		{
			htmltext = "30629-03.htm";
			st.playSound("ItemSound.quest_middle");
			st.giveItems(ITEM[2], 1);
			st.takeItems(ITEM[1],-1);
			st.unset("cond");
			st.setState(COMPLETED);
		}
		return htmltext;
	}

	public String onTalk(L2NpcInstance npc, L2PcInstance talker)
	{
		QuestState st = talker.getQuestState(getName());
		String htmltext = "<html><body>You are either not carrying out your quest or don't meet the criteria.</body></html>";
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
		if(st == null) return htmltext;
		State id = st.getState();
		if(id == CREATED)
		{
			QuestState req = talker.getQuestState("53_LinnaeusSpecialBait");
			if(talker.getLevel() >= 61 && req != null && req.getState() == COMPLETED)
				htmltext = "31577-01.htm";
			else
			{
				st.exitQuest(true);
				htmltext = "31577-00.htm";
			}
				
		}
		else if(id == STARTED)
		{
			if(npcId == NPC[0])
			{
				if(cond == 1)
				{
					if(st.getQuestItemsCount(ITEM[0]) != 0)
						htmltext = "31577-03.htm";
					else
						htmltext = "31577-03a.htm";
				}
				else
					htmltext = "31577-05.htm";
			}
			else
				htmltext = "30629-01.htm";
		}
		else if(id == COMPLETED)
			htmltext = "<html><body>This quest has already been completed.</body></html>";
		
		return htmltext;
	}

	public static void main(String[] args)
	{
		_log.info("30_ChestCaughtWithABaitOfFire:     load");
		new q30_ChestCaughtWithABaitOfFire(30, "30_ChestCaughtWithABaitOfFire", "Chest Caught With A Bait Of Fire");    	
	}
}