/*
 * 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.L2Clan;
import com.l2jcore.Game.model.actor.instance.L2NpcInstance;
import com.l2jcore.Game.model.actor.instance.L2PcInstance;
import com.l2jcore.Game.Event.Siege.clanhallsiege.BanditStrongholdSiege;
import com.l2jcore.Game.model.quest.Quest;
import com.l2jcore.Game.model.quest.QuestState;
import com.l2jcore.Game.model.quest.State;

public class q504_CompetitionForTheBanditStronghold extends Quest
{
	private final static int MESSENGER = 35437;
	private final static int[] ITEMS = {4332, 5009};
	private final static int[] MOBS = {20570, 20571, 20572, 20573, 20574};
	
	private State CREATED;
	private State STARTED;
	
	public q504_CompetitionForTheBanditStronghold(int questId, String name, String descr)
	{
		super(questId, name, descr);
		CREATED = new State("Start", this);
		STARTED = new State("Started", this);
		this.setInitialState(CREATED);

		addStartNpc(MESSENGER);
		addTalkId(MESSENGER);
		for(int MOB_ID : MOBS)
		{
			addKillId(MOB_ID);
		}
	}

	public String onEvent(String event, QuestState st)
	{
		if(event.equalsIgnoreCase("a2.htm"))
		{
			st.set("cond", "1");
			st.setState(STARTED);
			st.playSound("ItemSound.quest_accept");
		}
		else if(event.equalsIgnoreCase("a4.htm"))
		{
			if(st.getQuestItemsCount(ITEMS[0]) == 30)
			{
				st.takeItems(ITEMS[0],-30);
				st.giveItems(ITEMS[1],1);
				st.playSound("ItemSound.quest_finish");
				st.exitQuest(true);
			}
			else
				event = "a5.htm";
		}
		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>";
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
		L2Clan clan = talker.getClan();
		if(clan == null)
		{
			htmltext = "a6.htm";
			return htmltext;
		}
		if(clan.getLevel() < 4)
		{
			htmltext = "a6.htm";
			return htmltext;
		}
		if(clan.getLeaderName() != talker.getName())
		{
			htmltext = "a6.htm";
			return htmltext;
		}
		if(BanditStrongholdSiege.getInstance().isRegistrationPeriod())
		{
			if(npcId == MESSENGER)
			{
				if(cond == 0)
					htmltext = "a1.htm";
				else if(cond > 1)
					htmltext = "a3.htm";
			}
		}
		return htmltext;
	}

	@Override
	public String onKill(L2NpcInstance npc, L2PcInstance killer, boolean isPet)
	{
		QuestState st = killer.getQuestState(getName());
		if(st != null)
		{
			if(st.getState() == STARTED)
			{
				st.giveItems(ITEMS[0],1);
				st.playSound("ItemSound.quest_itemget");
				if(st.getQuestItemsCount(ITEMS[0]) == 30)
					st.set("cond", "2");
			}
		}
        return super.onKill(npc,killer,isPet);
    }
	
	public static void main(String[] args)
	{
		_log.info("504_CompetitionForTheBanditStronghold:     load");
		new q504_CompetitionForTheBanditStronghold(504, "504_CompetitionForTheBanditStronghold", "Competition for the Bandit Stronghold");    	
	}
}