package com.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.DragAppearance;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.VStack;

public class Demo implements EntryPoint
{

  private Canvas viewPort;

  public static final String DRAG_TYPE = "dragType";

  public static final String DRAG_TYPE_SUBSECTION = "dragTypeSubsection";

  private VLayout mainLayout;

  private VLayout stackLayout;

  private SectionStackSection[] sections;

  private static final int SECTION_AMOUNT = 3;
  private static final int COLUMNS_AMOUNT = 3;
  private static final int SUB_SECTION_AMOUNT = 3;

  @Override
  public void onModuleLoad()
  {

    final Canvas dropLineProperties = new Canvas();
    dropLineProperties.setHeight(4);
    dropLineProperties.setBackgroundColor("aqua");

    final SectionStack sectionStack = new SectionStack();

    sectionStack.setWidth100();
    sectionStack.setHeight100();
    sectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
    sectionStack.setCanReorderSections(true);
    sectionStack.setCanResizeSections(false);
    sectionStack.setOverflow(Overflow.VISIBLE);
    sectionStack.setDropLineProperties(dropLineProperties);
    sectionStack.setDropTypes(DRAG_TYPE);

    viewPort = new Canvas();
    viewPort.setOverflow(Overflow.SCROLL);
    viewPort.addChild(sectionStack);

    stackLayout = new VLayout();
    stackLayout.setWidth100();
    stackLayout.setHeight100();
    stackLayout.addMember(viewPort);

    mainLayout = new VLayout();
    mainLayout.setWidth100();
    mainLayout.setHeight100();
    mainLayout.setDisabled(false);
    mainLayout.addMember(stackLayout);

    sections = new SectionStackSection[SECTION_AMOUNT];
    for (int i = 0; i < SECTION_AMOUNT; i++) {
      final String red = "" + (i + 1) * 3;
      sections[i] = new SectionStackSection("Section " + i);

      sectionStack.addSection(sections[i], i);

      final HLayout columns = new HLayout();
      for (int columnIdx = 0; columnIdx < COLUMNS_AMOUNT; columnIdx++) {
        final String green = "" + (columnIdx + 1) * 3;
        final VStack column = new VStack();

        for (int subSectionIdx = 0; subSectionIdx < SUB_SECTION_AMOUNT; subSectionIdx++) {
          final String blue = "" + (subSectionIdx + 1) * 3;
          final VLayout subSection = new VLayout();
          final Layout content = new Layout();
          content.setOverflow(Overflow.HIDDEN);
          final String memberColor = "#" + red + green + blue;
          final Canvas member = new Canvas();
          member.setBackgroundColor(memberColor);
          member.setSize("90px", "100px");
          member.addChild(new Label("Canvas " + i + "/" + columnIdx + "/" + subSectionIdx));
          final String contentColor = "#" + red + green + 0 + 0;
          content.setBackgroundColor(contentColor);
          content.addMember(member);
          subSection.setDragType(DRAG_TYPE_SUBSECTION);
          subSection.setCanDragReposition(true);
          subSection.setDragAppearance(DragAppearance.TARGET);
          subSection.setCanDrop(true);
          subSection.setShowShadow(true);
          subSection.setShadowSoftness(4);
          subSection.setShadowOffset(1);

          subSection.addMember(content);
          column.setWidth(100);
          column.setHeight100();
          column.setDropTypes(DRAG_TYPE_SUBSECTION);
          final Canvas dropLinePropertiesColumn = new Canvas();
          dropLinePropertiesColumn.setBackgroundColor("aqua");
          column.setDropLineProperties(dropLinePropertiesColumn);
          column.setDropLineThickness(4);
          final Canvas placeHolderProperties = new Canvas();
          placeHolderProperties.setBorder("2px solid #8289A6");
          column.setPlaceHolderProperties(placeHolderProperties);
          column.setShowDragPlaceHolder(true);

          column.setCanAcceptDrop(true);
          column.setLayoutTopMargin(5);

          column.addMember(subSection, subSectionIdx);
        }
        columns.setWidth100();
        columns.setHeight100();
        columns.addMember(column);
      }
      sections[i].addItem(columns);
    }

    mainLayout.draw();
  }
}
