/*
 * 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 q4_LongLiveLordOfFlame extends Quest
{
	private final static int HONEY_KHANDAR = 1541;
	private final static int BEAR_FUR_CLOAK = 1542;
	private final static int BLOODY_AXE = 1543;
	private final static int ANCESTOR_SKULL = 1544;
	private final static int SPIDER_DUST = 1545;
	private final static int DEEP_SEA_ORB = 1546;
	//Reward
	private final static int CLUB = 4;
	
	private final static int[][] NPC_GIFTS =
	{
		{ 30585, BEAR_FUR_CLOAK },
		{ 30566, HONEY_KHANDAR },
		{ 30562, BLOODY_AXE },
		{ 30560, ANCESTOR_SKULL },
		{ 30559, SPIDER_DUST },
		{ 30587, DEEP_SEA_ORB }
	};
	private final static int[] NPC = {30559, 30560, 30562, 30566, 30578, 30585, 30587};
			
	private State CREATED;
	private State STARTED;
	private State COMPLETED;
	
	public q4_LongLiveLordOfFlame(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[4]);
		for(int npc1 : NPC)
			addTalkId(npc1);

		STARTED.addQuestDrop(NPC[4],HONEY_KHANDAR,1);
		STARTED.addQuestDrop(NPC[4],BEAR_FUR_CLOAK,1);
		STARTED.addQuestDrop(NPC[4],BLOODY_AXE,1);
		STARTED.addQuestDrop(NPC[4],ANCESTOR_SKULL,1);
		STARTED.addQuestDrop(NPC[4],SPIDER_DUST,1);
		STARTED.addQuestDrop(NPC[4],DEEP_SEA_ORB,1);
	}

	public String onEvent(String event, QuestState st)
	{
		
		if(event.equalsIgnoreCase("30578-03.htm"))
		{
			st.set("cond", "1");
			st.setState(STARTED);
			st.playSound("ItemSound.quest_accept");
		}
		return event;
	}

	public String onTalk(L2NpcInstance npc, L2PcInstance talker)
	{
		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 = talker.getQuestState(getName());
		
		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 == 30578)
		{
			if(cond == 0)
			{
				if(st.getPlayer().getRace().ordinal() != 3)
				{
					htmltext = "30578-00.htm";
					st.exitQuest(true);
				}
				else if(st.getPlayer().getLevel() >= 2)
					htmltext = "30578-02.htm";
				else
				{
					htmltext = "30578-01.htm";
					st.exitQuest(true);
				}
			}
			else if(cond == 1)
				htmltext = "30578-04.htm";
			else if(cond == 2)
			{
				htmltext = "30578-06.htm";
				for(int[] item : NPC_GIFTS)
				st.takeItems(item[1], 1);
				st.giveItems(CLUB, 1);
				st.giveItems(57,1850);
			    st.addExpAndSp(4254,335);
				st.unset("cond");
				st.playSound("ItemSound.quest_finish");
				st.setState(COMPLETED);
				st.exitQuest(false);
			}
		}
		else if(cond == 1)
			for(int Id[] : NPC_GIFTS)
				if(Id[0] == npcId)
				{
					int item = Id[1];
					if(st.getQuestItemsCount(item) > 0)
						htmltext = npcId + "-02.htm";
					else
					{
						st.giveItems(item, 1);
						htmltext = npcId + "-01.htm";
						int count = 0;
						for(int[] item1 : NPC_GIFTS)
							count += st.getQuestItemsCount(item1[1]);
						if(count == 6)
						{
							st.set("cond", "2");
							st.playSound("ItemSound.quest_middle");
						}
						else
							st.playSound("Itemsound.quest_itemget");
					}
					return htmltext;
				}
		return htmltext;
	}
	public static void main(String[] args)
    {
		_log.info("4_LongLiveLordOfFlame:     load");
    	new q4_LongLiveLordOfFlame(4, "4_LongLiveLordOfFlame", "Long Live the Paagrio Lord");    	
    }
}