/*
 * 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 java.util.logging.Logger;
import com.l2jcore.Game.model.base.Race;
public class q1_LettersOfLove1 extends Quest
{	
	private static final Logger _log = Logger.getLogger(q1_LettersOfLove1.class.getName());
	private final static int DARIN = 30048;
	private final static int ROXXY = 30006;
	private final static int BAULRO = 30033;

	private final static int DARINGS_LETTER = 687;
	private final static int RAPUNZELS_KERCHIEF = 688;
	private final static int DARINGS_RECEIPT = 1079;
	private final static int BAULROS_POTION = 1080;
	private final static int NECKLACE = 906;
	
	private State CREATED;
	private State STARTED;
	private State COMPLETED;
	
	public q1_LettersOfLove1(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(DARIN);
		addTalkId(DARIN);
		addTalkId(ROXXY);
		addTalkId(BAULRO);

		STARTED.addQuestDrop(DARIN,DARINGS_LETTER,1);
		STARTED.addQuestDrop(DARIN,RAPUNZELS_KERCHIEF,1);
		STARTED.addQuestDrop(DARIN,DARINGS_RECEIPT,1);
		STARTED.addQuestDrop(DARIN,BAULROS_POTION,1);
	}

	public String onEvent(String event, QuestState qs)
	{
		if(event.equalsIgnoreCase("30048-06.htm"))
		{
			qs.set("cond", "1");
			qs.setState(STARTED);
			qs.giveItems(DARINGS_LETTER, 1);
			qs.playSound("ItemSound.quest_accept");
		}
		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 (talker.getRace()!=Race.Human)
		{
			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");
		switch(npcId)
		{
			case DARIN:
				if(cond == 0)
				{
					if(st.getPlayer().getLevel() >= 2)
						htmltext = "30048-02.htm";
					else
					{
					htmltext = "30048-01.htm";
					st.exitQuest(true);
					}
				}
				else if(cond == 1)
					htmltext = "30048-07.htm";
				else if(cond == 2 && st.getQuestItemsCount(RAPUNZELS_KERCHIEF) == 1)
				{
					htmltext = "30048-08.htm";
					st.takeItems(RAPUNZELS_KERCHIEF, 1);
					st.giveItems(DARINGS_RECEIPT, 1);
					st.set("cond", "3");
					st.playSound("ItemSound.quest_middle");
				}
				else if(cond == 3)
					htmltext = "30048-09.htm";
				else if(cond == 4 && st.getQuestItemsCount(BAULROS_POTION) == 1)
				{
					htmltext = "30048-10.htm";
					st.takeItems(BAULROS_POTION, 1);
					st.giveItems(NECKLACE, 1);
					st.giveItems(57, 2466); // T2
					st.addExpAndSp(5672, 446);
					st.playSound("ItemSound.quest_finish");
					st.unset("cond");
					st.setState(COMPLETED);
					st.exitQuest(false);
				}
				break;
			case ROXXY:
				if(cond == 1 && st.getQuestItemsCount(RAPUNZELS_KERCHIEF) == 0 && st.getQuestItemsCount(DARINGS_LETTER) > 0)
				{
					htmltext = "30006-01.htm";
					st.takeItems(DARINGS_LETTER, 1);
					st.giveItems(RAPUNZELS_KERCHIEF, 1);
					st.set("cond", "2");
					st.playSound("ItemSound.quest_middle");
				}
				else if(cond == 2 && st.getQuestItemsCount(RAPUNZELS_KERCHIEF) > 0)
					htmltext = "30006-02.htm";
					else if(cond > 2 && (st.getQuestItemsCount(BAULROS_POTION) > 0 || st.getQuestItemsCount(DARINGS_RECEIPT) > 0))
					htmltext = "30006-03.htm";
				break;
			case BAULRO:
				if(cond == 3 && st.getQuestItemsCount(DARINGS_RECEIPT) == 1)
				{
					htmltext = "30033-01.htm";
					st.takeItems(DARINGS_RECEIPT, 1);
					st.giveItems(BAULROS_POTION, 1);
					st.set("cond", "4");
					st.playSound("ItemSound.quest_middle");
				}
				else if(cond == 4)
					htmltext = "30033-02.htm";
				break;
		}
		return htmltext;
	}
	
    public static void main(String[] args)
    {
    	_log.info("1_LettersOfLove1:     load");
    	new q1_LettersOfLove1(1, "1_LettersOfLove1", "Letters Of Love");    	
    }
}