package com.dlsc.flexgantt.showcase.demo.actions;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.dlsc.flexgantt.showcase.AbstractDemo;
import com.dlsc.flexgantt.showcase.DemoControlsPanel;
import com.dlsc.flexgantt.showcase.demo.actions.ActionsDemoGanttChartModel.RequestedActions;
import com.dlsc.flexgantt.model.gantt.DefaultTimelineObject;
import com.dlsc.flexgantt.model.gantt.TimelineObjectPath;
import com.dlsc.flexgantt.model.treetable.KeyColumn;
import com.dlsc.flexgantt.swing.GanttChart;
import com.dlsc.flexgantt.swing.GanttChartFrame;
import com.dlsc.flexgantt.swing.layer.LayerContainer;
import com.dlsc.flexgantt.swing.layer.system.ActionLayer;
import com.dlsc.flexgantt.swing.layer.system.IActionProvider;

public class ActionsDemo extends AbstractDemo implements IActionProvider {

    private GanttChart gc;
    
    private AbstractAction actionAlignLeft;
    private AbstractAction actionAlignCenter;
    private AbstractAction actionAlignRight;
    private AbstractAction actionAlignJustified;
    private AbstractAction actionFontItalic;
    private AbstractAction actionFontBold;
    private AbstractAction actionEditCut;
    private AbstractAction actionEditCopy;
    private AbstractAction actionEditPaste;
    private AbstractAction actionFontUnderlined;

    public ActionsDemo() {
        super("Actions");
    }

    @Override
    public String[] getSourceCodeFileNames() {
        return new String[] { "ActionsDemo", "ActionsDemoGanttChartModel" };
    }

    @Override
    public void run(boolean embedded) {
        gc = createGanttChart(new ActionsDemoGanttChartModel());
        gc.expandAll();

        KeyColumn key = gc.getColumnModel().getKeyColumn();
        key.setWidth(200);

        gc.resetToPreferredSizes();
        gc.getTreeTable().setRootVisible(false);

        createActions();

        ActionLayer actionLayer = gc.getLayerContainer().getSystemLayer(
                ActionLayer.class);
        actionLayer.setActionProvider(this);
        actionLayer.getButtonPanel().setColumns(7);
        actionLayer.getButtonPanel().setBackgroundPaint(
                new GradientPaint(0, 0, new Color(230, 230, 230), 0, 60,
                        new Color(250, 250, 250)));

        GanttChartFrame frame = createFrame("Actions", gc, embedded);
        
        JPanel controls = new DemoControlsPanel(this);
        controls.setLayout(new BorderLayout());
        
        frame.add(BorderLayout.EAST, controls);
        frame.center();
        frame.setVisible(true);
    }
   
    private void createActions() {
        actionAlignLeft = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 1 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionAlignCenter = new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 2 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionAlignRight = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 3 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionAlignJustified = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 4 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionFontItalic = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 5 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionFontBold = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 6 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionFontUnderlined = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 7 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionEditCut = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 8 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionEditCopy = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 9 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionEditPaste = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gc, "Action 10 was clicked!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        };

        actionAlignLeft.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("text_align_left.png")));
        actionAlignCenter.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("text_align_center.png")));
        actionAlignRight.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("text_align_right.png")));
        actionAlignJustified.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("text_align_justified.png")));
        
        actionFontItalic.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("text_italics.png")));
        actionFontBold.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("text_bold.png")));
        actionFontUnderlined.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("text_underlined.png")));

        actionEditCut.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("cut.png")));
        actionEditCopy.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("copy.png")));
        actionEditPaste.putValue(Action.SMALL_ICON, new ImageIcon(getClass()
                .getResource("paste.png")));

        actionAlignLeft.putValue(Action.SHORT_DESCRIPTION, "Align object to the left");
        actionAlignCenter.putValue(Action.SHORT_DESCRIPTION, "Align object in the center");
        actionAlignRight.putValue(Action.SHORT_DESCRIPTION, "Align object to the right");
        actionAlignJustified.putValue(Action.SHORT_DESCRIPTION, "Align object justified");
        actionFontItalic.putValue(Action.SHORT_DESCRIPTION, "Make text italic");
        actionFontBold.putValue(Action.SHORT_DESCRIPTION, "Make text bold");
        actionFontUnderlined.putValue(Action.SHORT_DESCRIPTION, "Make text underlined");
        actionEditCut.putValue(Action.SHORT_DESCRIPTION, "Cut the selection");
        actionEditCopy.putValue(Action.SHORT_DESCRIPTION, "Copy the selection");
        actionEditPaste.putValue(Action.SHORT_DESCRIPTION, "Paste the selection");
    }

    @Override
    public List<Action> getTimelineObjectActions(LayerContainer lc,
            TimelineObjectPath path) {
        
        List<Action> actions = new ArrayList<Action>();
        
        DefaultTimelineObject<?> tlo = (DefaultTimelineObject<?>) path.getTimelineObject();
        RequestedActions requestedActions = (RequestedActions) tlo.getUserObject();
        
        switch (requestedActions) {
        case ALIGNMENT:
            actions.add(actionAlignLeft);
            actions.add(actionAlignCenter);
            actions.add(actionAlignRight);
            actions.add(actionAlignJustified);
            break;
        case CUT_COPY_PASTE:
            actions.add(actionEditCut);
            actions.add(actionEditCopy);
            actions.add(actionEditPaste);
            break;
        case FONT_STYLE:
            actions.add(actionFontBold);
            actions.add(actionFontItalic);
            actions.add(actionFontUnderlined);
            break;
        case ALL_ACTIONS:
            actions.add(actionAlignLeft);
            actions.add(actionAlignCenter);
            actions.add(actionAlignRight);
            actions.add(actionAlignJustified);
            actions.add(actionEditCut);
            actions.add(actionEditCopy);
            actions.add(actionEditPaste);
            actions.add(actionFontBold);
            actions.add(actionFontItalic);
            actions.add(actionFontUnderlined);
            break;
        }
        
        return actions;
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ActionsDemo());
    }
}
