package nxm.sys.libg;

import java.awt.image.BufferedImage;

import nxm.sys.lib.StringUtil;
import nxm.sys.lib.Table;

/** A class to hold and plot images from the NeXtMidas Map Server (NMS). Since NeXtMidas 2.5.0 this
    extends {@link LayerMap} for most of its capabilities.

    @author  J. Czechowski IV
    @since   NeXtMidas 2.1.0
 */
public class LayerNMS extends LayerMap {
  private   static final String  PROTOCOL     = "NMS_1";        // Protocol id/version.
  private   static final boolean CHECK_LENGTH = true;           // Check image length?
  private   static final int     MAX_TRYS     = 3;              // Max retries

  protected String  options     = "DEF";   // Options (as given by user).
  protected String  optionsStr  = "DEF";   // Options (as sent to server).
  protected String  background  = "NONE";  // Background to use.

  /** Default constructor */
  public LayerNMS () {
    super("NMS");
    setDownloadThreads(8);
  }

  @Override
  protected boolean getDefUseTile ()     { return true; }
  @Override
  protected int     getDefTileLevels ()  { return 32;   }
  @Override
  protected int     getDefTileWidth ()   { return 256;  }
  @Override
  protected int     getDefTileHeight ()  { return 256;  }

  @Override
  public void setUseTiles (boolean tile) {
    if (!tile) warn("This service requires the use of tiles. UseTiles can not be changed to false.");
    super.setUseTiles(true);
  }

  @Override
  public boolean isConnected() {
    return (getURL() != null);
  }

  /** Sets the name of the background to use.
   *  @param bg the name of the background to use */
  public void setBackground(String bg) {
    if (!bg.equalsIgnoreCase(background)) {
      background = bg;
      refresh();
    }
  }

  /** Gets the name of the background to use.
   *  @return the name of the background to use */
  public String getBackground() {
    return background;
  }

  /** Sets the options mask indicating what to display.
   *  @param opts the options mask indicating what to display */
  public void setOptions (String opts) {
    if (!opts.equalsIgnoreCase(options)) {
      String[] optItems = opts.toUpperCase().split("[|]");
      for (int i = 0; i < optItems.length; i++) {
              if (optItems[i].startsWith("-")) { optItems[i] = "NO"+optItems[i].substring(1);  }
         else if (optItems[i].startsWith("+")) { optItems[i] = "ADD"+optItems[i].substring(1); }
      }

      options    = opts;
      optionsStr = StringUtil.join(optItems, "+");
      refresh();
    }
  }

  /** Gets the options mask indicating what to display.
   *  @return the options mask indicating what to display
   */
  public String getOptions () {
    return options;
  }

  @Override
  protected int drawArea(double minLon, double maxLon, double minLat, double maxLat,
                         int width, int height, boolean hiRes, String pos) {
    // Get and plot each of the tiles
    String url = mapServer+"?ver="+PROTOCOL+"&background="+background+"&options="+optionsStr+"&v=3&t="+pos;
    BufferedImage img = requestImage(url, MAX_TRYS, CHECK_LENGTH, width, height);
    if (img != null) drawImage(img, minLon, maxLon, minLat, maxLat);
    return 0;
  }

  @Override
  public Table getConfig() {
    Table  tbl = super.getConfig();

    tbl.put("BACKGROUND", getBackground());
    tbl.put("OPTIONS",    getOptions());

    return tbl;
  }
}
