/*
 * 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 ganttviewer.views;

import ganttviewer.util.interactors.MonthPanelInteractor;
import ganttviewer.viewer.View;
import ganttviewer.viewer.Viewer;
import ilog.views.gantt.IlvActivity;
import ilog.views.gantt.IlvGanttModel;
import ilog.views.gantt.event.SelectionEvent;
import ilog.views.gantt.event.SelectionListener;
import ilog.views.gantt.swing.calendarview.IlvDefaultActivityCalendarRenderer;
import ilog.views.gantt.swing.calendarview.IlvMonthView;
import ilog.views.util.styling.IlvStylingException;

import java.awt.Color;

import javax.swing.JComponent;

/**
 * <code>MonthlyCalendarView</code> is the Calendar view.
 */
public class MonthlyCalendarView extends View implements SelectionListener {

  /**
   * The activity renderer.
   */
  private IlvDefaultActivityCalendarRenderer activityRenderer;

  /**
   * Builds a <code>MonthlyCalendarView</code>.
   * @param viewer The viewer.
   * @param iD The ID of the view.
   * @param name The name of the view.
   */
  public MonthlyCalendarView(Viewer viewer, String iD, String name) {
    super(viewer, iD, name);
    // set the calendar.
    setMainComponent(new IlvMonthView());
  }

  /**
   * Customizes the calendar before it is added.
   * @param component The calendar to be added.
   * @return The calendar.
   */
  Override
  protected JComponent mainComponentAboutToBeAdded(JComponent component) {
    IlvMonthView calendar = (IlvMonthView) component;
    // listen to selection changes.
    calendar.addSelectionListener(this);
    calendar
        .setActivityRenderer(activityRenderer = new IlvDefaultActivityCalendarRenderer());
    // add move interactor
    new MonthPanelInteractor().connect(calendar);
    return component;
  }

  /**
   * Attaches the Gantt data model to the view.
   * @param model The Gantt data model.
   */
  Override
  protected void attachModel(Object model) {
    getMonthView().setGanttModel((IlvGanttModel) model);
    if (model == null) {
      return;
    }
    // to update the start of the data model
    if (((IlvGanttModel) model).getRootActivity() != null) {
      getMonthView().setDate(
          ((IlvGanttModel) model).getRootActivity().getStartTime());
    }
  }

  /**
   * returns the calendar.
   * @return The calendar.
   */
  public IlvMonthView getMonthView() {
    return (IlvMonthView) getMainComponent();
  }

  /**
   * Indicates whether this view is stylable or not.
   * @return <code>true</code>.
   */
  Override
  public boolean isStylable() {
    return true;
  }

  /**
   * Applies the style to this view.
   * @param color The color.
   */
  Override
  public void applyStyle(String color) throws IlvStylingException {
    String[] rgb = color.split(",");
    int red = Integer.parseInt(rgb[0].trim());
    int green = Integer.parseInt(rgb[1].trim());
    int blue = Integer.parseInt(rgb[2].trim());
    activityRenderer.setFillPaint(new Color(red, green, blue));
  }

  /**
   * Activity selection implementation.
   * @param obj The object to (de)select.
   * @param selected The selection status.
   */
  Override
  protected void setSelectedImpl(Object obj, boolean selected) {
    if (obj instanceof IlvActivity) {
      getMonthView().setSelected((IlvActivity) obj, selected);
    }
  }

  /**
   * Deselects all.
   */
  Override
  protected void deSelectAllImpl() {
    getMonthView().deselectAllActivities();
  }

  /**
   * Listens for selection changes.
   * @param event The selection event.
   */
  Override
  public void selectionChanged(SelectionEvent event) {
    super.selectionChanged(event.getSource(), event.isObjectSelected());
  }

}