/*
 * 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.l2dot.gameserver.network.clientpackets;

import java.util.logging.Logger;

import com.l2dot.Config;
import com.l2dot.gameserver.model.L2Object;
import com.l2dot.gameserver.model.L2World;
import com.l2dot.gameserver.model.actor.instance.L2PcInstance;
import com.l2dot.gameserver.network.SystemMessageId;
import com.l2dot.gameserver.network.serverpackets.ActionFailed;
import com.l2dot.gameserver.network.serverpackets.SendTradeRequest;
import com.l2dot.gameserver.network.serverpackets.SystemMessage;
import com.l2dot.gameserver.util.Util;


/**
 *
 * This class ...
 *
 * @version $Revision: 1.2.2.1.2.3 $ $Date: 2005/03/27 15:29:30 $
 */
public final class TradeRequest extends L2GameClientPacket
{
	private static final String TRADEREQUEST__C__15 = "[C] 15 TradeRequest";
	private static Logger _log = Logger.getLogger(TradeRequest.class.getName());

	private int _objectId;

	@Override
	protected void readImpl()
	{
		_objectId = readD();
	}

	@Override
	protected void runImpl()
	{
		L2PcInstance player = getClient().getActiveChar();
        if (player == null) return;

        if (Config.GM_DISABLE_TRANSACTION && player.getAccessLevel() >= Config.GM_TRANSACTION_MIN && player.getAccessLevel() <= Config.GM_TRANSACTION_MAX)
        {
        	player.sendMessage("Transactions are disable for your Access Level");
            sendPacket(new ActionFailed());
            return;
        }


		L2Object target = L2World.getInstance().findObject(_objectId);
        if (target == null || !player.getKnownList().knowsObject(target)
        		|| !(target instanceof L2PcInstance) || (target.getObjectId() == player.getObjectId()))
		{
			player.sendPacket(new SystemMessage(SystemMessageId.TARGET_IS_INCORRECT));
			return;
		}

        L2PcInstance partner = (L2PcInstance)target;

        if (partner.isInOlympiadMode() || player.isInOlympiadMode())
        {
            player.sendMessage("You or your target cant request trade in Olympiad mode");
            return;
        }
                        
                // L2Dot Faction Good vs Evil
                L2PcInstance player2 = (L2PcInstance) target;
                if (player2.isevil() && player.isgood() && Config.L2JMOD_GVE_ENABLE_FACTION)
                {
                player.sendMessage("You Can't Trade with Different Faction");
                return;
                }
                        
                if (player2.isgood() && player.isevil() && Config.L2JMOD_GVE_ENABLE_FACTION)
                {
                player.sendMessage("You Can't Trade with Different Faction");
                return;
                }

        // Alt game - Karma punishment
        if (!Config.ALT_GAME_KARMA_PLAYER_CAN_TRADE && (player.getKarma() > 0 || partner.getKarma() > 0))
        {
        	player.sendMessage("Chaotic players can't use Trade.");
            return;
        }

        if (player.getPrivateStoreType() != 0 || partner.getPrivateStoreType() != 0)
        {
            player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_TRADE_DISCARD_DROP_ITEM_WHILE_IN_SHOPMODE));
            return;
        }
        
        if (!Config.ALLOW_LOW_LEVEL_TRADE)    
         if (player.getLevel() <= 76 || player.getLevel() > 1)
        {
            player.sendMessage("You Cannot Trade a Lower Level Character");
        }
        
        if (player.isProcessingTransaction())
		{
			if (Config.DEBUG) _log.fine("already trading with someone");
			player.sendPacket(new SystemMessage(SystemMessageId.ALREADY_TRADING));
			return;
		}

		if (partner.isProcessingRequest() || partner.isProcessingTransaction())
		{
			if (Config.DEBUG) _log.info("transaction already in progress.");
			SystemMessage sm = new SystemMessage(SystemMessageId.S1_IS_BUSY_TRY_LATER);
			sm.addString(partner.getName());
			player.sendPacket(sm);
            return;
		}

        if (partner.getTradeRefusal())
        {
            player.sendMessage("Target is in trade refusal mode");
            return;
        }
        
     	if (Util.calculateDistance(player, partner, true) > 150) 
     	{ 
     	 	SystemMessage sm = new SystemMessage(SystemMessageId.TARGET_TOO_FAR); 
     	 	player.sendPacket(sm); 
     	 	return; 
     	} 
		
        if (partner.getAllowTrade() == false)
       {
        	player.sendMessage("Target is not allowed to receive more than one trade request at the same time.");
        	return;
        }
        partner.setAllowTrade(false);
        player.setAllowTrade(false);
		player.onTransactionRequest(partner);
		partner.sendPacket(new SendTradeRequest(player.getObjectId()));
		SystemMessage sm = new SystemMessage(SystemMessageId.REQUEST_S1_FOR_TRADE);
		sm.addString(partner.getName());
		player.sendPacket(sm);
	}

	@Override
	public String getType()
	{
		return TRADEREQUEST__C__15;
	}
}
