/*
 * This file is part of the L2J Mobius project.
 * 
 * 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 .
 */
package handlers.communityboard;
import com.l2jmobius.gameserver.data.sql.impl.ClanTable;
import com.l2jmobius.gameserver.handler.CommunityBoardHandler;
import com.l2jmobius.gameserver.handler.IWriteBoardHandler;
import com.l2jmobius.gameserver.model.L2Clan;
import com.l2jmobius.gameserver.model.actor.instance.L2PcInstance;
import com.l2jmobius.gameserver.network.SystemMessageId;
import com.l2jmobius.gameserver.util.Util;
import com.l2jmobius.util.StringUtil;
/**
 * Clan board.
 * @author Zoey76
 */
public class ClanBoard implements IWriteBoardHandler
{
	private static final String[] COMMANDS =
	{
		"_bbsclan",
		"_bbsclan_list",
		"_bbsclan_clanhome"
	};
	
	@Override
	public String[] getCommunityBoardCommands()
	{
		return COMMANDS;
	}
	
	@Override
	public boolean parseCommunityBoardCommand(String command, L2PcInstance activeChar)
	{
		if (command.equals("_bbsclan"))
		{
			CommunityBoardHandler.getInstance().addBypass(activeChar, "Clan", command);
			
			if ((activeChar.getClan() == null) || (activeChar.getClan().getLevel() < 2))
			{
				clanList(activeChar, 1);
			}
			else
			{
				clanHome(activeChar);
			}
		}
		else if (command.startsWith("_bbsclan_clanlist"))
		{
			CommunityBoardHandler.getInstance().addBypass(activeChar, "Clan List", command);
			
			if (command.equals("_bbsclan_clanlist"))
			{
				clanList(activeChar, 1);
			}
			else if (command.startsWith("_bbsclan_clanlist;"))
			{
				try
				{
					clanList(activeChar, Integer.parseInt(command.split(";")[1]));
				}
				catch (Exception e)
				{
					clanList(activeChar, 1);
					LOG.warning(ClanBoard.class.getSimpleName() + ": Player " + activeChar + " send invalid clan list bypass " + command + "!");
				}
			}
		}
		else if (command.startsWith("_bbsclan_clanhome"))
		{
			CommunityBoardHandler.getInstance().addBypass(activeChar, "Clan Home", command);
			
			if (command.equals("_bbsclan_clanhome"))
			{
				clanHome(activeChar);
			}
			else if (command.startsWith("_bbsclan_clanhome;"))
			{
				try
				{
					clanHome(activeChar, Integer.parseInt(command.split(";")[1]));
				}
				catch (Exception e)
				{
					clanHome(activeChar);
					LOG.warning(ClanBoard.class.getSimpleName() + ": Player " + activeChar + " send invalid clan home bypass " + command + "!");
				}
			}
		}
		else if (command.startsWith("_bbsclan_clannotice_edit;"))
		{
			CommunityBoardHandler.getInstance().addBypass(activeChar, "Clan Edit", command);
			
			clanNotice(activeChar, activeChar.getClanId());
		}
		else if (command.startsWith("_bbsclan_clannotice_enable"))
		{
			CommunityBoardHandler.getInstance().addBypass(activeChar, "Clan Notice Enable", command);
			
			if (activeChar.getClan() != null)
			{
				activeChar.getClan().setNoticeEnabled(true);
			}
			clanNotice(activeChar, activeChar.getClanId());
		}
		else if (command.startsWith("_bbsclan_clannotice_disable"))
		{
			CommunityBoardHandler.getInstance().addBypass(activeChar, "Clan Notice Disable", command);
			
			if (activeChar.getClan() != null)
			{
				activeChar.getClan().setNoticeEnabled(false);
			}
			clanNotice(activeChar, activeChar.getClanId());
		}
		else
		{
			CommunityBoardHandler.separateAndSend("
Command " + command + " need development.
", activeChar);
		}
		return true;
	}
	
	private void clanNotice(L2PcInstance activeChar, int clanId)
	{
		final L2Clan cl = ClanTable.getInstance().getClan(clanId);
		if (cl != null)
		{
			if (cl.getLevel() < 2)
			{
				activeChar.sendPacket(SystemMessageId.THERE_ARE_NO_COMMUNITIES_IN_MY_CLAN_CLAN_COMMUNITIES_ARE_ALLOWED_FOR_CLANS_WITH_SKILL_LEVELS_OF_2_AND_HIGHER);
				parseCommunityBoardCommand("_bbsclan_clanlist", activeChar);
			}
			else
			{
				final StringBuilder html = StringUtil.startAppend(2000, "
");
				if (activeChar.isClanLeader())
				{
					StringUtil.append(html, "
| The Clan Notice function allows the clan leader to send messages through a pop-up window to clan members at login. | 
|  | 
");
					
					if (activeChar.getClan().isNoticeEnabled())
					{
						StringUtil.append(html, "| Clan Notice Function:   on   /   off");
					}
					else
					{
						StringUtil.append(html, " | 
| Clan Notice Function:   on   /   off");
					}
					
					StringUtil.append(html, " | 

 
");
					
					Util.sendCBHtml(activeChar, html.toString(), activeChar.getClan().getNotice());
				}
				else
				{
					StringUtil.append(html, "
| You are not your clan's leader, and therefore cannot change the clan notice | 
");
					if (activeChar.getClan().isNoticeEnabled())
					{
						StringUtil.append(html, "| The current clan notice: | 
|  | " + activeChar.getClan().getNotice() + " |  | 
");
					}
					StringUtil.append(html, "