import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Affiche une barre d'actions en haut de la fenêtre.
 * @author V.BOULANGER
 */
public class UmlToolbar extends JPanel {

    public static final int SELECT_TOOL = 0;
    public static final int EDIT_TOOL = 1;
    public static final int DELETE_TOOL = 2;
    public static final int COPY_TOOL = 3;
    public static final int PASTE_TOOL = 4;
    public static final int CLASS_TOOL = 5;
    public static final int ASSOCIATION_TOOL = 6;
    public static final int LINK_TOOL = 7;

    private int _currentTool = 0;

    private JPanel _editionPanel;
    private JButton _select;
    private JButton _edit;
    private JButton _delete;
    private JButton _copy;
    private JButton _paste;
    private JButton _undo;
    private JButton _redo;

    private JPanel _modelisationPanel;
    private JButton _newClass;
    private JButton _newAssociation;
    private JButton _newLink;

    private UmlDiagram _umlDiagram;

    /**
     * Constructeur de la classe AppThinkerToolbar
     * @param diagram Le diagramme UML auquel appartient la toolbar.
     */
    public UmlToolbar(UmlDiagram diagram){
        _umlDiagram = diagram;
        //Création de la Toolbar
        this.setLayout(new GridLayout(2,2, 10, 0));
        this.setBackground(new Color(69, 69, 72));

        _editionPanel = new JPanel();
        _editionPanel.setLayout(new GridLayout(1,7));
        _select = new JButton();
        _select.setSize(32, 32);
        _select.setBorderPainted(false);
        _select.setIcon(new ImageIcon(getClass().getResource("img/x32/select.png")));
        _select.setToolTipText("Select item.");
        _select.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCurrentTool(UmlToolbar.SELECT_TOOL);
            }
        });
        _editionPanel.add(_select);
        _edit = new JButton();
        _edit.setSize(32, 32);
        _edit.setBorderPainted(false);
        _edit.setIcon(new ImageIcon(getClass().getResource("img/x32/edit.png")));
        _edit.setToolTipText("Edit item properties.");
        _edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCurrentTool(UmlToolbar.EDIT_TOOL);
            }
        });
        _editionPanel.add(_edit);
        _delete = new JButton();
        _delete.setSize(32, 32);
        _delete.setBorderPainted(false);
        _delete.setIcon(new ImageIcon(getClass().getResource("img/x32/delete.png")));
        _delete.setToolTipText("Delete item.");
        _delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCurrentTool(UmlToolbar.DELETE_TOOL);
            }
        });
        _editionPanel.add(_delete);
        _copy = new JButton();
        _copy.setSize(32, 32);
        _copy.setBorderPainted(false);
        _copy.setIcon(new ImageIcon(getClass().getResource("img/x32/copy.png")));
        _copy.setToolTipText("Copy item.");
        _copy.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCurrentTool(UmlToolbar.COPY_TOOL);
            }
        });
        _editionPanel.add(_copy);
        _paste = new JButton();
        _paste.setSize(32, 32);
        _paste.setBorderPainted(false);
        _paste.setIcon(new ImageIcon(getClass().getResource("img/x32/paste.png")));
        _paste.setToolTipText("Paste item.");
        _paste.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCurrentTool(UmlToolbar.PASTE_TOOL);
            }
        });
        _editionPanel.add(_paste);
        _undo = new JButton();
        _undo.setSize(32, 32);
        _undo.setBorderPainted(false);
        _undo.setIcon(new ImageIcon(getClass().getResource("img/x32/undo.png")));
        _undo.setToolTipText("Undo last change.");
        _editionPanel.add(_undo);
        _redo = new JButton();
        _redo.setSize(32, 32);
        _redo.setBorderPainted(false);
        _redo.setIcon(new ImageIcon(getClass().getResource("img/x32/redo.png")));
        _redo.setToolTipText("Redo last change.");
        _editionPanel.add(_redo);

        _modelisationPanel = new JPanel();
        _modelisationPanel.setLayout(new GridLayout(1,3));
        _newClass = new JButton();
        _newClass.setSize(32, 32);
        _newClass.setBorderPainted(false);
        _newClass.setIcon(new ImageIcon(getClass().getResource("img/x32/newClass.png")));
        _newClass.setToolTipText("Add a new class.");
        _newClass.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCurrentTool(UmlToolbar.CLASS_TOOL);
            }
        });
        _modelisationPanel.add(_newClass);
        _newAssociation = new JButton();
        _newAssociation.setSize(32, 32);
        _newAssociation.setBorderPainted(false);
        _newAssociation.setIcon(new ImageIcon(getClass().getResource("img/x32/newAssociation.png")));
        _newAssociation.setToolTipText("Add a new association.");
        _newAssociation.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCurrentTool(UmlToolbar.ASSOCIATION_TOOL);
            }
        });
        _modelisationPanel.add(_newAssociation);
        _newLink = new JButton();
        _newLink.setSize(32, 32);
        _newLink.setBorderPainted(false);
        _newLink.setIcon(new ImageIcon(getClass().getResource("img/x32/newLink.png")));
        _newLink.setToolTipText("Add a new link.");
        _newLink.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCurrentTool(UmlToolbar.LINK_TOOL);
            }
        });
        _modelisationPanel.add(_newLink);

        this.add(_editionPanel, BorderLayout.CENTER);
        this.add(_modelisationPanel, BorderLayout.CENTER);

        JLabel editLabel = new JLabel("Edition");
        editLabel.setHorizontalAlignment(JLabel.CENTER);
        editLabel.setForeground(Color.WHITE);
        this.add(editLabel);
        JLabel modelisationLabel = new JLabel("Modelisation");
        modelisationLabel.setHorizontalAlignment(JLabel.CENTER);
        modelisationLabel.setForeground(Color.WHITE);
        this.add(modelisationLabel);

        this.setEnabled(false);
    }

    /**
     * Verrouille/Déverrouille la barre d'outil.
     * @param enabled Paramètre de verrouillage.
     */
    public void setEnabled(boolean enabled){
        _select.setEnabled(enabled);
        _edit.setEnabled(enabled);
        _delete.setEnabled(enabled);
        _copy.setEnabled(enabled);
        _paste.setEnabled(enabled);
        _undo.setEnabled(enabled);
        _redo.setEnabled(enabled);

        _newClass.setEnabled(enabled);
        _newAssociation.setEnabled(enabled);
        _newLink.setEnabled(enabled);
    }

    /**
     * Récupère l'outil actuellement en fonction.
     * @return L'outil actuellement en fonction.
     */
    public int getCurrentTool(){
        return   this._currentTool;
    }

    /**
     * Change d'outil pour l'édition du diagramme.
     * @param currentTool L'outil cible.
     */
    public void setCurrentTool(int currentTool){
        this._currentTool = currentTool;
        if(this._currentTool == UmlToolbar.SELECT_TOOL) _umlDiagram.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        else _umlDiagram.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        switch(currentTool){
            case 1:
                if(_umlDiagram.getSelected() instanceof Class){
                    _umlDiagram.editClass((Class) _umlDiagram.getSelected());
                }
                AppThinker.getWindow().getStatusbar().setStatusMessage("Edit tool - Click an item to edit its properties.");
                break;
            case 2:
                if(_umlDiagram.getSelected() instanceof Class){
                    _umlDiagram.removeClass((Class) _umlDiagram.getSelected());
                }
                AppThinker.getWindow().getStatusbar().setStatusMessage("Delete tool - Click an item to delete it.");
                break;
            case 3:
                AppThinker.getWindow().getStatusbar().setStatusMessage("Copy tool - Click an item to copy it.");
                break;
            case 4:
                AppThinker.getWindow().getStatusbar().setStatusMessage("Paste tool - Click everywhere to paste the last copied item.");
                break;
            case 5:
                AppThinker.getWindow().getStatusbar().setStatusMessage("Class tool - Click everywhere to add a new class.");
                break;
            case 6:
                AppThinker.getWindow().getStatusbar().setStatusMessage("Association tool - Click everywhere to add a new association.");
                break;
            case 7:
                AppThinker.getWindow().getStatusbar().setStatusMessage("Link tool - Click on a class, hold, and release on another class.");
                break;
            default:
                AppThinker.getWindow().getStatusbar().setStatusMessage("Select tool - Click an item to select it or move it.");
                break;
        }
        _umlDiagram.repaint();
    }
}
