package com.smartgwt.sample.showcase.client.basics.interaction;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.VisibilityChangedEvent;
import com.smartgwt.client.widgets.events.VisibilityChangedHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.IPickTreeItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.menu.*;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeNode;
import com.smartgwt.sample.showcase.client.PanelFactory;
import com.smartgwt.sample.showcase.client.ShowcasePanel;


public class ContextMenuSample extends ShowcasePanel {
    private static final String DESCRIPTION = "Right click (or option-click on Macs) on the image to access a context menu. You can also click on the \"Widget\" button to access the identical menu.";

    public static class Factory implements PanelFactory {
        private String id;

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

        public String getID() {
            return id;
        }

        public String getDescription() {
            return DESCRIPTION;
        }
    }

    public Canvas getViewPanel() {
        Canvas canvas = new Canvas();

        final Img widget = new Img("crystal/128/apps/xmms.png", 100, 100);
        widget.setLeft(200);
        widget.setTop(75);
        canvas.addChild(widget);
        
        Menu sizeMenu = new Menu();
        sizeMenu.setWidth(150);

        MenuItem smallItem = new MenuItem("Small");
        smallItem.setCheckIfCondition(new MenuItemIfFunction() {
            public boolean execute(Canvas target, Menu menu, MenuItem item) {
                return widget.getWidth() == 50;
            }
        });
        smallItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                widget.animateResize(50, 50);
            }
        });

        MenuItem mediumItem = new MenuItem("Medium");
        mediumItem.setCheckIfCondition(new MenuItemIfFunction() {
            public boolean execute(Canvas target, Menu menu, MenuItem item) {
                return widget.getWidth() == 100;
            }
        });
        mediumItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                widget.animateResize(100, 100);
            }
        });

        MenuItem largeItem = new MenuItem("Large");
        largeItem.setCheckIfCondition(new MenuItemIfFunction() {
            public boolean execute(Canvas target, Menu menu, MenuItem item) {
                return widget.getWidth() == 200;
            }
        });
        largeItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                widget.animateResize(200, 200);
            }
        });

        sizeMenu.setItems(smallItem, mediumItem, largeItem);

        Menu moveMenu = new Menu();
        moveMenu.setWidth(150);

        MenuItem upItem = new MenuItem("Up");
        upItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                widget.animateMove(widget.getOffsetX(), widget.getOffsetY() - 20);
            }
        });

        MenuItem rightItem = new MenuItem("Right");
        rightItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                widget.animateMove(widget.getOffsetX() + 20, widget.getOffsetY());
            }
        });

        MenuItem downItem = new MenuItem("Down");
        downItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                widget.animateMove(widget.getOffsetX(), widget.getOffsetY() + 20);
            }
        });

        MenuItem leftItem = new MenuItem("Left");
        leftItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                widget.animateMove(widget.getOffsetX() - 20, widget.getOffsetY());
            }
        });

        MenuItem defect = new MenuItem("Defect");
        defect.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(MenuItemClickEvent event) {
				new SampleDialog().show();
			}
		});
        
        moveMenu.setData(upItem, rightItem, downItem, leftItem);

        Menu mainMenu = new Menu();
        mainMenu.setWidth(150);
        widget.setContextMenu(mainMenu);

        MenuItem visibleItem = new MenuItem();
        visibleItem.setTitle("Visible");
        visibleItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                if (widget.isVisible()) {
                    widget.animateHide(AnimationEffect.FADE);
                } else {
                    widget.animateShow(AnimationEffect.FADE);
                }
            }
        });

        MenuItemSeparator separator = new MenuItemSeparator();

        MenuItem sizeItem = new MenuItem("Size");
        sizeItem.setSubmenu(sizeMenu);

        MenuItem moveItem = new MenuItem("Move");
        moveItem.setSubmenu(moveMenu);

        MenuItem resetItem = new MenuItem("Reset");
        resetItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
            public void onClick(MenuItemClickEvent event) {
                widget.animateRect(200, 75, 100, 100);
                widget.animateShow(AnimationEffect.FADE);
            }
        });
        resetItem.setIcon("crystal/128/apps/xmms.png");
        resetItem.setIconWidth(20);
        resetItem.setIconHeight(20);

        mainMenu.setItems(visibleItem, separator, sizeItem, moveItem, separator, resetItem, defect);

        IMenuButton mainMenuButton = new IMenuButton("Widget", mainMenu);
        mainMenuButton.setWidth(150);

        canvas.addChild(mainMenuButton);

        return canvas;
    }

    public String getIntro() {
        return DESCRIPTION;
    }

	// /////////////////////////////////////////////////////////////////////////////////
	// / Save Filter Dialog Class
	// //////////////////////////////////////////////////////////////////////////////////
	private class SampleDialog extends Window {
		private IPickTreeItem pickItem;
		
		public SampleDialog() {
			setSize("500", "400");
			
			pickItem = new IPickTreeItem("pickItem");  
			pickItem.setTitle("PickItem"); 
			pickItem.setValueField("id");
			pickItem.setLoadDataOnDemand(false);  
			pickItem.setCanSelectParentItems(false);
			pickItem.setWidth("100%");
			pickItem.setTitleAlign(Alignment.LEFT);
		    
			// Defined  filtersTree to be used to fill the DropDawnFilterTreeItem
			Tree pickTree = new Tree();
			pickTree.setModelType(TreeModelType.PARENT);
			
			// Use destination nodes to populate the destinationsTree
			pickTree.setData(new TreeNode[0]);
			pickItem.setValueTree(pickTree);
			
			DynamicForm form = new DynamicForm();
			form.setItems(pickItem);
			form.setTop(50);
			form.setLeft(50);
			form.setSize("400", "300");
			form.setBackgroundColor("yellow");

			final Menu testMenu = new Menu();
			testMenu.setWidth(150);
			testMenu.setID("testContextMenu");
			testMenu.addVisibilityChangedHandler(new VisibilityChangedHandler() {
				@Override
				public void onVisibilityChanged(VisibilityChangedEvent event) {
					if (event.getIsVisible()) {
						testMenu.markForRedraw();
					}
				}
			});

			MenuItem item1 = new MenuItem("Start");
			item1.setIcon("crystal/128/apps/xmms.png");
			item1.setIconHeight(16);
			item1.setIconWidth(16);

			// Complete Option
			MenuItem item2 = new MenuItem("Finish");
			item2.setIcon("crystal/128/apps/xmms.png");
			item2.setIconHeight(16);
			item2.setIconWidth(16);

			testMenu.setItems(item1, item2);
			form.setContextMenu(testMenu);

			addChild(form);
		}
	}
}