content: /**
   * This file is part of Aion-Lightning <aion-lightning.org>.
   *
   *  Aion-Lightning 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.
   *
   *  Aion-Lightning 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 Aion-Lightning.
   *  If not, see <http://www.gnu.org/licenses/>.
   */
  package com.aionemu.gameserver.controllers.effect;
  
  import java.util.Collection;
  import java.util.Collections;
  
  import com.aionemu.gameserver.configs.main.CustomConfig;
  import com.aionemu.gameserver.dataholders.DataManager;
  import com.aionemu.gameserver.model.gameobjects.Creature;
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_ABNORMAL_STATE;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_PLAYER_STANCE;
  import com.aionemu.gameserver.skillengine.model.Effect;
  import com.aionemu.gameserver.skillengine.model.SkillTargetSlot;
  import com.aionemu.gameserver.skillengine.model.SkillTemplate;
  import com.aionemu.gameserver.taskmanager.tasks.PacketBroadcaster.BroadcastMode;
  import com.aionemu.gameserver.taskmanager.tasks.TeamEffectUpdater;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  
  /**
   * @author ATracer
   */
  public class PlayerEffectController extends EffectController {
  
  	public PlayerEffectController(Creature owner) {
  		super(owner);
  	}
  
  	@Override
  	public void addEffect(Effect effect) {
  		if (checkDuelCondition(effect) && !effect.getIsForcedEffect()) {
  			return;
  		}
  
  		super.addEffect(effect);
  		updatePlayerIconsAndGroup(effect);
  	}
  
  	@Override
  	public void clearEffect(Effect effect) {
  		super.clearEffect(effect);
  		updatePlayerIconsAndGroup(effect);
  	}
  
  	@Override
  	public Player getOwner() {
  		return (Player) super.getOwner();
  	}
  
  	/**
  	 * @param effect
  	 */
  	private void updatePlayerIconsAndGroup(Effect effect) {
  		if (!effect.isPassive()) {
  			updatePlayerEffectIcons();
  			if (getOwner().isInTeam()) {
  				TeamEffectUpdater.getInstance().startTask(getOwner());
  			}
  		}
  	}
  
  	@Override
  	public void updatePlayerEffectIcons() {
  		getOwner().addPacketBroadcastMask(BroadcastMode.UPDATE_PLAYER_EFFECT_ICONS);
  	}
  
  	@Override
  	public void updatePlayerEffectIconsImpl() {
  		Collection<Effect> effects = getAbnormalEffectsToShow();
  		PacketSendUtility.sendPacket(getOwner(), new SM_ABNORMAL_STATE(effects, abnormals));
  	}
  
  	/**
  	 * Effect of DEBUFF should not be added if duel ended (friendly unit)
  	 *
  	 * @param effect
  	 * @return
  	 */
  	private boolean checkDuelCondition(Effect effect) {
  		Creature creature = effect.getEffector();
  		if (creature instanceof Player) {
  			if (!getOwner().isEnemy(creature) && effect.getTargetSlot() == SkillTargetSlot.DEBUFF.ordinal()) {
  				return true;
  			}
  		}
  
  		return false;
  	}
  
  	/**
  	 * @param skillId
  	 * @param skillLvl
  	 * @param currentTime
  	 * @param reuseDelay
  	 */
  	public void addSavedEffect(int skillId, int skillLvl, int remainingTime, long endTime) {
  		SkillTemplate template = DataManager.SKILL_DATA.getSkillTemplate(skillId);
  
  		if (remainingTime <= 0) {
  			return;
  		}
  		if (CustomConfig.ABYSSXFORM_LOGOUT && template.isDeityAvatar()) {
  
  			if (System.currentTimeMillis() >= endTime) {
  				return;
  			}
  			else {
  				remainingTime = (int) (endTime - System.currentTimeMillis());
  			}
  		}
  
  		Effect effect = new Effect(getOwner(), getOwner(), template, skillLvl, remainingTime);
  		abnormalEffectMap.put(effect.getStack(), effect);
  		effect.addAllEffectToSucess();
  		effect.startEffect(true);
  
  		if (effect.getSkillTemplate().getTargetSlot() != SkillTargetSlot.NOSHOW) {
  			PacketSendUtility.sendPacket(getOwner(), new SM_ABNORMAL_STATE(Collections.singletonList(effect), abnormals));
  		}
  
  	}
  
  	@Override
  	public void broadCastEffectsImp() {
  		super.broadCastEffectsImp();
  		Player player = getOwner();
  		if (player.getController().isUnderStance()) {
  			PacketSendUtility.sendPacket(player, new SM_PLAYER_STANCE(player, 1));
  		}
  	}
  }
  