/*
 * 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 q32_AnObviousLie extends Quest
{
	private final static int[] NPC = {30120, 30094, 31706, 20135};
	private final static int[] ITEM = {7165, 7166, 3031, 1868, 1866};
	private final static int[] REWARD = {7680, 6843, 7683};

	private State CREATED;
	private State STARTED;
	private State COMPLETED;
	
	public q32_AnObviousLie(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(NPC[0]);
		for(int NPC_ID : NPC)
			addTalkId(NPC_ID);
		addKillId(NPC[3]);
	}

	public String onEvent(String event, QuestState st)
	{
		String htmltext = event;
		int cond = st.getInt("cond");
		if(event.equalsIgnoreCase("30120-1.htm"))
		{
			st.set("cond", "1");
			st.setState(STARTED);
			st.playSound("ItemSound.quest_accept");
		}
		else if(event.equalsIgnoreCase("30094-1.htm"))
		{
			st.giveItems(ITEM[0],1);
			st.set("cond","2");
		}
		else if(event.equalsIgnoreCase("31706-1.htm"))
		{
			st.takeItems(ITEM[0],1);
			st.set("cond","3");
		}
		else if(event.equalsIgnoreCase("30094-4.htm"))
		{
			if(st.getQuestItemsCount(ITEM[1]) > 19)
			{
				st.takeItems(ITEM[1],20);
				st.set("cond","5");
			}
			else
			{
				htmltext = "You don't have enough materials";
				st.set("cond","3");
			}
		}
		else if(event.equalsIgnoreCase("30094-7.htm"))
		{
			if(st.getQuestItemsCount(ITEM[2]) >= 500)
			{
				st.takeItems(ITEM[2],500);
				st.set("cond","6");
			}
			else
				htmltext="Youn don't have enough materials";
		}
		else if(event.equalsIgnoreCase("31706-4.htm"))
			st.set("cond","7");
		else if(event.equalsIgnoreCase("30094-10.htm"))
			st.set("cond","8");
		else if(event.equalsIgnoreCase("30094-13.htm"))
		{
			if(st.getQuestItemsCount(ITEM[3]) >= 1000 && st.getQuestItemsCount(ITEM[4]) >= 500)
			{
				st.takeItems(ITEM[3],1000);
				st.takeItems(ITEM[4],500);
			}
			else
				htmltext="Youn don't have enough materials";
		}
		else if(event.equalsIgnoreCase("cat") || event.equalsIgnoreCase("racoon") || event.equalsIgnoreCase("rabbit"))
		{
			if(cond == 8)
			{
				int item = 0;
				if(event.equalsIgnoreCase("cat"))
					item = REWARD[1];
				if(event.equalsIgnoreCase("racoon"))
					item = REWARD[0];
				if(event.equalsIgnoreCase("rabbit"))
					item = REWARD[2];
				if(item != 0)
					st.giveItems(item,1);
				st.setState(COMPLETED);
				st.unset("cond");
				st.playSound("ItemSound.quest_finish");
				htmltext = "30094-14.htm";
			}
			else
				htmltext="???";
		}
		return htmltext;
	}

	public String onTalk(L2NpcInstance npc, L2PcInstance talker)
	{
		QuestState st = talker.getQuestState(getName());
		String htmltext = "<html><body>You are either not carrying out your quest or don't meet the criteria.</body></html>";
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
		if(st == null) return htmltext;
		State id = st.getState();
		if(npcId == NPC[0])
		{
			if(cond == 0)
			{
				if(talker.getLevel() >= 45)
					htmltext = "30120-0.htm";
				else if(id == COMPLETED)
					htmltext = "<html><body>This quest has already been completed.</body></html>";
				else
				{
					htmltext = "30120-0a.htm";
					st.exitQuest(true);
				}
			}
			else if(cond == 1)
				htmltext = "30120-2.htm";
		}
		else if(id == STARTED)
		{
			if(npcId == NPC[1])
			{
				if(cond == 1)
					htmltext = "30094-0.htm";
				else if(cond == 2)
					htmltext = "30094-2.htm";
				else if(cond == 4)
					htmltext = "30094-3.htm";
				else if(cond == 5 && st.getQuestItemsCount(ITEM[2]) < 500)
					htmltext = "30094-5.htm";
				else if(cond == 5 && st.getQuestItemsCount(ITEM[2]) >= 500)
					htmltext = "30094-6.htm";
				else if(cond == 6)
					htmltext = "30094-8.htm";
				else if(cond == 7)
					htmltext = "30094-9.htm";
				else if(cond == 8 && (st.getQuestItemsCount(ITEM[3]) < 1000 || st.getQuestItemsCount(ITEM[4]) < 500))
					htmltext = "30094-11.htm";
				else if(cond == 8 && st.getQuestItemsCount(ITEM[3]) >= 1000 && st.getQuestItemsCount(ITEM[4]) >= 500)
					htmltext = "30094-12.htm";
			}
			if(npcId == NPC[2])
			{
				if(cond == 2)
					htmltext = "31706-0.htm";
				else if(cond == 3)
					htmltext = "31706-2.htm";
				else if(cond == 6)
					htmltext = "31706-3.htm";
				else if(cond == 7)
					htmltext = "31706-5.htm";
			}
		}
		return htmltext;
	}
	
	@Override
	public String onAttack (L2NpcInstance npc, L2PcInstance attacker, int damage, boolean isPet)
	{
		QuestState st = attacker.getQuestState(getName());
        if (st == null || st.getState() != STARTED)
        {
			return super.onAttack(npc, attacker, damage, isPet);
        }
		if(st.getRandom(100) < 30 && st.getInt("cond") == 3)
		{
			if(st.getQuestItemsCount(ITEM[1]) < 20)
			{
				st.giveItems(ITEM[1],1);
				if(st.getQuestItemsCount(ITEM[1]) == 19)
				{
					st.playSound("ItemSound.quest_middle");
					st.set("cond","4");
				}
				else
					st.playSound("ItemSound.quest_itemget");
			}
		}
        return super.onAttack(npc, attacker, damage, isPet);
    }

	public static void main(String[] args)
	{
		_log.info("32_AnObviousLie:     load");
		new q32_AnObviousLie(32, "32_AnObviousLie", "An Obvious Lie");    	
	}
}