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.model.skill;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import com.aionemu.commons.utils.Rnd;
  import com.aionemu.gameserver.dataholders.DataManager;
  import com.aionemu.gameserver.model.gameobjects.Npc;
  import com.aionemu.gameserver.model.templates.npcskill.NpcSkillTemplate;
  import com.aionemu.gameserver.model.templates.npcskill.NpcSkillTemplates;
  
  /**
   * @author ATracer
   */
  public class NpcSkillList implements SkillList<Npc> {
  
  	private List<NpcSkillEntry> skills;
  
  	public NpcSkillList(Npc owner) {
  		initSkillList(owner.getNpcId());
  	}
  
  	private void initSkillList(int npcId) {
  		NpcSkillTemplates npcSkillList = DataManager.NPC_SKILL_DATA.getNpcSkillList(npcId);
  		if (npcSkillList != null) {
  			initSkills();
  			for (NpcSkillTemplate template : npcSkillList.getNpcSkills()) {
  				skills.add(new NpcSkillTemplateEntry(template));
  			}
  		}
  	}
  
  	@Override
  	public boolean addSkill(Npc creature, int skillId, int skillLevel) {
  		initSkills();
  		skills.add(new NpcSkillParameterEntry(skillId, skillLevel));
  		return true;
  	}
  
  	@Override
  	public boolean removeSkill(int skillId) {
  		Iterator<NpcSkillEntry> iter = skills.iterator();
  		while (iter.hasNext()) {
  			NpcSkillEntry next = iter.next();
  			if (next.getSkillId() == skillId) {
  				iter.remove();
  				return true;
  			}
  		}
  		return false;
  	}
  
  	@Override
  	public boolean isSkillPresent(int skillId) {
  		if (skills == null) {
  			return false;
  		}
  		return getSkill(skillId) != null;
  	}
  
  	@Override
  	public int getSkillLevel(int skillId) {
  		return getSkill(skillId).getSkillLevel();
  	}
  
  	@Override
  	public int size() {
  		return skills != null ? skills.size() : 0;
  	}
  
  	private void initSkills() {
  		if (skills == null) {
  			skills = new ArrayList<NpcSkillEntry>();
  		}
  	}
  
  	public NpcSkillEntry getRandomSkill() {
  		return skills.get(Rnd.get(0, skills.size() - 1));
  	}
  
  	private SkillEntry getSkill(int skillId) {
  		for (SkillEntry entry : skills) {
  			if (entry.getSkillId() == skillId) {
  				return entry;
  			}
  		}
  		return null;
  	}
  
  	public NpcSkillEntry getUseInSpawnedSkill() {
  		if (this.skills == null) {
  			return null;
  		}
  		Iterator<NpcSkillEntry> iter = skills.iterator();
  		while (iter.hasNext()) {
  			NpcSkillEntry next = iter.next();
  			NpcSkillTemplateEntry tmpEntry = (NpcSkillTemplateEntry) next;
  			if (tmpEntry.UseInSpawned()) {
  				return next;
  			}
  		}
  		return null;
  	}
  }
  