/*
 * 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 custom.BossRespawn;

import javolution.text.TextBuilder;

import com.l2jcore.Game.datatables.sql.NpcTable;
import com.l2jcore.Game.managers.GrandBossManager;
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.State;
import com.l2jcore.Game.network.serverpackets.NpcHtmlMessage;

public class BossRespawn extends Quest
{
	private static final int NPC_ID = 93000;
	private static final int[] BOSSES = {29001, 29006, 29014, 29019, 29020, /*29022, 29028,*/ 29045};

	private State CREATED;

	public BossRespawn(int questid, String name, String descr)
	{
		super(questid, name, descr);
		CREATED = new State("Start", this);
		this.setInitialState(CREATED);
		
		addStartNpc(NPC_ID);
		addFirstTalkId(NPC_ID);
	}
	
	private void sendInfo(L2NpcInstance npc, L2PcInstance player)
	{
		TextBuilder tb = new TextBuilder("<html><title>Grand Boss Info By Dleogr</title><body><br><center>");
		tb.append("<img src=\"L2UI_CH3.herotower_deco\" width=256 height=32><br><br>");
		for(int boss : BOSSES)
		{
			String name = NpcTable.getInstance().getTemplate(boss).getName();
			long delay = GrandBossManager.getInstance().getStatsSet(boss).getLong("respawn_time");
			if (delay <= System.currentTimeMillis())
			{
				tb.append("<font color=\"00C3FF\">" + name + "</color>: " + "<font color=\"9CC300\">Is Alive</color>"+"<br1>");
			}
			else
			{
				int hours = (int) ((delay - System.currentTimeMillis()) / 1000 / 60 / 60);
				int mins = (int) (((delay - (hours * 60 * 60 * 1000)) - System.currentTimeMillis()) / 1000 / 60);
				int seconts = (int) (((delay - ((hours * 60 * 60 * 1000) + (mins * 60 * 1000))) - System.currentTimeMillis()) / 1000);
				tb.append("<font color=\"00C3FF\">" + name + "</color>" + "<font color=\"FFFFFF\">" +" " + "Respawn in :</color>" + " " + " <font color=\"32C332\">" + hours + " : " + mins + " : " + seconts + "</color><br1>");
			}

			tb.append("<br><img src=\"L2UI_CH3.herotower_deco\" width=256 height=32><br>");
			tb.append("</center></body></html>");
		
			NpcHtmlMessage msg = new NpcHtmlMessage(npc.getObjectId());
			msg.setHtml(tb.toString());
			player.sendPacket(msg);
		}
	}
	
	public String onFirstTalk(L2NpcInstance npc, L2PcInstance player)
	{
		if(npc == null || player == null)
			return null;
		if(npc.getNpcId() == NPC_ID)
		{
			sendInfo(npc, player);
		}
		return null;
	}

	public static void main(String[] args)
	{
		new BossRespawn(-1, "BossRespawn", "custom");
	}
}