/*
 * 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 2, 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.festina.gameserver.handler.itemhandlers;

import java.util.logging.Logger;

import com.festina.gameserver.NpcTable;
import com.festina.gameserver.ThreadPoolManager;
import com.festina.gameserver.handler.IItemHandler;
import com.festina.gameserver.model.L2ItemInstance;
import com.festina.gameserver.model.L2PetDataTable;
import com.festina.gameserver.model.L2World;
import com.festina.gameserver.model.actor.instance.L2PcInstance;
import com.festina.gameserver.model.actor.instance.L2PetInstance;
import com.festina.gameserver.model.actor.instance.L2PlayableInstance;
import com.festina.gameserver.serverpackets.MagicSkillLaunched;
import com.festina.gameserver.serverpackets.MagicSkillUser;
import com.festina.gameserver.serverpackets.PetInfo;
import com.festina.gameserver.serverpackets.Ride;
import com.festina.gameserver.serverpackets.SystemMessage;
import com.festina.gameserver.templates.L2NpcTemplate;
/**
 * This class ...
 * 
 * @version $Revision: 1.9.2.9.2.10 $ $Date: 2005/03/31 09:19:39 $
 */

public class PetSummon implements IItemHandler
{
    protected static Logger _log = Logger.getLogger(PetSummon.class.getName());
	
	// all the items ids that this handler knowns
	private static final int[] _itemIds = { 2375, 3500, 3501, 3502, 4422, 4423, 4424, 4425, 6648, 6649, 6650 };

	/* (non-Javadoc)
	 * @see com.festina.gameserver.handler.IItemHandler#useItem(com.festina.gameserver.model.L2PcInstance, com.festina.gameserver.model.L2ItemInstance)
	 */
	public void useItem(L2PlayableInstance playable, L2ItemInstance item)
	{
		if (!(playable instanceof L2PcInstance))
			return;
		L2PcInstance activeChar = (L2PcInstance)playable;
		int npcId;
        
        if (activeChar.isInOlympiadMode())
        {
            activeChar.sendPacket(new SystemMessage(SystemMessage.THIS_ITEM_IS_NOT_AVAILABLE_FOR_THE_OLYMPIAD_EVENT));
            return;
        }
		
		if (activeChar.getPet() != null)
		{
            activeChar.sendPacket(new SystemMessage(SystemMessage.YOU_ALREADY_HAVE_A_PET));
			return;
		}
		
		if ( activeChar.isAttackingNow() )
		{
            activeChar.sendPacket(new SystemMessage(SystemMessage.YOU_CANNOT_SUMMON_IN_COMBAT));
			return;
		}
        
        if (activeChar.isMounted())
        {
            activeChar.sendPacket(new SystemMessage(SystemMessage.YOU_ALREADY_HAVE_A_PET));
			return;
        }
		
        npcId = L2PetDataTable.getPetIdByItemId(item.getItemId());
        
        if (npcId == 0)
        	return;
        
        if (L2PetDataTable.isWyvern(npcId))
        {
            Ride mount = new Ride(activeChar.getObjectId(), Ride.ACTION_MOUNT, 12621);
            activeChar.sendPacket(mount);
            activeChar.broadcastPacket(mount);
            activeChar.setMountType(mount.getMountType());
            activeChar.setMountObjectID(item.getObjectId());
			return;
        }
		
		
		L2NpcTemplate petTemplate = NpcTable.getInstance().getTemplate(npcId);
		

		L2PetInstance newpet = L2PetInstance.spawnPet(petTemplate, activeChar, item);
		if (newpet == null) return;
		newpet.setTitle(activeChar.getName());
		
		if (!newpet.isRespawned()) {
			newpet.setCurrentHp(newpet.getMaxHp());
			newpet.setCurrentMp(newpet.getMaxMp());
			newpet.getStat().setExp(newpet.getExpForThisLevel());
			newpet.setCurrentFed(newpet.getMaxFed());
		}
		newpet.setRunning();
		if (!newpet.isRespawned()) {
			newpet.store();
		}
		

        activeChar.setPet(newpet);

		MagicSkillUser msk = new MagicSkillUser(activeChar, 2046, 1, 1000, 600000);
		activeChar.sendPacket(msk);
		SystemMessage sm2 = new SystemMessage(SystemMessage.SUMMON_A_PET);
		activeChar.sendPacket (sm2);
        L2World.getInstance().storeObject(newpet);
		newpet.spawnMe(activeChar.getX()+50, activeChar.getY()+100, activeChar.getZ());
        PetInfo pInfo = new PetInfo(newpet);
        activeChar.sendPacket(pInfo);
		newpet.startFeed( false );
		item.setEnchantLevel(newpet.getLevel());
		
		// continue execution in 1 seconds
		ThreadPoolManager.getInstance().scheduleGeneral(new SummonFinalizer(activeChar, newpet), 900);
		
		// if pet died of hunger last time it was summoned
		if ( newpet.getCurrentFed() <= 0 )
		{
			// wait 1 minute for owner to feed
			ThreadPoolManager.getInstance().scheduleGeneral(new FeedWait(activeChar, newpet), 60000);
		}
		else
		{
			// start normal feeding
			newpet.startFeed( false );
		}
	}
	
	static class FeedWait implements Runnable
	{
		private L2PcInstance _activeChar;
		private L2PetInstance _newpet;
		
		FeedWait(L2PcInstance activeChar, L2PetInstance newpet)
		{
			_activeChar = activeChar;
			_newpet = newpet;
		}
		
		public void run()
		{
			try
			{
				// owner didn't feed on time
				if ( _newpet.getCurrentFed() <= 0 )
				{
					_newpet.unSummon(_activeChar);
				}
				else
				{
					// normal feeding
					_newpet.startFeed( false );
				}
			}
			catch (Throwable e)
			{
				_log.severe(e.toString());
			}
		}
	}
	
	static class SummonFinalizer implements Runnable
	{
		private L2PcInstance _activeChar;
		private L2PetInstance _newpet;
		
		SummonFinalizer(L2PcInstance activeChar, L2PetInstance newpet)
		{
			_activeChar = activeChar;
			_newpet = newpet;
		}
		
		public void run()
		{
			try
			{
				_activeChar.sendPacket(new MagicSkillLaunched(_activeChar,2046,1));
				
				_newpet.setFollowStatus(true);
				// addVisibleObject created the info packets with summon animation
				// if someone comes into range now, the animation shouldnt show any more
		        _newpet.setShowSummonAnimation(false);
			}
			catch (Throwable e)
			{
				_log.severe(e.toString());
			}
		}
	}
	
	public int[] getItemIds()
	{
		return _itemIds;
	}
}
