package moe.oko.opennaw.util;

import moe.oko.opennaw.model.City;
import org.bukkit.Location;
import org.dynmap.bukkit.DynmapPlugin;
import org.dynmap.markers.Marker;
import org.dynmap.markers.MarkerAPI;
import org.dynmap.markers.MarkerSet;

public class DynmapHandler {

    public static final String SET_ID = "nawCity";
    private MarkerAPI markerAPI;
    private MarkerSet citySet;

    public DynmapHandler() {
        markerAPI = DynmapPlugin.plugin.getMarkerAPI();
        if(markerAPI.getMarkerSet(SET_ID) != null) {
            citySet = markerAPI.getMarkerSet(SET_ID);
        } else {
            //Creating a new Dynmap set with the ID predefined (finally) in the class, alongside the label "Cities",
            //that has no limit on icon usage (any), and is persistent. (Dynmap has nearly no docs pertaining to api usage).
            citySet = markerAPI.createMarkerSet(SET_ID, "Cities", markerAPI.getMarkerIcons(), true);
        }
    }

    public boolean initializeCityMarker(City city) {
        //Generate a new marker pertaining to shenanigans
        try {
            Location cityLoc = city.getPoint();
            citySet.createMarker(city.getName(), city.getName() + " [**" + city.getOwner().getName() + "**]",
                    true, cityLoc.getWorld().getName(), cityLoc.getX(), cityLoc.getY(), cityLoc.getZ(), markerAPI.getMarkerIcon(city.getOwner().getDynmapIcon()), true);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    //Updates the name and icon of the marker.
    public boolean updateCityInfoMarker(City city) {
        try {
            Marker marker = citySet.findMarker(city.getName());
            marker.setLabel(city.getName() + " [**" + city.getOwner().getName() + "**]");
            marker.setMarkerIcon(markerAPI.getMarkerIcon(city.getOwner().getDynmapIcon()));
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
