package nxm.ucl.libg;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;

import nxm.sys.inc.InternalUseOnly;
import nxm.sys.inc.MessageHandler;
import nxm.sys.lib.Command;
import nxm.sys.lib.Message;
import nxm.sys.lib.MessageQueue;
import nxm.sys.lib.Midas;
import nxm.sys.lib.Results;
import nxm.sys.libg.MidasBean;
import nxm.sys.libg.MidasDisplay;

/** Used to duplicate BUG 1905 and BUG 2028

    NOTE: PsdPanelLive does NOT currently support Java Serialization, even though it extends classes that support it.

    @author Wayne Dengel
    @since NeXtMidas 2.7.2
 */
@InternalUseOnly("Since NeXtMidas 3.5.2")
public class PsdPanelLive extends WHTabPanel implements MessageHandler {
  /** NOTE: this should be changed if the structure/fields of this class changes.
   *  This is being left to avoid warnings, though this class is not truly serializable.
   *  We are not overwriting readObject and writeObject because serialization will error out on WHTabPanel */
  private static final long serialVersionUID = 2012122804331L; // for Serializable interface

  /** The Raster Display */
  private MidasDisplay rasterDisplay = new MidasDisplay();
  /** The Line Display */
  private MidasDisplay lineDisplay = new MidasDisplay();
  /** Raster Bean for the Raster Display */
  public transient MidasBean rasterBean = null;
  /** Line Plot Bean for the Line Display*/
  public transient MidasBean lineplotBean = null;
  /** Split Pane to be added to the Parent Panel */
  private JSplitPane jSplitPane1 = null;

  /** Mute button for live stream audio */
  private JButton jMute = new JButton("MUTE");
  /** The state of whether or not live stream audio is muted */
  private boolean muted = false;
  /** The background color for the mute button */
  private Color jmcolor = null;

  /** PsdPanelLive Constructor
   *  @param M     the Midas context
   *  @param mq    the Midas message queue
   *  @param mr    the Midas results object
   *  @param bg    the Panel background color
   *  @param fg    the Panel foreground color
   *  @param name  the Panel name
   *  @param tabID the Panel tab ID ???
   */
  public PsdPanelLive (Midas M, MessageQueue mq, Results mr, Color bg, Color fg, String name, int tabID) {
    super(M, mq, mr, bg, fg, name, tabID);
    setup();
  }

  private void setup () {
    try {  buildGUI(); }
    catch (Exception e) { e.printStackTrace(); }
    createBeans();
  }

  private void buildGUI () throws Exception {
    SwingUtilities.invokeAndWait(new Runnable() { public void run() {
      setBackground(bg);
      setForeground(fg);
      setLayout(new BorderLayout());
      final JPanel panelSub = new JPanel();
      panelSub.setLayout(new BoxLayout(panelSub, BoxLayout.Y_AXIS));
      panelSub.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
      panelSub.add(buildPlayStop());
      add(panelSub, BorderLayout.NORTH);
      jSplitPane1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, lineDisplay, rasterDisplay );
      add(jSplitPane1, BorderLayout.CENTER);
      addComponentListener(new ComponentAdapter() { public void componentShown(final Component e)
        { jSplitPane1.setDividerLocation(0.50);}});
      // DBUFFER|BSTORE

      jSplitPane1.setVisible(true);
    }});
  }

  private void createBeans () {
    rasterBean = MidasBean.createBean(M, "plot/msgid=main/lps=600 options=+contrast file=_fftaudio axis=BRframe cmap=RAMP readout=SHOW|ONAXIS|T ",
        rasterDisplay, "rtpsd");
    lineplotBean = MidasBean.createBean(M, "plot/msgid=main options=+contrast file=_fftaudio axis=frame type=line cmap=RAMP readout=0x10009 ",
        lineDisplay, "rtpsd2");
  }

  private JPanel buildPlayStop() {
    final JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1,0));
    panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));

    final JButton jg = new JButton("G");
    jg.setForeground(Color.green);
    jg.setMargin(new Insets(0,1,0,1));
    panel.add(jg);

    jMute.setForeground(Color.gray);
    jMute.setFont(new Font("Sans", Font.BOLD, 12));
    jMute.setMargin(new Insets(0,1,0,1));
    jMute.setToolTipText("Mutes the LIVE stream audio");
    panel.add(jMute);
    jMute.addMouseListener(new MouseAdapter() {
      public void MouseClicked (final MouseEvent e) {
        if (!muted) {
          muted = true;
          jMute.setForeground(Color.red);
          jMute.setBackground(Color.LIGHT_GRAY);
          MQ.put("LIVEAUDIO",0,"NONE","MAIN");
        } else {
          muted = false;
          jMute.setForeground(Color.gray);
          jMute.setBackground(jmcolor);
          MQ.put("LIVEAUDIO",1,"NONE","MAIN");
        }
      }
    });
    jmcolor = jMute.getBackground();
    return panel;
  }

  /** Handle some messages here (forwarded from psdpanal.java), in order to control
      the command run by the beans.
      @since NeXtMidas 3.1.0
   */
  @Override
  public int processMessage (Message msg) {
    int status = Command.NOOP;
    if (msg.name.equals("SWAPRASTER")) {
      if (rasterBean == null) return Command.NOOP;
      rasterBean.close();
      rasterBean = MidasBean.createBean(M, "plot/msgid=main/lps=600 options=+contrast file="+msg.data+" axis=BRframe cmap=RAMP readout=SHOW|ONAXIS|T ",
          rasterDisplay, "rtpsd");
      return Command.NORMAL;
    }
    return status;
  }

}