/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

import ilog.views.dashboard.IlvDashboardDiagram;
import ilog.views.sdm.event.SDMPropertyChangeEvent;
import ilog.views.sdm.event.SDMPropertyChangeListener;
import ilog.views.sdm.model.IlvDefaultSDMNode;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



/**
 * The monitoring view displays a frame containing two dashboards in order to monitor
 * traffic statistics and tunnel states and events.
 */
public class MonitoringView extends JFrame{
    
    // the name of the tunnel that is monitored in this view
    private String currtentTunnelName = null;
    
    // the dashboards used to display monitoring data about traffic and tunnel states and events
    private IlvDashboardDiagram trafficDashboard;
    private IlvDashboardDiagram monitoringDashboard;


    /**
     * Creates the views with specified name and dashboards
     */
    public MonitoringView(String tunnelName,IlvDashboardDiagram monitoringDashboard,IlvDashboardDiagram trafficDashboard) {
        super();        
        currtentTunnelName = tunnelName;
        this.trafficDashboard = trafficDashboard;
        this.monitoringDashboard = monitoringDashboard;       
        initDashboards();
        createGUI();
    }

    
    /**
     * Creates the GUI of the view
     */
    private void createGUI() {
        // put the dashboards into a panel
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(monitoringDashboard,BorderLayout.CENTER);
        panel.add(trafficDashboard,BorderLayout.PAGE_END);
        
        // initialise the frame of the view
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel,BorderLayout.CENTER);
        setTitle(currtentTunnelName);    
        setResizable(true);
        pack();
        setVisible(true);
    }

    /**
     * Initialise dashboards: size, scrollable.. and prepare for simulation 
     */
    private void initDashboards() {
        // GUI init
        monitoringDashboard.setPreferredSize(new Dimension(822,520));
        monitoringDashboard.setScrollable(true);        
        trafficDashboard.setScrollable(false);
        trafficDashboard.setPreferredSize( new Dimension(822, 242));
        
        // add a property listener on the monitoring dashboard in order to
        // detect clicks on symbols. This illustrates how to manage user interaction
        // on symbols
        SDMPropertyChangeListener lstnr=  new SDMPropertyChangeListener(){
            Override
            public void propertyChanged(SDMPropertyChangeEvent arg0) {
                // get data about the event
                IlvDefaultSDMNode object = (IlvDefaultSDMNode)arg0.getObject();
//                String property = arg0.getPropertyName();
//                Object oldValue = arg0.getOldValue();
                Object newValue = arg0.getNewValue();

                String ID = (String)object.getID();
                
                // if user clicks a button representing a fan do something
                if(newValue.equals("BUTTON_CLICKED")){
                    if(ID.startsWith("Fan")){
                        JOptionPane.showMessageDialog(monitoringDashboard, ID+"\nThis equipment is fully functionnal");
                    }
                    else if(ID.startsWith("SafetySystem")){
                        JOptionPane.showMessageDialog(monitoringDashboard, "This is a safety system");
                    }
                }
            }
        };
        
        //add the listener
        monitoringDashboard.getEngine().getModel().addSDMPropertyChangeListener(lstnr);      

        
        // create a simulator and the supervisors for each dashboards. Supervisors will
        // request values to the simulator
        Simulator simulator = new Simulator();
        new Supervisor("Monitoring",monitoringDashboard,1000,simulator);
        new Supervisor("Traffic",trafficDashboard,500,simulator);
    }


}