/*
 * 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.itemhandlers;
import java.util.List;
import org.l2jmobius.commons.util.Rnd;
import org.l2jmobius.gameserver.enums.ItemSkillType;
import org.l2jmobius.gameserver.enums.ShotType;
import org.l2jmobius.gameserver.handler.IItemHandler;
import org.l2jmobius.gameserver.model.actor.Playable;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.holders.ItemSkillHolder;
import org.l2jmobius.gameserver.model.items.Weapon;
import org.l2jmobius.gameserver.model.items.instance.ItemInstance;
import org.l2jmobius.gameserver.model.items.type.ActionType;
import org.l2jmobius.gameserver.network.SystemMessageId;
import org.l2jmobius.gameserver.network.serverpackets.MagicSkillUse;
import org.l2jmobius.gameserver.util.Broadcast;
public class SoulShots implements IItemHandler
{
    @Override
    public boolean useItem(Playable playable, ItemInstance item, boolean forceUse)
    {
        if (!playable.isPlayer())
        {
            playable.sendPacket(SystemMessageId.YOUR_PET_CANNOT_CARRY_THIS_ITEM);
            return false;
        }
        
        final PlayerInstance player = playable.getActingPlayer();
        final ItemInstance weaponInst = player.getActiveWeaponInstance();
        final Weapon weaponItem = player.getActiveWeaponItem();
        final List<ItemSkillHolder> skills = item.getItem().getSkills(ItemSkillType.NORMAL);
        if (skills == null)
        {
            LOGGER.warning(getClass().getSimpleName() + ": is missing skills!");
            return false;
        }
        
        final int itemId = item.getId();
        
        // Check if Soul shot can be used
        if ((weaponInst == null) || (weaponItem.getSoulShotCount() == 0))
        {
            if (!player.getAutoSoulShot().contains(itemId))
            {
                player.sendPacket(SystemMessageId.CANNOT_USE_SOULSHOTS);
            }
            return false;
        }
        
        final boolean gradeCheck = item.isEtcItem() && (item.getEtcItem().getDefaultAction() == ActionType.SOULSHOT) && (weaponInst.getItem().getCrystalTypePlus() == item.getItem().getCrystalTypePlus());
        
        if (!gradeCheck)
        {
            if (!player.getAutoSoulShot().contains(itemId))
            {
                player.sendPacket(SystemMessageId.THE_SOULSHOT_YOU_ARE_ATTEMPTING_TO_USE_DOES_NOT_MATCH_THE_GRADE_OF_YOUR_EQUIPPED_WEAPON);
            }
            return false;
        }
        
        player.soulShotLock.lock();
        try
        {
            // Check if Soul shot is already active
            if (player.isChargedShot(ShotType.SOULSHOTS))
            {
                return false;
            }
            
            // Consume Soul shots if player has enough of them
            int SSCount = weaponItem.getSoulShotCount();
            if ((weaponItem.getReducedSoulShot() > 0) && (Rnd.get(100) < weaponItem.getReducedSoulShotChance()))
            {
                SSCount = weaponItem.getReducedSoulShot();
            }
            
            if (!player.destroyItemWithoutTrace("Consume", item.getObjectId(), SSCount, null, false))
            {
                if (!player.disableAutoShot(itemId))
                {
                    player.sendPacket(SystemMessageId.YOU_DO_NOT_HAVE_ENOUGH_SOULSHOTS_FOR_THAT);
                }
                return false;
            }
            // Charge soul shot
            player.chargeShot(ShotType.SOULSHOTS);
        }
        finally
        {
            player.soulShotLock.unlock();
        }
        
        // Send message to client
        if (!player.getAutoSoulShot().contains(item.getId()))
        {
            player.sendPacket(SystemMessageId.YOUR_SOULSHOTS_ARE_ENABLED);
        }
        
        // Visual effect change if player has equipped Ruby lvl 3 or higher
        if (player.getActiveRubyJewel() != null)
        {
            Broadcast.toSelfAndKnownPlayersInRadius(player, new MagicSkillUse(player, player, player.getActiveRubyJewel().getEffectId(), 1, 0, 0), 600);
        }
        else
        {
            skills.forEach(holder -> Broadcast.toSelfAndKnownPlayersInRadius(player, new MagicSkillUse(player, player, holder.getSkillId(), holder.getSkillLevel(), 0, 0), 600));
        }
        return true;
    }
}