package org.wikiwebserver.sync.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import org.wikiwebserver.sync.DataLocation;
import org.wikiwebserver.sync.LocalFileDataLocation;
import org.wikiwebserver.sync.HTTPDataLocationConnector;

public class DataLocationPanel extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;

    private int site;
    
    // Local location metadata
    private List<File> excludedFiles;
    
    // WikiWebServer metadata
    private String superPassword, userId, sessionAuth;
    
    private JLabel typeLabel, pathLabel;
    private JComboBox typeBox;
    private JTextField pathField;
    private JButton browseButton;
    private JFileChooser fileChooser;
    
    public DataLocationPanel(DataLocation dataLocation) {
        this.site = dataLocation.getSite();
        
        this.setLayout(new BorderLayout());
        
        JPanel panel = new JPanel(new GridLayout(2, 1));
        panel.setBorder(new TitledBorder("Site " + site));
        
        String[] sourceStrings = { "Local directory", "WikiWebServer" };
        
        typeLabel = new JLabel("Type");
        typeBox = new JComboBox(sourceStrings);
        //typeBox.setEnabled(false);
        pathLabel = new JLabel("Path");
        pathField = new JTextField();
        browseButton = new JButton("Browse");
        
        if (dataLocation instanceof LocalFileDataLocation) {
            excludedFiles = ((LocalFileDataLocation)dataLocation).getExcludedFiles();
            
            typeBox.setSelectedIndex(0);
            browseButton.setEnabled(true);
        }
        else if (dataLocation instanceof HTTPDataLocationConnector) {
            superPassword = ((HTTPDataLocationConnector)dataLocation).getSuperPassword();
            userId = ((HTTPDataLocationConnector)dataLocation).getUserId();
            sessionAuth = ((HTTPDataLocationConnector)dataLocation).getSessionAuth();
            
            typeBox.setSelectedIndex(1);
            browseButton.setEnabled(false);
        }  
        pathField.setText(dataLocation.getBasePath().toString());        

        panel.add(constructRow(typeLabel, typeBox));
        panel.add(constructRow(pathLabel, pathField, browseButton));        
        
        add(panel, BorderLayout.CENTER);
        
        typeBox.addActionListener(this);
        browseButton.addActionListener(this);
    }
    
    public JPanel constructRow(JLabel label, JComponent... components) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(new EmptyBorder(3, 3, 3, 3));
        label.setPreferredSize(new Dimension(75, 20));
        panel.add(label, BorderLayout.WEST);
        panel.add(components[0], BorderLayout.CENTER);
        if (components.length == 2) {
            panel.add(components[1], BorderLayout.EAST);
        }
        return panel;
    }    
    
    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == typeBox) {
            switch (typeBox.getSelectedIndex()) {
                case 0:
                    browseButton.setEnabled(true);
                    pathLabel.setText("Path");
                    File file = new File(System.getProperty("user.home"));
                    File docs = new File(file, "My Documents\\");
                    if (docs.exists()) file = docs;                    
                    pathField.setText(file.getPath());
                    
                    break;
                case 1:
                    browseButton.setEnabled(false);
                    pathLabel.setText("URL");
                    pathField.setText("http://www.wikiwebserver.org/");     
                    break;
            }
        }
        else if (evt.getSource() == browseButton) {
            if (fileChooser == null) {
                fileChooser = new JFileChooser(); 
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            }
            fileChooser.setSelectedFile(new File(pathField.getText()));
            int returnVal = fileChooser.showOpenDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                pathField.setText(fileChooser.getSelectedFile().getPath());
            }
        }
    }

    public DataLocation getDataLocation() throws MalformedURLException {
        String path = pathField.getText();      
        System.out.println("superPassword=" + superPassword +
                           ", userID=" + userId + 
                           ", sessionAuth=" + sessionAuth);
        switch (typeBox.getSelectedIndex()) {
            case 0:
                LocalFileDataLocation local = new LocalFileDataLocation(new File(path), site);
                local.setExcludedFiles(excludedFiles);
                return local;
            case 1:
                if (superPassword != null) {
                    return new HTTPDataLocationConnector(new URL(path), superPassword, site);               
                }
                return new HTTPDataLocationConnector(new URL(path), userId, sessionAuth, site);               
        }
        return null;
    } 
}

