/*
 * 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 q5_MinersFavor extends Quest
{
	//NPC
	private final static int BOLTER = 30554;
	private final static int SHARI = 30517;
	private final static int GARITA = 30518;
	private final static int REED = 30520;
	private final static int BRUNON = 30526;
	//QuestItem
	private final static int BOLTERS_LIST = 1547;
	private final static int MINING_BOOTS = 1548;
	private final static int MINERS_PICK = 1549;
	private final static int BOOMBOOM_POWDER = 1550;
	private final static int REDSTONE_BEER = 1551;
	private final static int BOLTERS_SMELLY_SOCKS = 1552;
	//Item
	private final static int NECKLACE = 906;
	
	private State CREATED;
	private State STARTED;
	private State COMPLETED;
	
	public q5_MinersFavor(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(BOLTER);
		addTalkId(BOLTER);
		addTalkId(SHARI);
		addTalkId(GARITA);
		addTalkId(REED);
		addTalkId(BRUNON);
		
		STARTED.addQuestDrop(BOLTER,MINING_BOOTS,1);
		STARTED.addQuestDrop(BOLTER,MINERS_PICK,1);
		STARTED.addQuestDrop(BOLTER,BOOMBOOM_POWDER,1);
		STARTED.addQuestDrop(BOLTER,REDSTONE_BEER,1);
		STARTED.addQuestDrop(BOLTER,BOLTERS_LIST,1);
		STARTED.addQuestDrop(BOLTER,BOLTERS_SMELLY_SOCKS,1);
	}

	public String onEvent(String event, QuestState st)
	{
		if(event.equalsIgnoreCase("30554-03.htm"))
		{
			st.set("cond", "1");
			st.setState(STARTED);
			st.playSound("ItemSound.quest_accept");
			st.giveItems(BOLTERS_LIST, 1);
			st.giveItems(BOLTERS_SMELLY_SOCKS, 1);
		}
		else if(event.equalsIgnoreCase("30526-02.htm"))
		{
			st.takeItems(BOLTERS_SMELLY_SOCKS, 1);
			st.giveItems(MINERS_PICK, 1);
			if(st.getQuestItemsCount(BOLTERS_LIST) > 0 && st.getQuestItemsCount(MINING_BOOTS) + st.getQuestItemsCount(MINERS_PICK) + st.getQuestItemsCount(BOOMBOOM_POWDER) + st.getQuestItemsCount(REDSTONE_BEER) == 4)
			{
				st.set("cond", "2");
				st.playSound("ItemSound.quest_middle");
			}
			else
				st.playSound("Itemsound.quest_itemget");

		}
		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 == BOLTER)
		{
			if(cond == 0)
			{
				if(st.getPlayer().getLevel() >= 2)
					htmltext = "30554-02.htm";
				else
				{
					htmltext = "30554-01.htm";
					st.exitQuest(true);
				}
			}
			else if(cond == 1)
				htmltext = "30554-04.htm";
			else if(cond == 2 && st.getQuestItemsCount(MINING_BOOTS) + st.getQuestItemsCount(MINERS_PICK) + st.getQuestItemsCount(BOOMBOOM_POWDER) + st.getQuestItemsCount(REDSTONE_BEER) == 4)
			{
				htmltext = "30554-06.htm";
				st.takeItems(MINING_BOOTS, 1);
				st.takeItems(MINERS_PICK, 1);
				st.takeItems(BOOMBOOM_POWDER, 1);
				st.takeItems(REDSTONE_BEER, 1);
				st.takeItems(BOLTERS_LIST, 1);
				st.giveItems(NECKLACE, 1);
				st.unset("cond");
				st.playSound("ItemSound.quest_finish");
				st.exitQuest(false);
			}
		}
		else if(cond == 1 && st.getQuestItemsCount(BOLTERS_LIST) > 0)
		{
			if(npcId == SHARI)
			{
				if(st.getQuestItemsCount(BOOMBOOM_POWDER) == 0)
				{
					htmltext = "30517-01.htm";
					st.giveItems(BOOMBOOM_POWDER, 1);
					st.playSound("Itemsound.quest_itemget");
				}
				else
					htmltext = "30517-02.htm";
			}
			else if(npcId == GARITA)
			{
				if(st.getQuestItemsCount(MINING_BOOTS) == 0)
				{
					htmltext = "30518-01.htm";
					st.giveItems(MINING_BOOTS, 1);
					st.playSound("Itemsound.quest_itemget");
				}
				else
					htmltext = "30518-02.htm";
			}
			else if(npcId == REED)
			{
				if(st.getQuestItemsCount(REDSTONE_BEER) == 0)
				{
					htmltext = "30520-01.htm";
					st.giveItems(REDSTONE_BEER, 1);
					st.playSound("Itemsound.quest_itemget");
				}
				else
					htmltext = "30520-02.htm";
			}
			else if(npcId == BRUNON && st.getQuestItemsCount(BOLTERS_SMELLY_SOCKS) > 0)
				if(st.getQuestItemsCount(MINERS_PICK) == 0)
					htmltext = "30526-01.htm";
				else
					htmltext = "30526-03.htm";
			if(st.getQuestItemsCount(BOLTERS_LIST) > 0 && st.getQuestItemsCount(MINING_BOOTS) + st.getQuestItemsCount(MINERS_PICK) + st.getQuestItemsCount(BOOMBOOM_POWDER) + st.getQuestItemsCount(REDSTONE_BEER) == 4)
			{
				st.set("cond", "2");
				st.playSound("Itemsound.quest_itemget");
			}
		}
		return htmltext;
	}
	
	public static void main(String[] args)
	{
		_log.info("5_MinersFavor:     load");
		new q5_MinersFavor(5, "5_MinersFavor", "Miner's Favor");    	
	}
}