package org.wikiwebserver.sync.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;


public class SyncToolBar extends JToolBar implements ActionListener {
    
    private static final long serialVersionUID = 1L;

    public enum Action { ADD_SITE, REMOVE_SITE, DIFF, SYNC, INTERRUPT };
    
    private JSiteSync syncGUI;
    private JButton syncButton, stopButton;

    public SyncToolBar(JSiteSync syncGUI) {
        this.syncGUI = syncGUI;
        
        add(makeButton("folder_add.png", Action.ADD_SITE, "Add another directory", "Add"));
        add(makeButton("folder_delete.png", Action.REMOVE_SITE, "Remove directory", "Remove"));
        addSeparator();
        add(makeButton("folder_magnify.png", Action.DIFF, "Show differences between sites", "Analyse"));
        addSeparator();
        syncButton = makeButton("arrow_refresh.png", Action.SYNC, "Synchronize sites", "Synchronize");
        syncButton.setEnabled(false);
        addSeparator();
        add(syncButton);
        addSeparator();
        stopButton = makeButton("delete.png", Action.INTERRUPT, "Stop all active tasks", "Stop");
        stopButton.setEnabled(!syncGUI.getSyncher().isComplete());
        add(stopButton);        
    }
    
    protected JButton makeButton(String imageName, Action action,
                                 String toolTipText, String altText) {
        //Look for the image.
        String imgLocation = "icons/" + imageName;
        URL imageURL = SyncToolBar.class.getResource(imgLocation);

        //Create and initialize the button.
        JButton button = new JButton();
        button.setActionCommand(action.name());
        button.setToolTipText(toolTipText);
        button.addActionListener(this);

        if (imageURL != null) {
            button.setIcon(new ImageIcon(imageURL, altText));
            button.setText(altText);
        } else {  
            button.setText(altText);
            System.err.println("Resource not found: " + imgLocation);
        }

        return button;
    }

    public void actionPerformed(ActionEvent evt) {
        
        String action = evt.getActionCommand();
        
        if (Action.INTERRUPT.name().equals(action)) {
            syncGUI.interrupt();
        }        
        else if (Action.ADD_SITE.name().equals(action)) {
            syncGUI.addDataLocation();
        }    
        else if (Action.REMOVE_SITE.name().equals(action)) {
            syncGUI.removeDataLocation();
        }         
        else {
            try {
                if (Action.DIFF.name().equals(action)) {
                    syncGUI.findDifferencesInBackground();
                    try { Thread.sleep(500);
                    } catch (InterruptedException ex) { }
                }
                else if (Action.SYNC.name().equals(action)) {
                    syncGUI.performSyncInBackground();
                    try { Thread.sleep(500);
                    } catch (InterruptedException ex) { }
                }  
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            }
        }
        
        updateButtons();
    }
    
    public void updateButtons() {
        syncButton.setEnabled(syncGUI.getSyncher().getSyncCommands() != null);
        stopButton.setEnabled(!syncGUI.getSyncher().isComplete());
    }
}

