package nxm.ucl.prim;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import nxm.sys.inc.Chainable;
import nxm.sys.lib.Message;
import nxm.sys.libg.GPrimitive;
import nxm.sys.libg.MWindow;
import nxm.ucl.libg.PsdPanelLive;

/** Used primarily to test Wayne's application
    @since neXtMidas 2.7.2
 */
public class psdpanel extends GPrimitive implements Chainable {
  private JPanel       mainPanel    = new JPanel(new BorderLayout());
  private PsdPanelLive livePsdPanel = null;
  private JTabbedPane  tabbedPane   = new JTabbedPane();
  private JPanel       blankPane1   = new JPanel();
  private JPanel       blankPane2   = new JPanel();

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

  public int open () {
    MW = new MWindow("PsdPanel", this);
    MW.open();
    MW.setSize(200, 250);
    MW.addTo(this);
    init(MW);
    return NORMAL;
  }

  public int close () {
    if (MW != null) MW.close();
    return NORMAL;
  }

  private void init (final MWindow mw) {
    /*try {
      SwingUtilities.invokeAndWait(new Runnable() { public void run() { init0(mw); }});
    } catch (Exception e) {
      e.printStackTrace();
    }*/
    init0(mw);
  }

  private void init0 (final MWindow mw) {
    Color bg = MW.panel.getBackground();
    Color fg = MW.panel.getForeground();
    livePsdPanel = new PsdPanelLive(M,MQ,MR,bg,fg,"Live",1);
    try {
      SwingUtilities.invokeAndWait(new Runnable() { public void run() {
        setupPanels(mw);
      }});
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void setupPanels (final MWindow mw) {
    mainPanel.add(tabbedPane, BorderLayout.CENTER);

    tabbedPane.addTab("BLANK",null, blankPane1);
    tabbedPane.addTab("LIVE", null, livePsdPanel);
    tabbedPane.addTab("BLANK",null, blankPane2);

    if (mw != null) {
      mw.getPanel().setLayout(new java.awt.GridLayout(1,1));
      mw.getPanel().add(mainPanel);
      mainPanel.setBounds(mw.getPanel().getBounds());
      mw.getPanel().setBounds(mw.getPanel().getBounds());
    }
  }

  public int processMessage (Message msg) {
    int status = NOOP;
    if (livePsdPanel != null) status = livePsdPanel.processMessage(msg);
    if (status==NORMAL) return status;

    if (msg.name.equals("WINDOW")) {
      if      (msg.data.equals("CLOSING")) { close(); }
      else if (msg.name.equals("TOGGLE"))  { MW.pop(-1); }
      else if (msg.name.equals("PUSH"))    { MW.pop(0); }
      else if (msg.name.equals("POP"))     { MW.pop(1); }
    }
    else if (msg.name.equals("RESIZE") || msg.name.equals("REFRESH") || msg.name.equals("MOVED")) {
      if (MW==null || MW.status==0 || !thisIsMe()) {
        MQ.remove(msg.name);
        MQ.put(msg);
        return (NOOP);
      }
      if (msg.name.equals("REFRESH")) { MW.refresh(); }
      else if (msg.name.equals("RESIZE") || msg.name.equals("MOVED")) {
        mainPanel.setBounds(0, 0, MW.pos.w, MW.pos.h);
        mainPanel.paintAll(mainPanel.getGraphics());
      }
    }
    else if (msg.name.equals("POINTER" )) { }  // do nothing
    else if (msg.name.equals("PAINT")) { } // do nothing
    else { M.warning("PSDPANEL unhandled msg: " +msg); }

    return status;
  }

}
