import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Affiche une barre de menu en entête de la fenêtre.
 * @author V.BOULANGER
 */
public class AppThinkerMenuBar extends JMenuBar {

    private JMenu _fileMenu;
    private JMenuItem _newProject;
    private JMenuItem _openProject;
    private JMenuItem _saveProject;
    private JMenuItem _saveAsProject;
    private JMenuItem _projectOptions;
    private JMenuItem _closeProject;
    private JMenuItem _quitMenu;

    private JMenu _compositionMenu;

    private JMenu _appMenu;
    private JMenuItem _appSettings;
    private JMenuItem _appInfo;
    private JMenuItem _appChangelog;

    private AppThinkerWindow _window;

    /**
     * Constructeur de la classe AppThinkerMenuBar.
     * @param window Une instance de AppThinkerWindow.
     */
    public AppThinkerMenuBar(AppThinkerWindow window){
        _window = window;
        //Création de la barre menu
        _fileMenu = new JMenu("File");

        _newProject = new JMenuItem("New project");
        _newProject.setIcon(new ImageIcon(getClass().getResource("img/x16/newProject.png")));
        _newProject.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _window.newProject();
            }
        });
        _fileMenu.add(_newProject);
        _openProject = new JMenuItem("Open project");
        _openProject.setIcon(new ImageIcon(getClass().getResource("img/x16/importProject.png")));
        _openProject.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _window.openProject();
            }
        });
        _fileMenu.add(_openProject);
        _saveProject = new JMenuItem("Save project");
        _saveProject.setIcon(new ImageIcon(getClass().getResource("img/x16/saveProject.png")));
        _saveProject.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _window.getProject().saveProject();
            }
        });
        _fileMenu.add(_saveProject);
        _saveAsProject = new JMenuItem("Save project as...");
        _saveAsProject.setIcon(new ImageIcon(getClass().getResource("img/x16/saveAsProject.png")));
        _saveAsProject.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _window.getProject().saveAsProject();
            }
        });
        _fileMenu.add(_saveAsProject);
        _closeProject = new JMenuItem("Close project");
        _closeProject.setIcon(new ImageIcon(getClass().getResource("img/x16/closeProject.png")));
        _closeProject.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _window.closeProject();
            }
        });
        _projectOptions = new JMenuItem("Project options");
        _projectOptions.setIcon(new ImageIcon(getClass().getResource("img/x16/settings.png")));
        _fileMenu.add(_projectOptions);
        _fileMenu.add(_closeProject);
        _quitMenu = new JMenuItem("Quit");
        _quitMenu.setIcon(new ImageIcon(getClass().getResource("img/x16/quit.png")));
        _quitMenu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        _fileMenu.add(_quitMenu);

        this.add(_fileMenu);

        _appMenu = new JMenu("AppThinker");

        _appSettings = new JMenuItem("Settings");
        _appSettings.setIcon(new ImageIcon(getClass().getResource("img/x16/settings.png")));
        _appMenu.add(_appSettings);
        _appInfo = new JMenuItem("About AppThinker");
        _appInfo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AboutWindow about = new AboutWindow();
            }
        });
        _appInfo.setIcon(new ImageIcon(getClass().getResource("img/x16/info.png")));
        _appMenu.add(_appInfo);
        _appChangelog = new JMenuItem("Check for updates");
        _appChangelog.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Ouverture du lien de téléchargement
                try {
                    Desktop.getDesktop().browse(new URL("https://insset-my.sharepoint.com/:f:/g/personal/valentin_boulanger_insset_onmicrosoft_com/En4LybbeF2ZMpeSiesh_Af8BX3Fl1aDzTUWyw4dtQzJaag").toURI());
                } catch (URISyntaxException | IOException ex) { }
            }
        });
        _appChangelog.setIcon(new ImageIcon(getClass().getResource("img/x16/news.png")));
        _appMenu.add(_appChangelog);

        this.add(_appMenu);

        this.setProjectEnable(false);
    }

    public void addCompositionMenu(Composition composition){
        //Cette partie du menu est dynamique en fonction du type de composition affichée
        if(composition.getType() == Composition.UML_DIAGRAM){
            _compositionMenu = new JMenu("Composition");
            UmlToolbar apptt = (UmlToolbar) composition.getToolbar();
            JMenuItem selectElement = new JMenuItem("Select");
            selectElement.setIcon(new ImageIcon(getClass().getResource("img/x16/select.png")));
            selectElement.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    apptt.setCurrentTool(UmlToolbar.SELECT_TOOL);
                }
            });
            _compositionMenu.add(selectElement);
            JMenuItem editElement = new JMenuItem("Edit");
            editElement.setIcon(new ImageIcon(getClass().getResource("img/x16/edit.png")));
            editElement.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    apptt.setCurrentTool(UmlToolbar.EDIT_TOOL);
                }
            });
            _compositionMenu.add(editElement);
            JMenuItem deleteElement = new JMenuItem("Delete");
            deleteElement.setIcon(new ImageIcon(getClass().getResource("img/x16/delete.png")));
            deleteElement.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    apptt.setCurrentTool(UmlToolbar.DELETE_TOOL);
                }
            });
            _compositionMenu.add(deleteElement);


            JMenuItem newClass = new JMenuItem("New class");
            newClass.setIcon(new ImageIcon(getClass().getResource("img/x16/newClass.png")));
            newClass.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    apptt.setCurrentTool(UmlToolbar.CLASS_TOOL);
                }
            });
            _compositionMenu.add(newClass);

            JMenuItem strongRelation = new JMenuItem("Strong relation");
            strongRelation.setIcon(new ImageIcon(getClass().getResource("img/x16/strongRelation.png")));
            strongRelation.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    apptt.setCurrentTool(UmlToolbar.STRONG_TOOL);
                }
            });
            _compositionMenu.add(strongRelation);
            JMenuItem weakRelation = new JMenuItem("Weak relation");
            weakRelation.setIcon(new ImageIcon(getClass().getResource("img/x16/weakRelation.png")));
            weakRelation.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    apptt.setCurrentTool(UmlToolbar.WEAK_TOOL);
                }
            });
            _compositionMenu.add(weakRelation);
            JMenuItem compositionRelation = new JMenuItem("Composition relation");
            compositionRelation.setIcon(new ImageIcon(getClass().getResource("img/x16/compositionRelation.png")));
            compositionRelation.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    apptt.setCurrentTool(UmlToolbar.COMPOSITION_TOOL);
                }
            });
            _compositionMenu.add(compositionRelation);
            JMenuItem aggregationRelation = new JMenuItem("Aggregation relation");
            aggregationRelation.setIcon(new ImageIcon(getClass().getResource("img/x16/aggregationRelation.png")));
            aggregationRelation.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    apptt.setCurrentTool(UmlToolbar.AGGREGATION_TOOL);
                }
            });
            _compositionMenu.add(aggregationRelation);
            JMenuItem inheritanceRelation = new JMenuItem("Inheritance relation");
            inheritanceRelation.setIcon(new ImageIcon(getClass().getResource("img/x16/inheritanceRelation.png")));
            inheritanceRelation.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    apptt.setCurrentTool(UmlToolbar.INHERITANCE_TOOL);
                }
            });
            _compositionMenu.add(inheritanceRelation);

            this.add(_compositionMenu);
        }
    }

    /**
     * Retire le menu de composition
     */
    public void removeCompositionMenu(){
        this.remove(_compositionMenu);
    }

    /**
     * Active/Désactive les boutons relatifs au projet.
     * @param enabled Le paramètre d'activation.
     */
    public void setProjectEnable(boolean enabled){
        _saveProject.setEnabled(enabled);
        _saveAsProject.setEnabled(enabled);
        _projectOptions.setEnabled(enabled);
        _closeProject.setEnabled(enabled);
    }
}
