/*
 * 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 demo.framework;

import ilog.views.IlvGraphic;
import ilog.views.IlvGraphicEnumeration;
import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.IlvPoint;
import ilog.views.IlvRect;
import ilog.views.IlvTransformer;
import ilog.views.faces.dhtml.component.IlvFacesDHTMLView;
import ilog.views.faces.dhtml.event.FacesViewActionEvent;
import ilog.views.faces.dhtml.interactor.IlvFacesObjectSelectedFinder;
import ilog.views.faces.dhtml.interactor.IlvFacesObjectSelectedRectFinder;
import ilog.views.faces.internalutil.IlvResourceUtil;
import ilog.views.io.IlvReadFileException;
import ilog.views.servlet.IlvImageMapAreaGenerator;
import ilog.views.util.servlet.IlvMenuFactory;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 * The bean that provides the logic of the JViews Framework Faces sample.
 */
public class FrameworkBean {

  private IlvManagerView managerView;

  private String currentURL;

  private List<SelectItem> ivlSelectItems;

  private IlvFacesDHTMLView facesView = new IlvFacesDHTMLView();

  private Object comboValue;

  private IlvImageMapAreaGenerator imapGenerator = new IlvImageMapAreaGenerator() {

    Override
    public int getShape(IlvManagerView view, IlvGraphic obj, IlvTransformer t) {
      String msg=ilog.views.util.IlvResourceUtil.getServerLocaleString(FrameworkBean.class,"CustomImapGeneratorOn");      
      Logger.getLogger("demo.framework").log(Level.FINEST, MessageFormat.format(msg,obj));
      return super.getShape(view, obj, t);
    }

    Override
    public String generateONMOUSEOVER(IlvManagerView view, IlvGraphic obj) {
      String msg=ilog.views.util.IlvResourceUtil.getCurrentLocaleString(FrameworkBean.class,"onImageMap");
      return "alert('"+msg+"')";
    }

  };

  private IlvFacesObjectSelectedFinder finder = new IlvFacesObjectSelectedFinder() {
    Override
    public Object computeObjectSelected(IlvManagerView managerView, IlvPoint pointClicked,
        IlvTransformer transformer) {
      String msg=ilog.views.util.IlvResourceUtil.getServerLocaleString(FrameworkBean.class,"CustomObjectSelectedFinderCalled");      
      Logger.getLogger("demo.framework").log(Level.FINEST, msg);
      return super.computeObjectSelected(managerView, pointClicked, transformer);
    }
  };

  private IlvFacesObjectSelectedRectFinder finderRect = new IlvFacesObjectSelectedRectFinder() {
    Override
    public ArrayList<?> computeObjectSelected(IlvManagerView managerView, IlvRect rect,
        IlvTransformer transformer) {
      String msg=ilog.views.util.IlvResourceUtil.getServerLocaleString(FrameworkBean.class,"CustomObjectSelectedRectFinderCalled");
      Logger.getLogger("demo.framework").log(Level.FINEST,msg);
      return super.computeObjectSelected(managerView, rect, transformer);
    }
  };

  private IlvMenuFactory factory = new FrameworkMenuFactory();

  public FrameworkBean() {
    initIvlSelectItems();
    initManagerView();
  }

  /**
   * Initializes the select items list, and sets the current ivl file.
   */
  protected void initIvlSelectItems() {
    String[] names = { "jviews", "namap", "classgraph", "demo2d" };
    String[] files = { "jviews", "namap", "classgraph", "demo2d" };

    ivlSelectItems = new ArrayList<SelectItem>();
    for (int i = 0; i < names.length; i++) {
      ivlSelectItems.add(new SelectItem("/data/" + files[i] + ".ivl", names[i]));
    }
    currentURL = (String) ivlSelectItems.get(0).getValue();
  }

  /**
   * Initializes the manager view with the current ivl file.
   */
  protected void initManagerView() {
    if (managerView == null)
      managerView = new IlvManagerView();
    URL ivlURL;
    try {
      ivlURL = new URL(IlvResourceUtil.getAbsoluteURL(currentURL));
      IlvManager manager = managerView.getManager();
      manager.setFileName(ivlURL);
      managerView.fitTransformerToContent();
      managerView.setKeepingAspectRatio(true);
      IlvGraphicEnumeration objs = managerView.getManager().getObjects();
      int id = 0;
      while(objs.hasMoreElements()){
        IlvGraphic g = objs.nextElement();
        id++;
        g.setToolTipText("obj "+id);
      }
      //Reset the state of the view.
      facesView.setStateURL(null);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (IlvReadFileException e) {
      e.printStackTrace();
    }
  }

  /**
   * Changes the current ivl displayed in the view.
   * @param event The value change event.
   */
  public void changeIvlFile(ValueChangeEvent event) {
    String newFile = (String) event.getNewValue();
    if (newFile != null && !currentURL.equals(newFile)) {
      currentURL = newFile;
      initManagerView();
    }
  }

  /**
   * The action of selecting a graphic object.
   * @param event The value change event.
   */
  public void selectObject(ValueChangeEvent event) {
    Object value = event.getNewValue();
    if (value != null && value instanceof IlvGraphic) 
      selectObject(managerView.getManager(), (IlvGraphic) value);       
  }

  /**
   * The action of selecting a set of graphic objects.
   * @param event The value change event.
   */
  public void selectRectObject(ValueChangeEvent event) {
    Object value = event.getNewValue();
    if (value != null && value instanceof List) {
      managerView.getManager().deSelectAll(false);
      List<?> list = (List) value;
      for (int i = 0; i < list.size(); i++) {
        Object obj = list.get(i);
        if (obj instanceof IlvGraphic) {
          IlvGraphic g = (IlvGraphic) obj;
          managerView.getManager().setSelected(g, true, false);
        }
      }
    }
  }
  
  public void selectObject(IlvManager manager, IlvGraphic g) {
    manager.deSelectAll(false);
    manager.setSelected(g, true, false);
  }

  public void action(FacesViewActionEvent event) {
    IlvManagerView view = (IlvManagerView) event.getGraphicComponent();
    Object value = event.getObject();
    if (value != null && value instanceof IlvGraphic)
      selectObject(view.getManager(), (IlvGraphic) value);   
  }
  
  //Getters/Setters -----------------------------------------

  public IlvManagerView getManagerView() {
    return managerView;
  }

  public void setManagerView(IlvManagerView managerView) {
    this.managerView = managerView;
  }

  public List<SelectItem> getIvlSelectItems() {
    return ivlSelectItems;
  }

  public void setIvlSelectItems(List<SelectItem> ivlSelectItems) {
    this.ivlSelectItems = ivlSelectItems;
  }

  public IlvFacesDHTMLView getFacesView() {
    return facesView;
  }

  public void setFacesView(IlvFacesDHTMLView facesView) {
    this.facesView = facesView;
  }

  public Object getComboValue() {
    return comboValue;
  }

  public void setComboValue(Object comboValue) {
    this.comboValue = comboValue;
  }

  public IlvImageMapAreaGenerator getImapGenerator() {
    return imapGenerator;
  }

  public void setImapGenerator(IlvImageMapAreaGenerator imapGenerator) {
    this.imapGenerator = imapGenerator;
  }

  public IlvFacesObjectSelectedFinder getFinder() {
    return finder;
  }

  public void setFinder(IlvFacesObjectSelectedFinder finder) {
    this.finder = finder;
  }

  public IlvFacesObjectSelectedRectFinder getFinderRect() {
    return finderRect;
  }

  public void setFinderRect(IlvFacesObjectSelectedRectFinder finderRect) {
    this.finderRect = finderRect;
  }

  public IlvMenuFactory getFactory() {
    return factory;
  }

  public void setFactory(IlvMenuFactory factory) {
    this.factory = factory;
  }



}