/*
 * 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;
import com.l2jcore.Game.model.base.Race;

public class q2_WhatWomenWant1 extends Quest
{	
	private final static int ARUJIEN = 30223;
	private final static int MIRABEL = 30146;
	private final static int HERBIEL = 30150;
	private final static int GREENIS = 30157;

	private final static int ARUJIENS_LETTER1 = 1092;
	private final static int ARUJIENS_LETTER2 = 1093;
	private final static int ARUJIENS_LETTER3 = 1094;
	private final static int POETRY_BOOK = 689;
	private final static int GREENIS_LETTER = 693;

	private final static int ADENA = 57;
	private final static int BEGINNERS_POTION = 1073;
	private final static int MYSTIC_EARRING = 113;
	
	private State CREATED;
	private State STARTED;
	private State COMPLETED;
	
	public q2_WhatWomenWant1(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(ARUJIEN);
		addTalkId(ARUJIEN);
		addTalkId(MIRABEL);
		addTalkId(HERBIEL);
		addTalkId(GREENIS);

		STARTED.addQuestDrop(ARUJIEN,GREENIS_LETTER,1);
		STARTED.addQuestDrop(ARUJIEN,ARUJIENS_LETTER3,1);
		STARTED.addQuestDrop(ARUJIEN,ARUJIENS_LETTER1,1);
		STARTED.addQuestDrop(ARUJIEN,ARUJIENS_LETTER2,1);
		STARTED.addQuestDrop(ARUJIEN,POETRY_BOOK,1);
	}

	public String onEvent(String event, QuestState qs)
	{
		String htmltext = event;
		if(event.equalsIgnoreCase("30223-04.htm"))
		{
			qs.giveItems(ARUJIENS_LETTER1, 1);
			qs.set("cond", "1");
			qs.setState(STARTED);
			qs.playSound("ItemSound.quest_accept");
		}
		else if(event.equalsIgnoreCase("30223-08.htm"))
		{
			qs.takeItems(ARUJIENS_LETTER3, 1);
			qs.giveItems(POETRY_BOOK, 1);
			qs.set("cond", "4");
			qs.playSound("ItemSound.quest_middle");
		}
		else if(event.equalsIgnoreCase("30223-10.htm"))
		{
			qs.takeItems(ARUJIENS_LETTER3, 1);
			qs.giveItems(ADENA, 2300);
			qs.addExpAndSp(4254, 335);
			qs.playSound("ItemSound.quest_finish");
		    qs.set("cond","0");
			qs.exitQuest(false);
		}
		return htmltext;
	}
	
	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 (talker.getRace()!=Race.Elf)
		{
			htmltext="<html><body>У меня нету заданий для чужаков! Пошел прочь!.</body></html>";
			return htmltext;
		}
		
		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 == ARUJIEN)
		{
			if(cond == 0)
			{
				if(st.getPlayer().getRace().ordinal() != 1 && st.getPlayer().getRace().ordinal() != 0)
					htmltext = "30223-00.htm";
				else if(st.getPlayer().getLevel() >= 2)
					htmltext = "30223-02.htm";
				else
				{
					htmltext = "30223-01.htm";
					st.exitQuest(true);
				}
			}
			else if(cond == 1 && st.getQuestItemsCount(ARUJIENS_LETTER1) > 0)
				htmltext = "30223-05.htm";
			else if(cond == 2 && st.getQuestItemsCount(ARUJIENS_LETTER2) > 0)
				htmltext = "30223-06.htm";
			else if(cond == 3 && st.getQuestItemsCount(ARUJIENS_LETTER3) > 0)
				htmltext = "30223-07.htm";
			else if(cond == 4 && st.getQuestItemsCount(POETRY_BOOK) > 0)
				htmltext = "30223-11.htm";
			else if(cond == 5 && st.getQuestItemsCount(GREENIS_LETTER) > 0)
			{
				htmltext = "30223-10.htm";
				st.takeItems(GREENIS_LETTER, 1);
				st.giveItems(BEGINNERS_POTION, 5);
				st.giveItems(ADENA,1850);
			    st.giveItems(MYSTIC_EARRING, 1);
			    st.addExpAndSp(4254, 335);
				st.playSound("ItemSound.quest_finish");
				st.set("cond","0");
				st.setState(COMPLETED);
				st.exitQuest(false);
			}
		}
		else if(npcId == MIRABEL)
		{
			if(cond == 1 && st.getQuestItemsCount(ARUJIENS_LETTER1) > 0)
			{
				htmltext = "30146-01.htm";
				st.takeItems(ARUJIENS_LETTER1, 1);
				st.giveItems(ARUJIENS_LETTER2, 1);
				st.set("cond", "2");
				st.playSound("ItemSound.quest_middle");
			}
			else if(cond == 2)
				htmltext = "30146-02.htm";
		}
		else if(npcId == HERBIEL)
		{
			if(cond == 2 && st.getQuestItemsCount(ARUJIENS_LETTER2) > 0)
			{
				htmltext = "30150-01.htm";
				st.takeItems(ARUJIENS_LETTER2, 1);
				st.giveItems(ARUJIENS_LETTER3, 1);
				st.set("cond", "3");
				st.playSound("ItemSound.quest_middle");
			}
			else if(cond == 3)
				htmltext = "30150-02.htm";
		}
		else if(npcId == GREENIS)
			if(cond == 4 && st.getQuestItemsCount(POETRY_BOOK) > 0)
			{
				htmltext = "30157-02.htm";
				st.takeItems(POETRY_BOOK, 1);
				st.giveItems(GREENIS_LETTER, 1);
				st.set("cond", "5");
				st.playSound("ItemSound.quest_middle");
			}
			else if(cond == 5 && st.getQuestItemsCount(GREENIS_LETTER) > 0)
				htmltext = "30157-03.htm";
			else
				htmltext = "30157-01.htm";
		return htmltext;
		}	
	
    public static void main(String[] args)
    {
    	_log.info("2_WhatWomenWant1:     load");
    	new q2_WhatWomenWant1(2, "2_WhatWomenWant1", "What Women Want");
    }
}