/* 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 net.sf.l2j.custom;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Logger;

import javolution.util.FastList;
import javolution.util.FastMap;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.model.L2World;

/**
 *
 * @author  JOJO
 */
public class ZoneName
{
	class ZoneItem
	{
//		int id;
		String zone_name;
		int top_z;
		int bottom_z;
		int map_x1;
		int map_y1;
		int map_x2;
		int map_y2;
	}

	private static Logger _log = Logger.getLogger(ZoneName.class.getName());

	private static ZoneName _instance;

	private static FastMap<Integer, FastList<ZoneItem>> _zonetree = new FastMap<Integer, FastList<ZoneItem>>();

	public static ZoneName getInstance()
	{
///*[DEBUG]*/ _instance = null;
		if (_instance == null)
		{
			_instance = new ZoneName();
		}
		return _instance;
	}

	public static void deleteInstance()
	{
		_instance = null;
	}

	private ZoneName()
	{
		java.sql.Connection con = null;
		try
		{
			con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement statement = con.prepareStatement(
						  "SELECT id, zone_name, x_world_grid, y_world_grid, top_z, bottom_z"
						+ ", map_x, map_y, map_x2, map_y2, map_width_x, map_width_y, map_scale"
						+ " FROM zonename ORDER BY top_z-bottom_z, zone_color_id");
			ResultSet rs = statement.executeQuery();
			while (rs.next())
			{
			//	int id = rs.getInt("id");
				int x_world_grid = rs.getShort("x_world_grid");	// 16..26
				int y_world_grid = rs.getShort("y_world_grid");	// 10..26
				if (x_world_grid == 0 && y_world_grid == 0) continue;

				/*Integer*/ int hashkey = (x_world_grid << 5) + y_world_grid;
				ZoneItem item = new ZoneItem();
				//	item.id          = rs.getInt("id");
					item.zone_name   = rs.getString("zone_name");
					item.top_z       = rs.getInt("top_z");
					item.bottom_z    = rs.getInt("bottom_z");
					float map_scale = rs.getFloat("map_scale");
					if (map_scale != 0.0) {
						item.map_x1 = rs.getInt("map_x");
						item.map_y1 = rs.getInt("map_y");
						item.map_x2 = (int)(rs.getInt("map_width_x") / map_scale) + item.map_x1;
						item.map_y2 = (int)(rs.getInt("map_width_y") / map_scale) + item.map_y1;
					}
					else if (rs.getInt("map_x2") != 0) {
						item.map_x1 = rs.getInt("map_x");
						item.map_y1 = rs.getInt("map_y");
						item.map_x2 = rs.getInt("map_x2");
						item.map_y2 = rs.getInt("map_y2");
					}
				FastList<ZoneItem> zz;
				if ((zz = _zonetree.get(hashkey)) == null) {
					zz = new FastList<ZoneItem>();
					_zonetree.put(hashkey, zz);
				}
				zz.add(item);
			}
		}
		catch (Exception e)
		{
			_log.warning("error reading ZoneName: "+e);
		} 
		finally 
		{
			try { con.close(); } catch (Exception e) {}
		}
	}

//	public String getName(int posX, int posY, int posZ, int z)
//	{
//		Zone zone = ZoneManager.getInstance().getZone(ZoneType.getZoneTypeName(ZoneType.ZoneTypeEnum.Water), getX(), getY()); //checking if char is in water zone
//	}

	public String getName(int posX, int posY, int posZ)
	{
		int x_world_grid = ((posX - L2World.MAP_MIN_X) >> 15) + 16;
		int y_world_grid = ((posY - L2World.MAP_MIN_Y) >> 15) + 10;
		int hashkey = (x_world_grid << 5) + y_world_grid;

		FastList<ZoneItem> items = _zonetree.get(hashkey);
		if (items == null) return null;
		for (ZoneItem item : items) {
			if (item.bottom_z <= posZ && posZ <= item.top_z) {
				if (item.map_x1 != 0) {
					if (item.map_x1 <= posX && posX <= item.map_x2
					 && item.map_y1 <= posY && posY <= item.map_y2) {
/*DEBUG*/				System.out.println("*** ZoneName! "+x_world_grid + "_" + y_world_grid+"_"+item.zone_name);	//XXX:[JOJO]
						return x_world_grid + "_" + y_world_grid+"_"+item.zone_name;
					}
				} else {
/*DEBUG*/			System.out.println("*** ZoneName  "+x_world_grid + "_" + y_world_grid+"_"+item.zone_name);	//XXX:[JOJO]
					return x_world_grid + "_" + y_world_grid+"_"+item.zone_name;
				}
			}
		}
		return null;
	}
}
