/*
 * Copyright 2003-2012 Yusuke Yamamoto
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package one.cafebabe.samurai.swing;

import one.cafebabe.samurai.util.Configuration;
import one.cafebabe.samurai.util.GUIResourceBundle;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public final class FileHistory {
    private static final GUIResourceBundle resources = GUIResourceBundle.getInstance();
    private final List<File> files = new ArrayList<>();
    private final int numberToRemember = 10;
    public String config_RecentlyUsedFiles = "";
    private final Configuration config;
    private FileHistoryListener listener = null;
    private final JMenu openRecentMenu = new JMenu(resources.getMessage("menu.file.openRecent"));
    private final JFileChooser fileChooser = new JFileChooser();

    public FileHistory(Configuration config, FileHistoryListener listener) {
        this(config);
        setFileHistoryListener(listener);
    }

    public FileHistory(Configuration config) {
        this.config = config;
        config.apply(this);
        load();
        updateChildMenuItems();
    }

    public JMenu getOpenRecentMenu() {
        return this.openRecentMenu;
    }

    public synchronized JMenuItem getOpenMenu(final Component component) {
        JMenuItem openMenu = new JMenuItem(resources.getMessage("menu.file.open"));
            openMenu.addActionListener(e -> {
                if (JFileChooser.APPROVE_OPTION == fileChooser
                        .showOpenDialog(component)) {
                    File file = fileChooser.getSelectedFile();
                    open(file);
                }
            });
        return openMenu;
    }


    public void setFileHistoryListener(FileHistoryListener listener) {
        this.listener = listener;
    }

    public List<File> getList() {
        return files;
    }

    public void open(File[] files) {
        if (files.length == 1) {
            open(files[0]);
        } else {
            addHistory(files);
            if (null != listener) {
                listener.filesOpened(files);
            }
        }
    }

    public void addHistory(File[] files) {
        for (File file : files) {
            addHistory(file);
        }
    }

    public void clearHistory() {
        files.clear();
    }

    public void addHistory(File file) {
        delete(file);
        files.add(0, file);
        if (files.size() > numberToRemember) {
            files.remove(files.size() - 1);
        }
        save();
        updateChildMenuItems();
    }

    public void open(File file) {
        addHistory(file);
        if (null != listener) {
            listener.fileOpened(file);
        }
    }

    private void delete(File file) {
        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).equals(file)) {
                //noinspection SuspiciousListRemoveInLoop
                files.remove(i);
            }
        }
        updateChildMenuItems();
        save();
    }

    private void save() {
        cleanOrphans();
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < files.size(); i++) {
            buf.append(files.get(i).getAbsolutePath());
            if (i < (files.size() - 1)) {
                buf.append(File.pathSeparator);
            }
        }
        config_RecentlyUsedFiles = buf.toString();
        config.store(this);
    }

    private void load() {
        String[] fileArray = config_RecentlyUsedFiles.split(File.pathSeparator);
        for (String s : fileArray) {
            File file = new File(s);
            if (file.exists()) {
                files.add(file);
            }
        }
    }

    private boolean cleanOrphans = false;

    public void enableCleaningOrphans() {
        this.cleanOrphans = true;
    }

    public void disableCleaningOrphans() {
        this.cleanOrphans = false;
    }

    private void cleanOrphans() {
        if (cleanOrphans) {
            for (int i = 0; i < files.size(); i++) {
                if (!files.get(i).exists() || !files.get(i).isFile()) {
                    //noinspection SuspiciousListRemoveInLoop
                    files.remove(i);
                }
            }
        }
    }

    private void updateChildMenuItems() {
        cleanOrphans();
        openRecentMenu.removeAll();
        for (File file : files) {
            JMenuItem aHistory = new HistoryMenu(file);
            openRecentMenu.add(aHistory);
        }
        openRecentMenu.setEnabled(files.size() != 0);
    }

    class HistoryMenu extends JMenuItem implements ActionListener {
        private final File file;

        public HistoryMenu(File file) {
            super(file.getName() + " - " + file.getAbsolutePath());
            this.file = file;
            addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            open(file);
        }
    }
}

@@ -755,28 +755,17 @@
 x.swing.
-JFileChooser
+*
 ;%0Aimport
@@ -773,78 +773,14 @@
 java
-x.swing.JMenu;%0Aimport javax.swing.JMenuItem;%0Aimport java.awt.Component
+.awt.*
 ;%0Aim
@@ -2062,113 +2062,107 @@
     
-if (JFileChooser.APPROVE_OPTION == fileChooser%0A                        .showOpenDialog(component)) %7B%0A    
+FileDialog fileDialog = new FileDialog((Frame) null);%0A                fileDialog.setVisible(true);%0A
     
@@ -2169,32 +2169,34 @@
             File
+%5B%5D
  file = fileChoo
@@ -2195,31 +2195,23 @@
 file
-Chooser.getSelected
+Dialog.get
 File
+s
 ();%0A
@@ -2218,36 +2218,32 @@
                 
-  
-  
 open(file);%0A    
@@ -2238,34 +2238,16 @@
 (file);%0A
-                %7D%0A
         

use native file chooser to get access to files in sandboxed environment