/*
 * 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.Q002_WhatWomenWant;

import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.base.Race;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;

public class Q002_WhatWomenWant extends Quest
{
	private static final String qn = "Q002_WhatWomenWant";
	
	// NPCs
	private static final int ARUJIEN = 30223;
	private static final int MIRABEL = 30146;
	private static final int HERBIEL = 30150;
	private static final int GREENIS = 30157;
	
	// Items
	private static final int ARUJIENS_LETTER1 = 1092;
	private static final int ARUJIENS_LETTER2 = 1093;
	private static final int ARUJIENS_LETTER3 = 1094;
	private static final int POETRY_BOOK = 689;
	private static final int GREENIS_LETTER = 693;
	
	// Rewards
	private static final int ADENA = 57;
	private static final int MYSTICS_EARRING = 113;
	
	public Q002_WhatWomenWant(int questId, String name, String descr)
	{
		super(questId, name, descr);
		
		questItemIds = new int[]
		{
			ARUJIENS_LETTER1,
			ARUJIENS_LETTER2,
			ARUJIENS_LETTER3,
			POETRY_BOOK,
			GREENIS_LETTER
		};
		
		addStartNpc(ARUJIEN);
		addTalkId(ARUJIEN, MIRABEL, HERBIEL, GREENIS);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (event.equalsIgnoreCase("30223-04.htm"))
		{
			st.set("cond", "1");
			st.setState(STATE_STARTED);
			st.giveItems(ARUJIENS_LETTER1, 1);
			st.playSound(QuestState.SOUND_ACCEPT);
		}
		else if (event.equalsIgnoreCase("30223-08.htm"))
		{
			st.set("cond", "4");
			st.playSound(QuestState.SOUND_MIDDLE);
			st.takeItems(ARUJIENS_LETTER3, -1);
			st.giveItems(POETRY_BOOK, 1);
		}
		else if (event.equalsIgnoreCase("30223-09.htm"))
		{
			st.takeItems(ARUJIENS_LETTER3, -1);
			st.rewardItems(ADENA, 450);
			st.playSound(QuestState.SOUND_FINISH);
			st.exitQuest(false);
		}
		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = getNoQuestMsg();
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		switch (st.getState())
		{
			case STATE_CREATED:
				if (player.getRace() != Race.Elf && player.getRace() != Race.Human)
					htmltext = "30223-00.htm";
				else if (player.getLevel() < 2)
					htmltext = "30223-01.htm";
				else
					htmltext = "30223-02.htm";
				break;
			
			case STATE_STARTED:
				int cond = st.getInt("cond");
				switch (npc.getNpcId())
				{
					case ARUJIEN:
						if (st.hasQuestItems(ARUJIENS_LETTER1))
							htmltext = "30223-05.htm";
						else if (st.hasQuestItems(ARUJIENS_LETTER3))
							htmltext = "30223-07.htm";
						else if (st.hasQuestItems(ARUJIENS_LETTER2))
							htmltext = "30223-06.htm";
						else if (st.hasQuestItems(POETRY_BOOK))
							htmltext = "30223-11.htm";
						else if (st.hasQuestItems(GREENIS_LETTER))
						{
							htmltext = "30223-10.htm";
							st.takeItems(GREENIS_LETTER, 1);
							st.giveItems(MYSTICS_EARRING, 1);
							st.playSound(QuestState.SOUND_FINISH);
							st.exitQuest(false);
						}
						break;
					
					case MIRABEL:
						if (cond == 1)
						{
							htmltext = "30146-01.htm";
							st.set("cond", "2");
							st.playSound(QuestState.SOUND_MIDDLE);
							st.takeItems(ARUJIENS_LETTER1, 1);
							st.giveItems(ARUJIENS_LETTER2, 1);
						}
						else if (cond > 1)
							htmltext = "30146-02.htm";
						break;
					
					case HERBIEL:
						if (cond == 2)
						{
							htmltext = "30150-01.htm";
							st.set("cond", "3");
							st.playSound(QuestState.SOUND_MIDDLE);
							st.takeItems(ARUJIENS_LETTER2, 1);
							st.giveItems(ARUJIENS_LETTER3, 1);
						}
						else if (cond > 2)
							htmltext = "30150-02.htm";
						break;
					
					case GREENIS:
						if (cond > 0 && cond < 4)
							htmltext = "30157-01.htm";
						else if (cond == 4)
						{
							htmltext = "30157-02.htm";
							st.takeItems(POETRY_BOOK, -1);
							st.giveItems(GREENIS_LETTER, 1);
							st.set("cond", "5");
							st.playSound(QuestState.SOUND_MIDDLE);
						}
						else if (cond == 5)
							htmltext = "30157-03.htm";
						break;
				}
				break;
			
			case STATE_COMPLETED:
				htmltext = getAlreadyCompletedMsg();
				break;
		}
		
		return htmltext;
	}
	
	public static void main(String[] args)
	{
		new Q002_WhatWomenWant(2, qn, "What Women Want");
	}
}