package com.smartgwt.sample.showcase.client.grid.autofit;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.ResultSet;
import com.smartgwt.client.data.events.DataChangedEvent;
import com.smartgwt.client.data.events.DataChangedHandler;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.util.EventHandler;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RowContextClickEvent;
import com.smartgwt.client.widgets.grid.events.RowContextClickHandler;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;
import com.smartgwt.sample.showcase.client.data.WorldXmlDS;

public class AutofitFilterSample extends ShowcasePanel {
    private static final String DESCRIPTION = "<p>Change the filter to show the grid resizing within the constraint of its "
            + "maximum autofit rows.</p><p>Enter a country filter of \"cook island\" to see the grid shrink down to minimum size. "
            + "</p><p>Change the country filter to \"island\" to show the grid at almost maximum size, but not scrolling."
            + "</p><p>Change the country filter to \"land\" to show the grid scrolling because its maximum autofit size (10)"
            + " isn't large enough to display all rows.</p>";

    public static class Factory implements PanelFactory {
        private String id;

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

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

    public Canvas getViewPanel() {

        Canvas canvas = new Canvas();

        final ListGrid countryGrid = new ListGrid();
        countryGrid.setWidth(500);
        countryGrid.setTop(50);

        countryGrid.setAutoFitData(Autofit.VERTICAL);
        countryGrid.setAutoFitMaxRecords(10);
        countryGrid.setDataSource(WorldXmlDS.getInstance());
        countryGrid.setAutoFetchData(true);

        ListGridField nameField = new ListGridField("countryName", "Country");
        ListGridField capitalField = new ListGridField("capital", "Capital");
        ListGridField continentField = new ListGridField("continent", "Continent");
        countryGrid.setFields(nameField, capitalField, continentField);

        addContextMenu(countryGrid);

        canvas.addChild(countryGrid);
        return canvas;
    }

    private void addContextMenu(ListGrid countryGrid) {
        countryGrid.addRowContextClickHandler(new RowContextClickHandler() {

            @Override
            public void onRowContextClick(RowContextClickEvent event) {
                final Record record = event.getRecord();
                final Menu appMenu = new Menu();

                final MenuItem openInWindowItem = new MenuItem("Open in Window");

                openInWindowItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
                    public void onClick(MenuItemClickEvent event) {
                        new DetailWindow(record).show();
                    }
                });

                appMenu.addItem(openInWindowItem);

                appMenu.show();
                appMenu.moveTo(EventHandler.getX(), EventHandler.getY());
                event.cancel();
            }
        });
    }

    public String getIntro() {
        return DESCRIPTION;
    }

    public class DetailWindow extends Window {

        private ResultSet resultSet;

        public DetailWindow(Record record) {

            initializeWindow(record);

            initializeResultSet(record);
        }

        private void initializeWindow(final Record record) {
            setIsModal(false);
            setTitle(record.getAttribute("pk"));
            setHeight(300);
            setWidth(300);

            Button update = new Button("update");

            update.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    WorldXmlDS.getInstance().updateData(record);
                }
            });

            addMember(update);
        }

        private void initializeResultSet(Record record) {
            if (resultSet != null) {
                resultSet.destroy();
            }
            resultSet = new ResultSet(WorldXmlDS.getInstance());
            resultSet.setAllRows(new Record[] { record });
            resultSet.setCriteria(new Criteria("pk", record.getAttribute("pk")));
            resultSet.addDataChangedHandler(new DataChangedHandler() {

                @Override
                public void onDataChanged(DataChangedEvent event) {
                    SC.say("onDataChanged called...");
                }
            });
            resultSet.ensureCreated();
        }
    }

}