package cn.citycraft.AuthMe.settings;

import org.bukkit.Bukkit;
import org.bukkit.Location;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.PluginHelper.config.FileConfig;

/**
 *
 * @author Xephi59
 */
public class Spawn extends FileConfig {

	private static Spawn spawn;

	public Spawn() {
		super(AuthMe.getInstance(), "spawn.yml");
		spawn = this;
	}

	public static Spawn getInstance() {
		if (spawn == null) {
			spawn = new Spawn();
		}
		return spawn;
	}

	public Location getFirstSpawn() {
		try {
			if (this.getString("firstspawn.world").isEmpty() || this.getString("firstspawn.world").equals("")) {
				return null;
			}
			final Location location = new Location(Bukkit.getWorld(this.getString("firstspawn.world")), this.getDouble("firstspawn.x"), this.getDouble("firstspawn.y"), this
					.getDouble("firstspawn.z"), Float.parseFloat(this.getString("firstspawn.yaw")), Float.parseFloat(this.getString("firstspawn.pitch")));
			return location;
		} catch (NullPointerException | NumberFormatException npe) {
			return null;
		}
	}

	@Deprecated
	public Location getLocation() {
		return getSpawn();
	}

	public Location getSpawn() {
		try {
			if (this.getString("spawn.world").isEmpty() || this.getString("spawn.world").equals("")) {
				return null;
			}
			final Location location = new Location(Bukkit.getWorld(this.getString("spawn.world")), this.getDouble("spawn.x"), this.getDouble("spawn.y"), this.getDouble("spawn.z"), Float
					.parseFloat(this.getString("spawn.yaw")), Float.parseFloat(this.getString("spawn.pitch")));
			return location;
		} catch (NullPointerException | NumberFormatException npe) {
			return null;
		}
	}

	public boolean setFirstSpawn(final Location location) {
		try {
			set("firstspawn.world", location.getWorld().getName());
			set("firstspawn.x", location.getX());
			set("firstspawn.y", location.getY());
			set("firstspawn.z", location.getZ());
			set("firstspawn.yaw", location.getYaw());
			set("firstspawn.pitch", location.getPitch());
			save();
			return true;
		} catch (final NullPointerException npe) {
			return false;
		}
	}

	public boolean setSpawn(final Location location) {
		try {
			set("spawn.world", location.getWorld().getName());
			set("spawn.x", location.getX());
			set("spawn.y", location.getY());
			set("spawn.z", location.getZ());
			set("spawn.yaw", location.getYaw());
			set("spawn.pitch", location.getPitch());
			save();
			return true;
		} catch (final NullPointerException npe) {
			return false;
		}
	}
}
