package org.gephi.ui.project;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import org.gephi.desktop.project.ProjectControllerUIImpl;
import org.gephi.project.api.Project;
import org.gephi.project.api.ProjectController;
import org.openide.awt.Actions;
import org.openide.util.Lookup;

public class ProjectList extends javax.swing.JPanel {

    /**
     * Creates new form ProjectList
     */
    public ProjectList() {
        initComponents();

        openProjectButton.setEnabled(false);
        removeProjectButton.setEnabled(false);
        projectList.setCellRenderer(new ProjectCellRenderer());
        projectList.addListSelectionListener(evt -> {
            if (!evt.getValueIsAdjusting()) {
                Project project = null;
                if (projectList.getSelectedIndex() != -1) {
                    project = projectList.getModel().getElementAt(projectList.getSelectedIndex());
                }
                openProjectButton.setEnabled(project != null && project.isClosed());
                removeProjectButton.setEnabled(project != null);
            }
        });

        openProjectButton.addActionListener(evt -> {
            Project project = projectList.getModel().getElementAt(projectList.getSelectedIndex());
            Actions.forID("File", "org.gephi.desktop.project.actions.OpenFile").actionPerformed(
                new ActionEvent(project, 0, null));
        });

        removeProjectButton.addActionListener(evt -> {
            Project project = projectList.getModel().getElementAt(projectList.getSelectedIndex());
            Lookup.getDefault().lookup(ProjectControllerUIImpl.class).removeProject(project);
            ((DefaultListModel<Project>)projectList.getModel()).removeElement(project);
        });

        setup();
    }

    private void setup() {
        // Project list
        ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
        DefaultListModel<Project> model = new DefaultListModel<>();
        for (Project p : pc.getAllProjects()) {
            model.addElement(p);
        }
        projectList.setModel(model);
    }

    static class ProjectCellRenderer extends DefaultListCellRenderer {

        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                                                      boolean cellHasFocus) {
            JLabel c = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            Project p = (Project) value;
            if (p.isOpen()) {
                c.setFont(c.getFont().deriveFont(Font.BOLD));
            } else {
                c.setFont(c.getFont().deriveFont(Font.PLAIN));
            }
            String name = p.getName();
            if (p.getFileName() != null) {
                name += " (" + p.getFileName() + ")";
            }
            c.setText(name);
            return c;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        projectList = new javax.swing.JList<>();
        openProjectButton = new javax.swing.JButton();
        removeProjectButton = new javax.swing.JButton();

        projectList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(projectList);

        org.openide.awt.Mnemonics.setLocalizedText(openProjectButton,
            org.openide.util.NbBundle.getMessage(ProjectList.class, "ProjectList.openProjectButton.text")); // NOI18N
        openProjectButton.setToolTipText(org.openide.util.NbBundle.getMessage(ProjectList.class,
            "ProjectList.openProjectButton.toolTipText")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(removeProjectButton,
            org.openide.util.NbBundle.getMessage(ProjectList.class, "ProjectList.removeProjectButton.text")); // NOI18N
        removeProjectButton.setToolTipText(org.openide.util.NbBundle.getMessage(ProjectList.class,
            "ProjectList.removeProjectButton.toolTipText")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 243,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(openProjectButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(removeProjectButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(openProjectButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(removeProjectButton))
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton openProjectButton;
    private javax.swing.JList<Project> projectList;
    private javax.swing.JButton removeProjectButton;
    // End of variables declaration//GEN-END:variables
}
