package nxm.sys.prim;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import nxm.sys.lib.Message;
import nxm.sys.lib.Midas;
import nxm.sys.lib.Table;
import nxm.sys.libg.GMenu;
import nxm.sys.libg.GPrimitive;
import nxm.sys.libg.MJText;
import nxm.sys.libg.MWText;
import nxm.sys.libg.MWindow;


/**
  A class to display INFO|WARN|ERROR messages from a macro context.

  @author Jeff Schoen

*/
public class msglog extends GPrimitive implements ActionListener {

  MWText MWT;
  String lastmsg;
  int info,warn,err,other,banner;
  /** list of banner locations */
  public static final String bannerList = "None,Top,Pane";

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

  @Override
  public int open() {

    int lines  = MA.getL("/LINES",200);
    int rows   = MA.getL("/ROWS",24);
    int cols   = MA.getL("/COLS",80);
    int fontsz = MA.getL("/FONTSZ",12);
    int flags  = MJText.FIFO | MJText.COLOR | MJText.LOGGER;

   if (MA.getState("/WRAP")) flags |= MJText.WRAP;

    banner = MA.getChoice("/BANNER",bannerList,2);
    if (banner==2) flags |= MJText.BANNER;

    MWT = new MWText("MsgLog",lines,rows,cols,fontsz,flags,this);

    if (MWT.mtext!=null) MWT.mtext.addActionListener(this);
    setBanner();

    MW = (MWindow) MWT;
    MW.open();
    MW.addTo(this);

    M.msglog = getID();

    return (NORMAL);
  }

  @Override
  public int process() {
    return (NOOP);
  }

  @Override
  public int processMessage (Message msg) {
    int status = NORMAL;
         if (msg.name.equals("EXIT")) setState(FINISH);
    else if (msg.name.equals("RESIZE")) {
      MW.resize(0);
      if (MWT.mtext!=null) MWT.mtext.setScroll(-1);
    }
    else if (msg.name.equals("LOG")) {
      String head="";
      switch (msg.info) {
        case Midas.INFO:     head="INFO: ";  info++;  break;
        case Midas.WARNING:  head="WARN: ";  warn++;  break;
        case Midas.ERROR:    head="ERROR: "; err++;   break;
        default:             head="";        other++; break;
      }
      setBanner();
      lastmsg = head+msg.data;
      if (MWT.mtext!=null) MWT.mtext.add(lastmsg);
    }
    else if (msg.name.equals("MSGLOG")) {
      String text = msg.getS();
      if (text.equals("CLEAR")) clear();
      if (text.equals("CONFIGURE")) configure();
      if (text.equals("EXIT")) setState(FINISH);
    }
    else status = MW.processMessage(msg);
    return status;
  }

  @Override
  public int close() {
    MW.close();
    M.msglog = null;
    return (NORMAL);
  }

  private void clear() {
    MWT.mtext.removeAll();
    info = warn = err = other = 0;
    setBanner();
  }

  private void configure() {  }

  private void setBanner() {
    String text = "MsgLog: Info="+info+" Warn="+warn+" Err="+err+" Other="+other;
    if (banner==2) {
      MWT.mtext.setBanner(text);
    }
    else if (banner==3 && MWT.parent instanceof nxm.sys.prim.panel.Pane) {
      ((nxm.sys.prim.panel.Pane)MWT.parent).setTitle(text);
    }
  }

  public void actionPerformed (ActionEvent e) {
    String action = e.getActionCommand();
    if (action.equals("POP")) {
      if (MW.status!=MWindow.EXIT) MW.pop(-1);
    }
    else if (action.equals("BANNER")) {
      if (MW.status!=MWindow.EXIT) MW.pop(-1);
    }
    else if (action.equals("BUTTON")) {
      int button = e.getModifiers();
      if (button == 2) new GMenu (MW,"MSGLOG","Clear,Configure,Exit",0,0,this);
    }
  }

  /** Get Table listing number of each type of message
   *  @return Table listing number of each type of message */
  public Table getStats() {
    Table t = new Table();
    t.put("INFO",info);
    t.put("WARN",warn);
    t.put("ERR",err);
    t.put("OTHER",other);
    return t;
  }
  /** Get last message
   *  @return last message */
  public String getLast() { return lastmsg; }

}

