package Window;

import IHM.WorkingImage;
import Action.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Interface extends JFrame {

    public static final String NAME_PROGRAM = "myPhotoshop";
    public static final String PATH_TO_PLUGIN_FOLDER = "plugin/";


    private static Interface inter;

    public JScrollPane getScrollPane() {
        return scrollPane;
    }

    private JScrollPane scrollPane;

    private Interface(String name) {
        super(name);
        createWindow();
    }

    public static Interface getInstance() {
        if (inter == null)
            inter = new Interface(NAME_PROGRAM);
        return inter;
    }

    public void setMenuBar() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JMenuBar menuBar = createMenuBar();
        //JMenu plugins = loadPlugin(PATH_TO_PLUGIN_FOLDER);
        //menuBar.add(plugins);
        this.setJMenuBar(menuBar);
    }

    public void createWindow() {
        setMenuBar();
        scrollPane = new JScrollPane(WorkingImage.getInstance());
        this.setContentPane(scrollPane);
    }

    //Display the window.

    public void showWindow() {
        pack();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setVisible(true);
        repaint();
    }

    public JMenuBar createMenuBar() {
        JMenuItem menuItem;
        JMenu menu, submenu;
        JMenuBar menuBar = new JMenuBar();

        //Menu File
        menu = new JMenu("File");
        menuItem = new JMenuItem(new ActionOpenProject("Open Project", "Opens a new Project", KeyEvent.VK_O));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('O'));
        menu.add(menuItem);
        menuItem = new JMenuItem(new ActionSaveProject("Save Project", "Save the Project", KeyEvent.VK_S));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('S'));
        menu.add(menuItem);
        menuItem = new JMenuItem(new ActionCloseProject("Close Project", "Close the actual Project", KeyEvent.VK_X));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('x'));
        menu.add(menuItem);
        menu.add(new JPopupMenu.Separator());
        menuItem = new JMenuItem(new ActionOpenImage("Open Image", "Opens a new Image", KeyEvent.VK_O));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('o'));
        menu.add(menuItem);
        menuItem = new JMenuItem(new ActionSaveImage("Save Image", "Save the actual Image", KeyEvent.VK_S));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('s'));
        menu.add(menuItem);
        menu.add(new JPopupMenu.Separator());
        menuItem = new JMenuItem(new ActionReloadPlugin("Reload Plugin", "Reload the plugin", KeyEvent.VK_P));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('p'));
        menu.add(menuItem);
        menu.add(new JPopupMenu.Separator());
        menuItem = new JMenuItem(new ActionQuit("EXIT", "Exit the program", KeyEvent.VK_ESCAPE));
        menuItem.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_ESCAPE));
        menu.add(menuItem);
        menuBar.add(menu);

        //Menu Project
        menu = new JMenu("Project");
        menuItem = new JMenuItem(new ActionNextImage("Next Image", "Start working on next image", KeyEvent.VK_N));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('n'));
        menu.add(menuItem);
        submenu = new JMenu("Select Image");
        ArrayList<ImageIcon> tmp = WorkingImage.getInstance().getImages();
        for (int i = 0; i < tmp.size(); i++) {
            menuItem = new JMenuItem(new ActionChooseImage(WorkingImage.getInstance().getName(tmp.get(i)), i));
            submenu.add(menuItem);
        }
        if (WorkingImage.getInstance().getImages().size() == 0)
            submenu.setEnabled(false);
        menu.add(submenu);
        menuBar.add(menu);

        //Menu Edition
        menu = new JMenu("Edition");
        menuItem = new JMenuItem(new ActionUndoAll("Undo All", "Undo all modification", KeyEvent.VK_U));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('U'));
        menu.add(menuItem);
        menuItem = new JMenuItem(new ActionUndo("Undo", "Undo the last action performed", KeyEvent.VK_U));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('u'));
        menu.add(menuItem);
        menuItem = new JMenuItem(new ActionRedo("Redo", "Redo the last action undone", KeyEvent.VK_R));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('r'));
        menu.add(menuItem);
        menuItem = new JMenuItem(new ActionRedoAll("Redo All", "Redo all actions undone", KeyEvent.VK_R));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('R'));
        menu.add(menuItem);
        menu.add(new JPopupMenu.Separator());
        menuItem = new JMenuItem(new ActionZoom("Zoom In", "Zoom into the current image", KeyEvent.VK_ADD));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('+'));
        menu.add(menuItem);
        menuItem = new JMenuItem(new ActionZoom("Zoom Out", "Zoom outto the current image", KeyEvent.VK_MINUS));
        menuItem.setAccelerator(KeyStroke.getKeyStroke('-'));
        menu.add(menuItem);
        menuBar.add(menu);

        return menuBar;
    }

    private JMenu loadPlugin(String pathToFolder) throws IOException, ClassNotFoundException {
        File folderPlugin = new File(pathToFolder);
        File[] listFiles = folderPlugin.listFiles();
        JMenu menu = new JMenu("Plugins");
        for (int i = 0; i < listFiles.length; i++) {
            File file = listFiles[i];
            JMenu submenu = new JMenu(file.getName().substring(0, file.getName().length() - ".jar".length()));
            JMenuItem menuItem;
            JarFile jarFile = new JarFile(file.getAbsolutePath());
            System.out.println(file.getCanonicalPath());
            Enumeration e = jarFile.entries();
            URL url = new URL("jar:file:" + file.getAbsolutePath() + "!/");
            URL[] urls = {url};
            ClassLoader child = new URLClassLoader(urls);
            while (e.hasMoreElements()) {
                JarEntry je = (JarEntry) e.nextElement();
                if (je.isDirectory() || !je.getName().endsWith(".class") || je.getName().contains("IPlugin.class"))
                    continue;
                String className = je.getName().substring(0, je.getName().length() - ".class".length());
                className = className.replace('/', '.');
                Class c = Class.forName(className, true, child);
                menuItem = new JMenuItem(new ActionPerformFilter(c));
                submenu.add(menuItem);
            }
            menu.add(submenu);
        }
        return menu;
    }

}
