/*
 * 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 java.util.HashMap;

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 q999_C3Tutorial extends Quest
{
	private static int RECOMMENDATION_01 = 1067;
	private static int RECOMMENDATION_02 = 1068;
	private static int LEAF_OF_MOTHERTREE = 1069;
	private static int BLOOD_OF_JUNDIN = 1070;
	private static int LICENSE_OF_MINER = 1498;
	private static int VOUCHER_OF_FLAME = 1496;
	private static int SOULSHOT_NOVICE = 5789;
	private static int SPIRITSHOT_NOVICE = 5790;
	private static int BLUE_GEM = 6353;
	private static int SCROLL = 8594;
	private static int TOKEN = 8542;
	private final int[] startNpc = new int[]
	{
		30008,30009,30017,30019,30129,30131,30573,30575,30370,30528,30530,30400,30401,30402,30403,30404,
		30600, 30601, 30602, 30598, 30599,
		18342,20001
	};

	private State CREATED;
	private State STARTING;
	private State STARTED;
	private State COMPLETED;

	private static class Event
	{
		public String htm;
		public int radarX;
		public int radarY;
		public int radarZ;
		public int item;
		public int classId1;
		public int gift1;
		public int count1;
		public int classId2;
		public int gift2;
		public int count2;

		public Event(String htm, int radarX, int radarY, int radarZ, int item, int classId1, int gift1, int count1, int classId2, int gift2, int count2)
		{
			this.htm = htm;
			this.radarX = radarX;
			this.radarY = radarY;
			this.radarZ = radarZ;
			this.item = item;
			this.classId1 = classId1;
			this.gift1 = gift1;
			this.count1 = count1;
			this.classId2 = classId2;
			this.gift2 = gift2;
			this.count2 = count2;
		}
	}

	private static class Talk
	{
		public int raceId;
		public String[] htmlfiles;
		public int npcTyp;
		public int item;

		public Talk(int raceId, String[] htmlfiles, int npcTyp, int item)
		{
			this.raceId = raceId;
			this.htmlfiles = htmlfiles;
			this.npcTyp = npcTyp;
			this.item = item;
		}
	}

	private static final HashMap<String, Event> events = new HashMap<String, Event>();
	private static final HashMap<Integer, Talk> talks = new HashMap<Integer, Talk>();

	public q999_C3Tutorial(int questId, String name, String descr)
	{
		super(questId, name, descr);
		CREATED = new State("Start", this);
		STARTING = new State("Starting", this);
		STARTED = new State("Started", this);
		COMPLETED = new State("Completed", this);
		this.setInitialState(CREATED);
		
		events.put("30008_02", new Event("30008-03.htm", -84058, 243239, -3730, RECOMMENDATION_01, 0x00, SOULSHOT_NOVICE, 200, 0x00, 0, 0));
		events.put("30017_02", new Event("30017-03.htm", -84058, 243239, -3730, RECOMMENDATION_02, 0x0a, SPIRITSHOT_NOVICE, 100, 0x00, 0, 0));
		events.put("30370_02", new Event("30370-03.htm", 45491, 48359, -3086, LEAF_OF_MOTHERTREE, 0x19, SPIRITSHOT_NOVICE, 100, 0x12,SOULSHOT_NOVICE,200));
		events.put("30129_02", new Event("30129-03.htm", 12116, 16666, -4610, BLOOD_OF_JUNDIN, 0x26, SPIRITSHOT_NOVICE, 100, 0x1f,SOULSHOT_NOVICE,200));
		events.put("30528_02", new Event("30528-03.htm", 115642, -178046, -941, LICENSE_OF_MINER, 0x35, SOULSHOT_NOVICE , 200, 0x00, 0, 0));
		events.put("30573_02", new Event("30573-03.htm", -45067, -113549, -235, VOUCHER_OF_FLAME, 0x31, SPIRITSHOT_NOVICE, 100, 0x2c,SOULSHOT_NOVICE,200));
	
		talks.put(30017, new Talk(0, new String[] { "30017-01.htm","30017-02.htm","30017-04.htm" }, 0,0));
		talks.put(30008, new Talk(0, new String[] { "30008-01.htm","30008-02.htm","30008-04.htm" }, 0,0));
		talks.put(30370, new Talk(1, new String[] { "30370-01.htm","30370-02.htm","30370-04.htm" }, 0,0));
		talks.put(30129, new Talk(2, new String[] { "30129-01.htm","30129-02.htm","30129-04.htm" }, 0,0));
		talks.put(30573, new Talk(3, new String[] { "30573-01.htm","30573-02.htm","30573-04.htm" }, 0,0));
		talks.put(30528, new Talk(4, new String[] { "30528-01.htm","30528-02.htm","30528-04.htm" }, 0,0));
		talks.put(30018, new Talk(0, new String[] { "30131-01.htm","","30019-03a.htm","30019-04.htm" }, 1,RECOMMENDATION_02));
		talks.put(30019, new Talk(0, new String[] { "30131-01.htm","","30019-03a.htm","30019-04.htm" }, 1,RECOMMENDATION_02));
		talks.put(30020, new Talk(0, new String[] { "30131-01.htm","","30019-03a.htm","30019-04.htm" }, 1,RECOMMENDATION_02));
		talks.put(30021, new Talk(0, new String[] { "30131-01.htm","","30019-03a.htm","30019-04.htm" }, 1,RECOMMENDATION_02));
		talks.put(30009, new Talk(0, new String[] { "30530-01.htm","30009-03.htm","","30009-04.htm" }, 1,RECOMMENDATION_01));
		talks.put(30011, new Talk(0, new String[] { "30530-01.htm","30009-03.htm","","30009-04.htm" }, 1,RECOMMENDATION_01));
		talks.put(30012, new Talk(0, new String[] { "30530-01.htm","30009-03.htm","","30009-04.htm" }, 1,RECOMMENDATION_01));
		talks.put(30056, new Talk(0, new String[] { "30530-01.htm","30009-03.htm","","30009-04.htm" }, 1,RECOMMENDATION_01));
		talks.put(30400, new Talk(1, new String[] { "30131-01.htm","30400-03.htm","30400-03a.htm","30400-04.htm" }, 1,LEAF_OF_MOTHERTREE));
		talks.put(30401, new Talk(1, new String[] { "30131-01.htm","30400-03.htm","30400-03a.htm","30400-04.htm" }, 1,LEAF_OF_MOTHERTREE));
		talks.put(30402, new Talk(1, new String[] { "30131-01.htm","30400-03.htm","30400-03a.htm","30400-04.htm" }, 1,LEAF_OF_MOTHERTREE));
		talks.put(30403, new Talk(1, new String[] { "30131-01.htm","30400-03.htm","30400-03a.htm","30400-04.htm" }, 1,LEAF_OF_MOTHERTREE));
		talks.put(30131, new Talk(2, new String[] { "30131-01.htm","30131-03.htm","30131-03a.htm","30131-04.htm" }, 1,BLOOD_OF_JUNDIN));
		talks.put(30404, new Talk(2, new String[] { "30131-01.htm","30131-03.htm","30131-03a.htm","30131-04.htm" }, 1,BLOOD_OF_JUNDIN));
		talks.put(30574, new Talk(3, new String[] { "30575-01.htm","30575-03.htm","30575-03a.htm","30575-04.htm" }, 1,VOUCHER_OF_FLAME));
		talks.put(30575, new Talk(3, new String[] { "30575-01.htm","30575-03.htm","30575-03a.htm","30575-04.htm" }, 1,VOUCHER_OF_FLAME));
		talks.put(30530, new Talk(4, new String[] { "30530-01.htm","30530-03.htm","","30530-04.htm" }, 1,LICENSE_OF_MINER));

		for(int i = 0; i <= 22; i++)
		{
			if(i >= 0 && i <= 15)
			{
				addStartNpc(startNpc[i]);
				addTalkId(startNpc[i]);
			}
			else if(i >= 16 && i <= 20)
				addFirstTalkId(startNpc[i]);
			else if(i == 21 || i == 22)
				addKillId(startNpc[i]);
		}
	}

	public String onAdvEvent(String event, L2NpcInstance npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState(getName());
		String htmltext = event;
		int classId = player.getClassId().getId();
		Event e = events.get(event);
		if (e.radarX != 0)
			st.addRadar(e.radarX, e.radarY, e.radarZ);
		htmltext = e.htm;
		if (st.getQuestItemsCount(e.item) != 0 && st.getInt("onlyone") == 0)
		{
			st.addExpAndSp(0, 50);
			st.takeItems(e.item, 1);
			if (classId == e.classId1)
				st.giveItems(e.gift1, e.count1);
			else if (classId == e.classId2)
			{
				if (e.gift2 != 0)
					st.giveItems(e.gift2, e.count2);	
			}
			st.unset("cond");
			st.set("onlyone","1");
			st.setState(COMPLETED);
			st.playSound("ItemSound.quest_finish");
		}
		return htmltext;
	}
	
	public String onFirstTalk(L2NpcInstance npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState(getName());
		if(st != null)
		{
			State id = st.getState();
			int onlyone = st.getInt("onlyone");
			if(id == COMPLETED && onlyone == 1)
			{
				st.set("onlyone","2");
				if(player.getClassId().isMage())
					st.giveItems(SPIRITSHOT_NOVICE,100);
				else
					st.giveItems(SOULSHOT_NOVICE,200);
				st.giveItems(TOKEN,12);
				if(st.getRandom(2) != 0)
					st.giveItems(SCROLL,2);
			}
		}
		return null;
	}
	
	public String onTalk(L2NpcInstance npc, L2PcInstance player)
	{	
		QuestState st = player.getQuestState(getName());
		if(st == null) st = newQuestState(player);
		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 onlyone = st.getInt("onlyone");
		int level = player.getLevel();
		int cond = st.getInt("cond");
		boolean isMage = player.getClassId().isMage();
		State id = st.getState();
		Talk t = talks.get(npcId);
		if(t == null) return null;
		if(id == CREATED)
		{
			st.setState(STARTING);
			st.set("onlyone","0");
		}
		if((level >= 10 || onlyone != 0) && t.npcTyp == 1)
			htmltext = "30575-05.htm";
		else if(level < 10 && onlyone == 0)
		{
			if(player.getRace().ordinal() == t.raceId)
			{
				htmltext = t.htmlfiles[0];
				if(t.npcTyp == 1)
				{
					if(cond == 0)
					{
						if(isMage)
						{
							st.set("cond","1");
							st.setState(STARTED);
							st.playSound("ItemSound.quest_tutorial");
						}
						else
						{
							htmltext="30530-01.htm";
							st.set("cond","1");
							st.setState(STARTED);
							st.playSound("ItemSound.quest_tutorial");
						}
					}
					else if(cond == 1 && st.getQuestItemsCount(t.item) == 0)
					{
						if(st.getQuestItemsCount(BLUE_GEM) > 0)
						{
							st.takeItems(BLUE_GEM, st.getQuestItemsCount(BLUE_GEM));
							st.giveItems(t.item, 1);
							st.set("cond","2");
							st.playSound("ItemSound.quest_middle");
							if(isMage)
							{
								st.playTutorialVoice("tutorial_voice_027");
								st.giveItems(SPIRITSHOT_NOVICE, 100);
								htmltext = t.htmlfiles[2];
								if(htmltext.isEmpty())
									htmltext = "<html><body>I am sorry. I only help warriors. Please go to another Newbie Helper who may assist you.</body></html>";
							}
							else
							{
								st.playTutorialVoice("tutorial_voice_026");
								st.giveItems(SOULSHOT_NOVICE, 200);
								htmltext = t.htmlfiles[1];
								if(htmltext.isEmpty())
									htmltext = "<html><body>I am sorry. I only help mystics. Please go to another Newbie Helper who may assist you.</body></html>";
							}
						}
						else if(isMage)
						{
							htmltext = "30131-02.htm";
							if(player.getRace().ordinal() == 3)
								htmltext = "30575-02.htm";
						}
						else
							htmltext = "30530-02.htm";
					}
					else if(cond == 2)
						htmltext = t.htmlfiles[3];
				}
				else if(t.npcTyp == 0)
				{
					if(cond == 1)
						htmltext = t.htmlfiles[0];
					else if(cond == 2)
						htmltext = t.htmlfiles[1];
					else if(cond == 3)
						htmltext = t.htmlfiles[2];
				}
			}
		}		
		else
			htmltext = "<html><body>You are too experienced now.</body></html>";
		
		return htmltext;
	}

	public String onKill(L2NpcInstance npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = player.getQuestState(getName());
		if(st == null) return super.onKill(npc, player, isPet);

		if(st.getInt("cond") == 1 && st.getRandom(100) < 25 && st.getQuestItemsCount(BLUE_GEM) == 0)
		{
			st.giveItems(BLUE_GEM,1);
			st.playSound("ItemSound.quest_itemget");
			st.playSound("ItemSound.quest_tutorial");
		}
		return super.onKill(npc, player, isPet);
	}
	
	public static void main(String[] args)
	{
		_log.info("999_C3Tutorial:     load");
		new q999_C3Tutorial(999, "999_C3Tutorial", "Interlude Tutorial");    	
	}
}