/*
 * Copyright (C) 2005-2008 L2J_JP / 2008-2013 L2J-SFJP
 * 
 * This file is part of L2J Server.
 * 
 * L2J Server 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.
 * 
 * L2J Server 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/>.
 */
//@formatter:off
package ai.fantasy_isle;

import static com.l2jserver.gameserver.util.GameTimeFunction.gameTimeHourToRealMilli;
import static com.l2jserver.gameserver.util.GameTimeFunction.timeLeftMilli;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.concurrent.ScheduledFuture;

import ai.npc.AbstractNpcAI;

import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.ai.CtrlEvent;
import com.l2jserver.gameserver.ai.CtrlIntention;
import com.l2jserver.gameserver.ai.NextAction;
import com.l2jserver.gameserver.model.Location;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.network.serverpackets.PlaySound;

/**
 * Fantasy Isle Parade
 * @author JOJO
 */
public class Parade extends AbstractNpcAI
{
	private static final boolean DEBUG = true;
	private static final boolean MUSIC = true;
	protected final int[] ACTORS = {
		// 3列縦隊
		32381,/*子供竹馬*/              32379,/*子供旗持ち*/            32381,/*子供竹馬*/
		32382,/*子供かご*/              32383,/*子供お手玉*/            32384,/*子供皿回し*/
		32381,/*子供竹馬*/              32385,/*子供玉乗り*/            32381,/*子供竹馬*/
		32384,/*子供皿回し*/            32383,/*子供お手玉*/            32382,/*子供かご*/
		32386,/*ＤＥ女*/                32387,/*ＤＥ女*/                32388,/*ＤＥ女*/
		32389,/*ＤＥ女*/                32390,/*ＤＥ女*/                32391,/*ドワ男ガード*/
		32392,/*ドワ男職人*/            32393,/*ドワ女*/                32394,/*ドワ女*/
		32395,/*ドワ女ＧＫ*/            32396,/*エルフ男ガード*/        32397,/*エルフ男マスター*/
		32398,/*エルフ女黄色*/          32399,/*エルフ女ドレス*/        32400,/*エルフ女*/
		32401,/*オーク男ガード*/        32402,/*オーク男ハゲハンマー*/  32403,/*オーク男*/
		32404,/*オーク女ＧＫ*/          32405,/*ヒューマン男マスター*/  32406,/*ヒューマン女マスター*/
		32407,/*ヒューマン女ファイター*/32408,/*ヒューマン女ＧＫ*/      32409,/*猫女*/
		32411,/*クーガ*/                32412,/*クッカプロ*/            32413,/*イエティ(大)*/
		32414,/*温泉イエティ(小)*/      32415,/*温泉イエティ(小)*/      32416,/*ユニコーン金*/
		32417,/*ユニコーン青透明*/      32418,/*ユニコーン茶*/          32419,/*ユニコーン白*/
		32420,/*ユニコーン紫透明*/      32421,/*猫男*/                  32422,/*猫男*/
		32423,/*猫女*/           32429,/*ブレカ ウォーロック パストゥ*/ 32430,/*ドワ女ＧＫ*/
		32447,/*メデューサ*/            32448,/*女*/                    32449,/*女*/
		32450,/*女*/                    32451,/*ガラガラへび女(巨大)*/  32452,/*こうもり女(空中)*/
		32453,/*女(空中)*/              32454,/*女(空中)*/              32455,/*女*/
		32456,/*女(空中)*/              0,                              0,
		0,                              32415,/*温泉イエティ(小)*/      0,
	};
	
	//ルート１ (270度 北行き)
	private final Location[] START1 = {new Location(-54780, -56810, -2015, 49152), new Location(-54860, -56810, -2015, 49152), new Location(-54940, -56810, -2015, 49152)};
	private final Location[] GOAL1  = {new Location(-54780, -57965, -2015, 49152), new Location(-54860, -57965, -2015, 49152), new Location(-54940, -57965, -2015, 49152)};
	//ルート２ (180度 西行き)
//	private final Location[] START2 = {new Location(-55740, -58900, -2015, 32768), new Location(-55740, -58820, -2015, 32768), new Location(-55740, -58740, -2015, 32768)};//TODO
	private final Location[] START2 = {new Location(-55715, -58900, -2015, 32768), new Location(-55715, -58820, -2015, 32768), new Location(-55715, -58740, -2015, 32768)};
	private final Location[] GOAL2  = {new Location(-60850, -58900, -2015, 32768), new Location(-60850, -58820, -2015, 32768), new Location(-60850, -58740, -2015, 32768)};
	//ルート３ (90度 南行き)
	private final Location[] START3 = {new Location(-61790, -57965, -2015, 16384), new Location(-61710, -57965, -2015, 16384), new Location(-61630, -57965, -2015, 16384)};
	private final Location[] GOAL3  = {new Location(-61790, -53890, -2116, 16384), new Location(-61710, -53890, -2116, 16384), new Location(-61630, -53890, -2116, 16384)};
	//ルート４ (0度 東行き)
	private final Location[] START4 = {new Location(-60840, -52990, -2108, 0), new Location(-60840, -53070, -2108, 0), new Location(-60840, -53150, -2108, 0)};
	private final Location[] GOAL4  = {new Location(-58620, -52990, -2015, 0), new Location(-58620, -53070, -2015, 0), new Location(-58620, -53150, -2015, 0)};
	//ルート５ (315度 北東行き)
	private final Location[] START5 = {new Location(-57233, -53554, -2015, 57344), new Location(-57290, -53610, -2015, 57344), new Location(-57346, -53667, -2015, 57344)};
	private final Location[] GOAL5  = {new Location(-55338, -55435, -2015, 57344), new Location(-55395, -55491, -2015, 57344), new Location(-55451, -55547, -2015, 57344)};
	
	protected final Location[][] START = { START1, START2, START3, START4, START5 };
	protected final Location[][] GOAL  = { GOAL1,  GOAL2,  GOAL3,  GOAL4,  GOAL5 };
	
	protected final ArrayList<CtrlEvent> ARRIVED_EVENTS;
	
	protected ScheduledFuture<?> spawnTask;
	
	protected int npcIndex;
	
	private Parade(int id, String name, String descr)
	{
		super(id, name, descr);
		
		ARRIVED_EVENTS = new ArrayList<>(1);
		ARRIVED_EVENTS.add(CtrlEvent.EVT_ARRIVED);
		
		final long diff = timeLeftMilli(8, 0, 0, 6), cycle = gameTimeHourToRealMilli(6); // リネ時間で8時00分開始、6時間ごと繰り返し
		if (DEBUG)
		{
			SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
			_log.info("Fantasy Isle: Parade script starting at " + format.format(System.currentTimeMillis() + diff) + " and is scheduled each next " + (cycle / 3600000) + " hours.");
		}
		ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Start(), diff, cycle);
	}
	
	protected class Start implements Runnable
	{
		@Override
		public void run()
		{
			if (DEBUG)
				System.out.println("Fantasy Isle: Parade start.");
			npcIndex = 0;
			spawnTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Spawn(), 0, 5000);
		}
	}
	
	protected class Spawn implements Runnable
	{
		@Override
		public void run()
		{
			for (int i = 0; i < 3; ++i) {
				if (npcIndex >= ACTORS.length) { spawnTask.cancel(false); return; }
				int npcId = ACTORS[npcIndex++];
				if (npcId == 0) continue;
				for (int route = START.length; --route >= 0;) {
					Location start = START[route][i];
					Location goal = GOAL[route][i];
					final L2Npc actor = addSpawn(npcId, start.getX(), start.getY(), start.getZ(), start.getHeading(), false, 0);
					actor.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, goal);
					actor.getAI().setNextAction(new NextAction(ARRIVED_EVENTS, null){
						@Override public void doWork()
						{
							actor.deleteMe();
						}
					});
					if (MUSIC)
						if (npcIndex == 1) actor.broadcastPacket(new PlaySound("TP05_F"));
				}
			}
		}
	}
	
	public static void main(String[] args)
	{
		new Parade(-1, "Parade", "fantasy_isle");
	}
}

/**
  メモ：
    ゲーム時間での"24時間"は、現実で４時間。つまり６倍速。
    サーバ起動時は "6時"に設定される。
    ステージがはじまる"20時30分"は、サーバ起動してから"14時間30分後"なので、
    現実に換算すると(６で割ると)２時間２５分後。

    パレードはゲーム時間で
      2:00、8:00、14:00、20:00 の１日４回、６時間ごとにおこなわれる。
    ステージは
      20:30 の１回のみ。
*/
