/*
 * 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.
 */
package action;

import java.util.EventObject;

import ilog.cpl.model.IlpAttribute;
import ilog.cpl.model.IlpRepresentationObject;
import ilog.tgo.model.IltAlarm;
import ilog.tgo.model.IltObjectState;
import ilog.views.faces.dhtml.IlvDHTMLConstants;
import ilog.views.faces.dhtml.event.FacesMenuActionEvent;
import ilog.views.faces.dhtml.event.FacesViewActionListener;
import ilog.views.util.servlet.model.IlvMenuItem;

/**
 * This is ActionListener that handles the alarms management of the currently
 * selected object.
 */
public class AlarmStateHandler extends FacesViewActionListener {

  private static final String ADD = "add";
  private static final String REMOVE = "remove";
  private static final String REMOVE_ALL = "remove_all";

  private static final String MINOR = "minor";
  private static final String MAJOR = "major";
  private static final String WARNING = "warning";

  /**
   * Id of Add Major alarm menu item.
   */
  public static final String ADD_MAJOR_ID = ADD + "-" + MAJOR;
  /**
   * Id of Add Minor alarm menu item.
   */
  public static final String ADD_MINOR_ID = ADD + "-" + MINOR;
  /**
   * Id of Add Warning alarm menu item.
   */
  public static final String ADD_WARNING_ID = ADD + "-" + WARNING;

  /**
   * Id of Remove Major alarm menu item.
   */
  public static final String REMOVE_MAJOR_ID = REMOVE + "-" + MAJOR;
  /**
   * Id of Remove Minor alarm menu item.
   */
  public static final String REMOVE_MINOR_ID = REMOVE + "-" + MINOR;
  /**
   * Id of Remove Warning alarm menu item.
   */
  public static final String REMOVE_WARNING_ID = REMOVE + "-" + WARNING;

  /**
   * Id of Remove All alarm menu item.
   */
  public static final String REMOVE_ALL_ID = REMOVE_ALL;

  /**
   * Constructor. It uses the <code>IMAGE_SERVLET_CONTEXT</code> as no other JSF
   * component has to be updated.
   */
  public AlarmStateHandler() {
    super(IlvDHTMLConstants.JSF_CONTEXT);
  }

  /**
   * Adds or removes alarms from the object in the EventObject depending on the
   * ID (as defined in this class) of the potential IlvMenuItem carried by the
   * EventObject.
   *
   * @param event
   *          The event object.
   */
  Override
  public void actionPerformed(EventObject event) throws Exception {

    // event is instance of ServletActionEvent
    FacesMenuActionEvent facesActionEvent = (FacesMenuActionEvent) event;

    // get active object
    IlpRepresentationObject obj = (IlpRepresentationObject) facesActionEvent.getObject();

    // get the menu item
    IlvMenuItem menuItem = facesActionEvent.getSelectedMenuItem();

    String actionId = menuItem.getId();

    if (actionId != null) {

      // get object state from 'objectState' attribute
      IlpAttribute attrib = obj.getAttributeGroup().getAttribute("objectState");
      IltObjectState oState = (IltObjectState) obj.getAttributeValue(attrib);

      if (null != oState) {

        IltAlarm.State alarmState = ((IltAlarm.State) oState.getAlarmState());

        // If we need to remove all, then just do it
        if (AlarmStateHandler.REMOVE_ALL_ID.equals(actionId)) {
          alarmState.removeAllAlarms();
          return;
        } else {

          IltAlarm.Severity severity = getSeverityForId(actionId);

          // Either we add it
          if (actionId.indexOf(AlarmStateHandler.ADD) != -1) {
            alarmState.addNewAlarm(severity);
          }
          // Or remove it
          else if (actionId.indexOf(AlarmStateHandler.REMOVE) != -1) {
            alarmState.removeNewAlarm(severity);
          }

          // Nothing else
        }
      }
    }
  }

  /**
   * Return the IltAlarm.Severity instance for the provided action ID as defined
   * in this class.
   */
  private IltAlarm.Severity getSeverityForId(String id) {

    if (id.indexOf(AlarmStateHandler.MAJOR) != -1) {
      return IltAlarm.Severity.Major;
    } else if (id.indexOf(AlarmStateHandler.MINOR) != -1) {
      return IltAlarm.Severity.Minor;
    } else if (id.indexOf(AlarmStateHandler.WARNING) != -1) {
      return IltAlarm.Severity.Warning;
    }
    return IltAlarm.Severity.Unknown;
  }

}