/*
 * Copyright © 2004-2021 L2J Server
 * 
 * This file is part of L2J Server.
 * 
 * L2J Server 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.
 * 
 * L2J Server 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 <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver;

import java.lang.reflect.Constructor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.l2jserver.commons.util.Rnd;
import com.l2jserver.gameserver.data.xml.impl.NpcData;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;

public class MonsterRace {
	protected static final Logger LOG = LoggerFactory.getLogger(MonsterRace.class);
	
	private final L2Npc[] _monsters;
	private int[][] _speeds;
	private final int[] _first, _second;
	
	protected MonsterRace() {
		_monsters = new L2Npc[8];
		_speeds = new int[8][20];
		_first = new int[2];
		_second = new int[2];
	}
	
	public static MonsterRace getInstance() {
		return SingletonHolder._instance;
	}
	
	public void newRace() {
		int random;
		
		for (int i = 0; i < 8; i++) {
			int id = 31003;
			random = Rnd.get(24);
			while (true) {
				for (int j = i - 1; j >= 0; j--) {
					if (_monsters[j].getTemplate().getId() == (id + random)) {
						random = Rnd.get(24);
					}
				}
				break;
			}
			try {
				L2NpcTemplate template = NpcData.getInstance().getTemplate(id + random);
				Constructor<?> constructor = Class.forName("com.l2jserver.gameserver.model.actor.instance." + template.getType() + "Instance").getConstructors()[0];
				_monsters[i] = (L2Npc) constructor.newInstance(template);
			} catch (Exception e) {
				LOG.warn("Unable to create monster!", e);
			}
		}
		newSpeeds();
	}
	
	public void newSpeeds() {
		_speeds = new int[8][20];
		int total;
		_first[1] = 0;
		_second[1] = 0;
		for (int i = 0; i < 8; i++) {
			total = 0;
			for (int j = 0; j < 20; j++) {
				if (j == 19) {
					_speeds[i][j] = 100;
				} else {
					_speeds[i][j] = Rnd.get(60) + 65;
				}
				total += _speeds[i][j];
			}
			if (total >= _first[1]) {
				_second[0] = _first[0];
				_second[1] = _first[1];
				_first[0] = 8 - i;
				_first[1] = total;
			} else if (total >= _second[1]) {
				_second[0] = 8 - i;
				_second[1] = total;
			}
		}
	}
	
	/**
	 * @return Returns the monsters.
	 */
	public L2Npc[] getMonsters() {
		return _monsters;
	}
	
	/**
	 * @return Returns the speeds.
	 */
	public int[][] getSpeeds() {
		return _speeds;
	}
	
	public int getFirstPlace() {
		return _first[0];
	}
	
	public int getSecondPlace() {
		return _second[0];
	}
	
	private static class SingletonHolder {
		protected static final MonsterRace _instance = new MonsterRace();
	}
}
