/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */
package com.sun.glass.ui;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

import com.sun.glass.ui.Pixels;
import com.sun.glass.ui.delegate.MenuDelegate;
import com.sun.glass.ui.delegate.MenuItemDelegate;

public class Menu {
    
    public static class EventHandler {
        // currently used only on Mac OS X
        public void handleMenuOpening(Menu menu, long time) {
        }
        
        // currently used only on Mac OS X
        public void handleMenuClosed(Menu menu, long time) {
        }
    }
    
    public EventHandler getEventHandler() {
        return eventHandler;
    }
    
    public void setEventHandler(EventHandler eventHandler) {
        this.eventHandler = eventHandler;
    }
    
    private final MenuDelegate delegate;

    private String title;
    private boolean enabled;
    private final List<Object> items = new ArrayList<Object>();

    private EventHandler eventHandler;

    protected Menu(String title) {
        this(title, true);
    }

    protected Menu(String title, boolean enabled) {
        this.title = title;
        this.enabled = enabled;
        delegate = PlatformFactory.getPlatformFactory().createMenuDelegate(this);
        if (!delegate.createMenu(title, enabled)) {
            throw new RuntimeException("Menu creation error.");
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        if (delegate.setTitle(title)) {
            this.title = title;
        }
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        if (delegate.setEnabled(enabled)) {
            this.enabled = enabled;
        }
    }
    
    public boolean setPixels(Pixels pixels) {
        return (delegate.setPixels(pixels));
    }
    
    /**
     * Gets list of menu elements.
     * Objects in the list returned are instances of Menu or MenuItem.
     */
    public List<Object> getItems() {
        return Collections.unmodifiableList(items);
    }

    public void add(Menu menu) {
        insert(menu, items.size());
    }

    public void add(MenuItem item) {
        insert(item, items.size());
    }

    public void insert(Menu menu, int pos) throws IndexOutOfBoundsException {
        if (menu == null) {
            throw new IllegalArgumentException();
        }
        synchronized (items) {
            if (pos < 0 || pos > items.size()) {
                throw new IndexOutOfBoundsException();
            }
            MenuDelegate menuDelegate = menu.getDelegate();
            if (delegate.insert(menuDelegate, pos)) {
                items.add(pos, menu);
            }
        }
    }

    public void insert(MenuItem item, int pos) throws IndexOutOfBoundsException {
        synchronized (items) {
            if (pos < 0 || pos > items.size()) {
                throw new IndexOutOfBoundsException();
            }
            MenuItemDelegate itemDelegate = item != null ? item.getDelegate() : null;
            if (delegate.insert(itemDelegate, pos)) {
                items.add(pos, item);
            }
        }
    }

    public void remove(int pos) throws IndexOutOfBoundsException {
        synchronized (items) {
            Object item = items.get(pos);   // throws IndexOutOfBoundsException
            boolean success = false;
            if (item instanceof MenuItem) {
                success = delegate.remove(((MenuItem)item).getDelegate(), pos);
            } else {
                success = delegate.remove(((Menu)item).getDelegate(), pos);
            }
            if (success) {
                items.remove(pos);
            }
        }
    }

    // public void remove(Menu menu)
    // public void remove(MenuItem item);

    // package private
    MenuDelegate getDelegate() {
        return delegate;
    }
    
    // *****************************************************
    // notification callbacks
    // *****************************************************
    protected void notifyMenuOpening() {
        if (this.eventHandler != null) {
            eventHandler.handleMenuOpening(this, System.nanoTime());
        }
    }
    
    protected void notifyMenuClosed() {
        if (this.eventHandler != null) {
            eventHandler.handleMenuClosed(this, System.nanoTime());
        }
    }
}

