package nxm.xbc.prim;

import nxm.sys.inc.PlotFile;
import nxm.sys.lib.Message;
import nxm.sys.lib.Parser;
import nxm.sys.lib.Position;
import nxm.sys.prim.plot;

/**
  A class to mimic the X-Midas plotters with the NeXtMidas plotter

  @author Jeff Schoen
*/
public class xplotters extends plot {

  /** X-Midas XPLOT plot mode */
  public static final int XPLOT=1;
  /** X-Midas XRASTER plot mode */
  public static final int XRASTER=2;
  /** X-Midas XRTPLOT plot mode */
  public static final int XRTPLOT=3;
  /** X-Midas XRTRASTER plot mode */
  public static final int XRTRASTER=4;
  /** X-Midas XGEOPLOT plot mode */
  public static final int XGEOPLOT=5;
  /** X-Midas XP3D plot mode */
  public static final int XP3D=6;
  /** X-Midas plot mode */
  public int plotMode;
  private String xtag,ytag,ztag,xytag;

  /** default constructor */
  public xplotters () {}

  public int open() {
    if (M.macro!=null) M.macro.adjustStaticCount(1);

    // determine the plot mode
    if (this instanceof xplot) plotMode=XPLOT;
    if (this instanceof xrtplot) plotMode=XRTPLOT;
    if (this instanceof xraster) plotMode=XRASTER;
    if (this instanceof xrtraster) plotMode=XRTRASTER;
    if (this instanceof xgeoplot) plotMode=XGEOPLOT;
    if (this instanceof xp3d) plotMode=XP3D;

    // convert line type defaults
    type = (plotMode==XRASTER || plotMode==XRTRASTER)? PlotFile.T_RASTER : PlotFile.T_LINE;

    // controls mode converted to Plots list
    controls = (MA.getL("/XCNT",1)==2)? ONMOTION : ONCLICK;

    // convert complex mode
    String cm = MA.getS("MODE");
    if (cm.startsWith("LO") || cm.equals("D1")) cm = "10LOG";
    if (cm.equals("L2") || cm.equals("D2")) cm = "20LOG";
    if (cm.equals("RI")) cm = "RVI";
    MA.put("CM",cm);

    // adjust ranges for pre mode limits
    if (cm.endsWith("LOG")) {
      double scl = 10.0 / 2.3;
      if (cm.startsWith("2")) scl *= 2;
      if (plotMode==XRASTER || plotMode==XRTRASTER) {
        double min = 1e-10;
        if (MA.find("Z1")) MA.put("Z1", ""+scl*Math.log(Math.max(min,MA.getD("Z1"))));
        if (MA.find("Z2")) MA.put("Z2", ""+scl*Math.log(Math.max(min,MA.getD("Z2"))));
      }
      if (plotMode==XPLOT || plotMode==XRTPLOT) {
        double min = 1e-20;
        if (MA.find("Y1")) MA.put("Y1", ""+scl*Math.log(Math.max(min,MA.getD("Y1"))));
        if (MA.find("Y2")) MA.put("Y2", ""+scl*Math.log(Math.max(min,MA.getD("Y2"))));
      }
    }

    // parse for mouse tags
    String mtag = MA.getS("/MTAG");
    if (mtag!=null && mtag.length()>0) {
      if (mtag.charAt(0)=='(') mtag = mtag.substring(1,mtag.length()-1);
      Parser p = new Parser(mtag);
      for (int i=1; i<=p.elements(); i++) {
        String tag = p.get(i);
        if (tag.startsWith("X:")) xtag = tag.substring(2);
        if (tag.startsWith("Y:")) ytag = tag.substring(2);
        if (tag.startsWith("Z:")) ztag = tag.substring(2);
        if (tag.startsWith("XY:")) xytag = tag.substring(3);
        if (tag.startsWith("X+Y:")) xytag = tag.substring(4);
      }
    }

    // parse for mouse widget IDs
    int wid;
    wid = MA.getL("/WIDX"); if (wid>0) xtag = "W_"+wid;
    wid = MA.getL("/WIDY"); if (wid>0) ytag = "W_"+wid;
    wid = MA.getL("/WIDZ"); if (wid>0) ztag = "W_"+wid;
    wid = MA.getL("/WIDXY"); if (wid>0) xytag = "W_"+wid;

    // readout
    String readout = MA.getState("/NOSPECS")? "-ONAXIS":"+ONAXIS";
    MA.put("/READOUT",readout);

    int status = super.open();

    // parse for plot name
    if (MA.find("/XN")) MW.setTitle(MA.getCS("/XN"));

    return status;
  }

  protected void sendMouseMessage (Message msg, int type) {
    /* Send to macro is type is set in controls. */
    if ((controls & type) != 0) {
      Position pos = (Position)msg.data;
      if (xtag!=null) MR.put(xtag,pos.x);
      if (ytag!=null) MR.put(ytag,pos.y);
      if (ztag!=null) MR.put(ztag,pos.z);
      if (xytag!=null) MR.put(xytag,pos.x+pos.y);
    }
    super.sendMouseMessage (msg,type);
  }

}