package com.smartgwt.sample.client;

import java.util.ArrayList;
import java.util.Date;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceSequenceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.GroupStartOpen;
import com.smartgwt.client.types.TimeUnit;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.calendar.*;
import com.smartgwt.client.widgets.grid.*;
import com.smartgwt.client.widgets.layout.VLayout;

public class GroupUnfoldingAndScrolling extends VLayout {

  private final Date _calStart = new Date(112, 6, 2);

  private final Date _calEnd = new Date(112, 6, 22);

  private final int year = _calStart.getYear();

  private final int month = _calStart.getMonth();

  private final int start = _calStart.getDate() - _calStart.getDay();

  private Lane[] developers = null;

  private ListGridField[] laneFields = null;

  private Timeline currentTimeline = null;

  private DataSource eventDS = null;

  public GroupUnfoldingAndScrolling() {
    SC.showConsole();

    eventDS = new DataSource();
    eventDS.setID("siemensTestDS");
    DataSourceSequenceField eventIdField = new DataSourceSequenceField("eventId");
    eventIdField.setPrimaryKey(true);

    DataSourceTextField nameField = new DataSourceTextField("name");
    DataSourceTextField descField = new DataSourceTextField("description");
    DataSourceDateField startDateField = new DataSourceDateField("startDate");
    DataSourceDateField endDateField = new DataSourceDateField("endDate");

    eventDS.setFields(eventIdField, nameField, descField, startDateField, endDateField);
    eventDS.setClientOnly(true);
    eventDS.setTestData(getEvents());

    setWidth100();
    setHeight100();

    currentTimeline = getGroupedTimeline();

    this.addMembers(currentTimeline);
  }

  private Timeline getGroupedTimeline() {
    Timeline timeline = new Timeline();
    setupTimeline(timeline);
    timeline.setShowLaneRollOver(true);
    timeline.setCanGroupLanes(true);
    timeline.setLaneGroupByField("groupField1");
    timeline.setLaneGroupStartOpen(GroupStartOpen.ALL);

    return timeline;
  }

  private void setupTimeline(Timeline timeline) {
    timeline.setWidth100();
    timeline.setHeight(300);
    timeline.setStartDate(_calStart);
    timeline.setEndDate(_calEnd);
    timeline.setHideUnusedLanes(true);
    timeline.setDataSource(eventDS);
    timeline.setAutoFetchData(true);
    timeline.setAllowDurationEvents(true);
    timeline.setUseSublanes(true);
    timeline.setCanEditLane(true);
    timeline.setCanEditSublane(true);
    timeline.setLanes(getDevelopers());
    timeline.setCanReorderLanes(true);
    timeline.setLaneFields(getLaneFields());
    timeline.setHeaderLevels(new HeaderLevel[]{
      new HeaderLevel(TimeUnit.WEEK),
      new HeaderLevel(TimeUnit.DAY)
    });
    timeline.setDisableWeekends(false);
    timeline.setCanResizeEvents(true);
    timeline.setShowEventDescriptions(true);
    timeline.setColumnsPerPage(5);
    timeline.setLaneEventPadding(4);
    timeline.setShowZones(true);
    timeline.setZones(
            new CalendarEvent(99, "zone1", "", new Date(2012 - 1900, 6, 5), new Date(2012 - 1900, 6, 11)));
    timeline.setZoneStyleName("customZoneStyle");
    timeline.setShowIndicators(true);
    CalendarEvent indicator = new CalendarEvent(199, "indicator1", "", new Date(112, 6, 7, 12, 0), null);
    timeline.setIndicators(indicator);
  }

  private Lane[] getDevelopers() {
    developers = new Lane[]{
      getLane("shelleyFewel", "Shelley Fewel", 60, "Group 3", "a"),
      getLane("kaiKong", "Kai Kong", 40, "Group 2", "a"),
      getLane("charlesMadigen", "Charles Madigen", 60, "Group 1", "b"),
      getLane("tamaraKane", "Tamara Kane", 60, "Group 3", "a"),
      getLane("darcyFeeney", "Darcy Feeney", 60, "Group 1", "a"),
      getLane("garretMonroe", "Garret Monroe", 60, "Group 2", "b")
    };

    return developers;
  }

  private Lane getLane(String name, String title, Integer height, String groupField1, String groupField2) {
    ArrayList<Lane> sublanes = new ArrayList<Lane>();
    Lane sublane = new Lane("planned", "Planned");
    sublane.setEventStyleName("testStyle1");
    sublanes.add(sublane);
    sublane = new Lane("actual", "Actual");
    sublane.setEventStyleName("testStyle2");
    sublanes.add(sublane);

    if (name.equals("shelleyFewel")) {
      Lane extraLane = new Lane("additional", "Additional");
      extraLane.setEventStyleName("testStyle3");
      extraLane.setHeight(30);
      sublanes.add(extraLane);
    }

    //Lane[] laneArray = sublanes..toArray();
    Lane lane = new Lane(name, title);
    lane.setHeight(height);
    lane.setSublanes(sublanes.toArray(new Lane[sublanes.size()]));
    lane.setAttribute("groupField1", groupField1);
    lane.setAttribute("groupField2", groupField2);

    return lane;
  }

  private ListGridField[] getLaneFields() {
    laneFields = new ListGridField[]{
      getLaneField("title", "Title", 120, false),
      // these two are hidden because they're for grouping only
      getLaneField("groupField1", "", 120, true),
      getLaneField("groupField1", "", 120, true)
    };

    return laneFields;
  }

  private ListGridField getLaneField(String name, String title, Integer width, Boolean hidden) {
    ListGridField field = new ListGridField(name, title, width);
    field.setHidden(hidden);
    if (hidden) {
      field.setShowIfCondition(new ListGridFieldIfFunction() {
        @Override
        public boolean execute(ListGrid grid, ListGridField field, int fieldNum) {
          // TODO Auto-generated method stub
          return false;
        }
      });
    }
    return field;
  }

  private CalendarEvent[] getEvents() {

    return new CalendarEvent[]{
      getEvent(13, "TestItem13", "Test Item 13", start + 4, start + 6, null, null, "kaiKong", "planned", null),
      getEvent(14, "TestItem14", "Test Item 14", start + 4, start + 7, null, null, "kaiKong", "actual", null),
      getEvent(23, "TestItem23", "Test Item 23", start + 4, start + 6, null, null, "charlesMadigen", "planned", null),
      getEvent(24, "TestItem24", "Test Item 24", start + 4, start + 7, null, null, "charlesMadigen", "actual", null),
      getEvent(33, "TestItem33", "Test Item 33", start + 4, start + 6, null, null, "tamaraKane", "planned", null),
      getEvent(34, "TestItem34", "Test Item 34", start + 4, start + 7, null, null, "tamaraKane", "actual", null),
      getEvent(43, "TestItem13", "Test Item 43", start + 4, start + 6, null, null, "darcyFeeney", "planned", null),
      getEvent(44, "TestItem14", "Test Item 44", start + 4, start + 7, null, null, "darcyFeeney", "actual", null),
      getEvent(53, "TestItem53", "Test Item 53", start + 4, start + 6, null, null, "garretMonroe", "planned", null),
      getEvent(54, "TestItem54", "Test Item 54", start + 4, start + 7, null, null, "garretMonroe", "actual", null),};
  }

  private CalendarEvent getEvent(Integer id, String name, String title, Integer start,
          Integer end,
          Integer duration,
          TimeUnit durationUnit,
          String lane, String sublane, String styleName) {
    CalendarEvent event = new CalendarEvent(id, name, title, new Date(year, month, start), null, lane);
    event.setSublane(sublane);
    if (duration != null) {
      if (duration == 0) {
        event.setCanResize(false);
      }
      event.setDuration(duration);
      event.setDurationUnit(durationUnit);
    } else {
      event.setEndDate(new Date(year, month, end));
    }
    event.setStyleName(styleName);
    return event;
  }
}
