/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © 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 demo.ice;

import ilog.views.diagrammer.IlvDiagrammer;
import ilog.views.diagrammer.faces.component.IlvFacesDiagrammerView;
import ilog.views.diagrammer.faces.dhtml.component.IlvFacesDHTMLDiagrammerView;
import ilog.views.sdm.IlvSDMEngine;
import ilog.views.util.IlvResourceUtil;

import java.text.MessageFormat;
import java.util.Timer;
import java.util.TimerTask;

import javax.faces.event.ActionEvent;

import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;

public class PushingBean {

        /**
         * The predefined animation path.
         */
        private String[] steps = { "workflow4", "transition8", "split2",
                        "transition5", "activity9", "transition12", "split3",
                        "transition15", "activity11", "transition17", "join3",
                        "transition19", "join2", "transition28", "activity16",
                        "transition29", "end2", "workflow4", "transition8", "split2",
                        "transition52", "activity6", "transition131", "activity61",
                        "transition13", "join2", "transition28", "activity16",
                        "transition29", "end2" };
        /**
         * The id of the current node.
         */
        private String currentNode = "none!";

        /**
         * @return The id of the current node.
         */
        public String getCurrentNode() {
                return currentNode;
        }

        /**
         * Current step.
         */
        private int step = -1;

        /**
         * Moves to the next step.
         */
        private void nextStep() {
                step++;
                step = step % steps.length;
                currentNode = steps[step];
        }

        /**
         * The connection status.
         */
        private Boolean connected = false;

        /**
         * @return The label of the connect button.
         */
        public String getAction() {

                return connected ? "Disconnect" : "Connect";
        }

        /**
         * @return The verb used to form the connection message.
         */
        public String getVerb() {

                return connected ? "disconnect from" : "connect to";
        }

        /**
         * Called when the connect button is clicked.
         * @param event Not used.
         */
        public void onConnect(ActionEvent event) {
                connected = !connected;
        }

        /**
         * @return The JavaScript code to be sent to the client.
         */
        public String getInstruction() {
                return connected ? "<script id=code\"" + step
                + "\" type=\"text/javascript\">diagram.updateImage();</script>"
                : "";
        }

        /**
         * The diagrammer view.
         */
        private IlvFacesDiagrammerView diagrammerView = new IlvFacesDHTMLDiagrammerView();

        /**
         * The timer used for animation.
         */
        static Timer timer = new Timer();

        /**
         * The constructor.
         */
        public PushingBean() {
                // ((IlvFacesDHTMLDiagrammerView)diagrammerView).setStateURL(null);
                timer.schedule(new UpdateTask(), 2000, 2000);
        }

        /**
         * @return The diagrammer view used in this demo.
         */
        public IlvFacesDiagrammerView getDiagrammerView() {
                return diagrammerView;
        }

        /**
         * Sets the diagrammer view.
         * @param diagrammerView The new diagrammer view.
         */
        public void setDiagrammerView(IlvFacesDiagrammerView diagrammerView) {
                this.diagrammerView = diagrammerView;
        }

        /**
         * ICEfaces faces state used for server side initiated rendering (push).
         */
        private PersistentFacesState iceState = PersistentFacesState.getInstance();
        
        public String getTitleMsg(){
          return IlvResourceUtil.getCurrentLocaleString(PushingBean.class,"titleMsg");
        }
        
        public String getH1Msg(){
          return IlvResourceUtil.getCurrentLocaleString(PushingBean.class,"H1Msg");
        }
        
        public String getConnectToServerMsg(){    
          String msg=IlvResourceUtil.getCurrentLocaleString(PushingBean.class,"connectToServerMsg");
          return  MessageFormat.format(msg,this.getVerb());  
        }
        
        public String getCurrentActivityMsg(){
          String msg=IlvResourceUtil.getCurrentLocaleString(PushingBean.class,"currentActivityMsg");
          return  MessageFormat.format(msg,this.getCurrentNode());
        }
        

        /**
         * The task to be executed by the timer.
         */
        protected class UpdateTask extends TimerTask {
                public void run() {
                        try {
                                if (connected) {
                                        nextStep();
                                        updateDiagrammer();
                                        iceState.render();
                                }
                        } catch (Exception ex) {
                                ex.printStackTrace();
                        }
                }
        }

        /**
         * Updates the diagrammer.
         */
        protected void updateDiagrammer() {
                IlvDiagrammer diagrammer = null;
                try {
                        diagrammer = diagrammerView.getDiagrammer();
                } catch (Exception e1) {
                        e1.printStackTrace();
                }
                if (diagrammer != null) {
                        diagrammer.setAdjusting(true);

                        IlvSDMEngine engine = diagrammer.getEngine();
                        Object activity = engine.getObject(currentNode);
                        if (activity != null) {
                                engine.deselectAllObjects();
                                engine.setSelected(activity, true);
                        }
                        diagrammer.setAdjusting(false);
                }
        }
}