package com.smartgwt.sample.showcase.client.tiling;

import java.util.Random;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.FlowLayout;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;

public class BasicTilingSample extends ShowcasePanel {

    private static final String DESCRIPTION = "Smart GWT can display data in a \"tiled\" view. Mouse over widgets to see rollovers, click to select (shift- and ctrl-click for multi-select).";

    public static class Factory implements PanelFactory {

        private String id;

        public Canvas create() {
            BasicTilingSample panel = new BasicTilingSample();
            id = panel.getID();
            return panel;
        }

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

    private Random rnd;
    
    public Canvas getViewPanel() {
        rnd = new Random();
        HLayout container = new HLayout();
        container.setAutoHeight();
        
        final FlowLayout flowLayout = new FlowLayout();
        flowLayout.setBackgroundColor("#f99");
        flowLayout.setWidth("*");
        flowLayout.setOverflow(Overflow.VISIBLE);
        
        flowLayout.setTiles(getRandomTiles());
        
        HLayout controlsLayout = new HLayout(5);
        controlsLayout.setBackgroundColor("#99f");
        controlsLayout.setAutoWidth();
        controlsLayout.setAutoHeight();
        
        Button addTileButton = new Button("Add tile");
        addTileButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                flowLayout.addTile(getTile());
            }
        });
        
        Button removeTileButton = new Button("Remove tile");
        removeTileButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                Canvas tile = flowLayout.getTile(0);
                int tilesCount = flowLayout.getChildren().length;
                if (tile != null) {
                    flowLayout.removeTile(tilesCount - 1);
                }
            }
        });
     
        controlsLayout.addMember(addTileButton);
        controlsLayout.addMember(removeTileButton);
        container.setMembers(flowLayout, controlsLayout);
        return container;
    }

    private Canvas[] getRandomTiles() {
        Canvas[] canvases = new Canvas[rnd.nextInt(20)+10];
        for (int i = 0; i < canvases.length; i++) {
            Canvas c = getTile();
            canvases[i] = c;
        }
        return canvases;
    }

    private Canvas getTile() {
        Canvas c = new Canvas();
        c.setBackgroundColor("#9f9");
        c.setWidth(rnd.nextInt(80)+20);
        return c;
    }

    public String getIntro() {
        return DESCRIPTION;
    }

}