package nxm.sys.libg;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import nxm.sys.inc.InternalUseOnly;
import nxm.sys.lib.Shell;
import nxm.sys.lib.Table;

/** Creates an HTML formatted popup based on the supplied Map (Key/Value pairs)
 *
 *  @author Jeanette McCall / Kevin Esterheld
 *  @since NeXtMidas 3.9.3
 */
@InternalUseOnly
public class PopUpHTML {

   String label;
   private JFrame frame;
   private static Dimension DEFAULT_MIN_SIZE = new Dimension(250,200);
   private static Dimension DEFAULT_MAX_SIZE = new Dimension(600,600);

   /** Constructs an HTML formatted popup based to the KEY Value pairs in the Map
    *
    *   @param objInfo information about the object described as key value pairs
    *   @param MP      the MPlot on which to render the popup
    */
   public PopUpHTML (Table objInfo, MPlot MP) {
     this(objInfo, MP, MP.MW.theme.cwfg, DEFAULT_MIN_SIZE, DEFAULT_MAX_SIZE);
   }

   /** Constructs an HTML formatted popup based to the KEY Value pairs in the Map
    *
    *  @param objInfo    information about the object described as key value pairs
    *  @param MP         the MPlot on which to render the popup
    *  @param valueColor the font color for the value
    *  @param minSize    minimum popup size
    *  @param maxSize    maximum popup size
    */
   public PopUpHTML (Table objInfo, MPlot MP, Color valueColor, Dimension minSize, Dimension maxSize) {
     Object name = objInfo.get("NAME");
     this.label = (name == null) ? "" : String.valueOf(objInfo.get("NAME"));
     frame      = new JFrame(label);
     Point mousePoint = MP.panel.getLocationOnScreen();
     int xPos = mousePoint.x;
     int yPos = mousePoint.y;
     frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
     PopupPanel myPopup = new PopupPanel(objInfo, valueColor);

     // keep the popup size between 250 - 600 pixels
     int w = Math.min(maxSize.width, Math.max(minSize.width, myPopup.getPreferredSize().width));
     int h = Math.min(maxSize.height, Math.max(minSize.height, myPopup.getPreferredSize().height));
     frame.setSize(w,h);

     frame.setLocation(xPos, yPos);  // where to popup on the screen
     frame.setContentPane(myPopup);
     frame.setVisible(true);
   }

   /** close the popup */
   public void close () {
     frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
     if (Shell.needRemoveNotifyToAvoidJVMHang()) {
       frame.removeNotify();
     }
   }

   /** A class to popup a panel on the screen with information about an object
    *  @since 3.9.3
    */
   private class PopupPanel extends JPanel {

     private static final long serialVersionUID = 1L;
     private JScrollPane   scrollPane;        // The scrollpane used to display the text

     public PopupPanel(Table objInfo, Color valueColor) {
       setLayout(new GridLayout(1,1));
       String labelText = setupText(objInfo, valueColor);
       display(labelText);
     }

     /** Format the text to be displayed */
     private String setupText (Table objInfo, Color fontColor) {
       String fontColorStr = Integer.toHexString(fontColor.getRGB()).substring(2);

       String displayText = "<html>";
       displayText += Table.toHTMLTable(objInfo, fontColorStr);
       displayText += "</html>";

       return  displayText;
     }

     /**
      *  Display the provided text in a popup
      *  @param labelText The text to display
      */
     private void display (String labelText) {
       JLabel textDisplay = new JLabel(labelText, JLabel.CENTER);
       textDisplay.setHorizontalTextPosition(JLabel.CENTER);
       textDisplay.setVerticalTextPosition(JLabel.BOTTOM);
       scrollPane = new JScrollPane(textDisplay);
       add(scrollPane);

       setVisible(true);
     }
   } // end class PopupPanel
} // end class PopUpHTML

