/*
 * aTunes 2.1.0-SNAPSHOT
 * Copyright (C) 2006-2011 Alex Aranda, Sylvain Gaudard and contributors
 *
 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
 *
 * http://www.atunes.org
 * http://sourceforge.net/projects/atunes
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package net.sourceforge.atunes.gui.views.dialogs;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

import net.sourceforge.atunes.gui.views.controls.AbstractCustomDialog;
import net.sourceforge.atunes.gui.views.controls.CustomJFileChooser;
import net.sourceforge.atunes.gui.views.controls.CustomTextArea;
import net.sourceforge.atunes.model.IExportOptionsDialog;
import net.sourceforge.atunes.model.IFrame;
import net.sourceforge.atunes.model.ILookAndFeelManager;
import net.sourceforge.atunes.model.IOSManager;
import net.sourceforge.atunes.utils.I18nUtils;

/**
 * The Class ExportOptionsDialog.
 * 
 * @author fleax
 */
public final class ExportOptionsDialog extends AbstractCustomDialog implements IExportOptionsDialog {

    private static final long serialVersionUID = 4403091324599627762L;

    /** RadioButton to export current navigator selection */
    private JRadioButton currentNavigatorSelectionRadioButton;

    /** RadioButton to export current play list selection */
    private JRadioButton currentPlayListSelectionRadioButton;

    /** The export location. */
    private CustomJFileChooser exportLocationFileChooser;

    /** The export button. */
    private JButton exportButton;

    /**
     * Setted to true when cancelled
     */
    private boolean cancel;
    
    private IOSManager osManager;
    
    /**
     * Instantiates a new export options dialog.
     * @param frame
     * @param osManager
     * @param lookAndFeelManager
     */
    public ExportOptionsDialog(IFrame frame, IOSManager osManager, ILookAndFeelManager lookAndFeelManager) {
        super(frame, 400, 220, true, CloseAction.DISPOSE, lookAndFeelManager.getCurrentLookAndFeel());
        this.osManager = osManager;
        setResizable(false);
        setTitle(I18nUtils.getString("EXPORT"));
        add(getContent());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                cancel = true;
            }
        });
    }
    
    /**
     * Gets the content.
     * 
     * @return the content
     */
    private JPanel getContent() {
        JPanel container = new JPanel(new GridBagLayout());

        // Export instructions
        JTextArea exportInstructions = new CustomTextArea(I18nUtils.getString("EXPORT_INSTRUCTIONS"));
        exportInstructions.setWrapStyleWord(true);
        exportInstructions.setLineWrap(true);
        exportInstructions.setEditable(false);
        exportInstructions.setOpaque(false);
        exportInstructions.setBorder(BorderFactory.createEmptyBorder());

        // Export Type
        JPanel exportTypePanel = new JPanel(new GridLayout(2, 1));
        currentNavigatorSelectionRadioButton = new JRadioButton(I18nUtils.getString("CURRENT_NAVIGATOR_SELECTION"));
        currentPlayListSelectionRadioButton = new JRadioButton(I18nUtils.getString("CURRENT_PLAYLIST_SELECTION"));
        ButtonGroup group = new ButtonGroup();
        group.add(currentNavigatorSelectionRadioButton);
        group.add(currentPlayListSelectionRadioButton);
        exportTypePanel.add(currentNavigatorSelectionRadioButton);
        exportTypePanel.add(currentPlayListSelectionRadioButton);
        currentNavigatorSelectionRadioButton.setSelected(true);

        // Export location
        JPanel exportLocationPanel = new JPanel(new BorderLayout());
        JLabel locationLabel = new JLabel(I18nUtils.getString("LOCATION"));
        exportLocationPanel.add(locationLabel, BorderLayout.WEST);
        exportLocationFileChooser = new CustomJFileChooser(this, 20, JFileChooser.DIRECTORIES_ONLY, osManager);
        exportLocationPanel.add(exportLocationFileChooser, BorderLayout.CENTER);

        // Buttons
        JPanel buttons = new JPanel(new GridLayout(1, 2, 5, 0));
        buttons.setOpaque(false);
        exportButton = new JButton(I18nUtils.getString("EXPORT"));
        exportButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
        JButton cancelButton = new JButton(I18nUtils.getString("CANCEL"));
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cancel = true;
                setVisible(false);
            }
        });
        buttons.add(exportButton);
        buttons.add(cancelButton);

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(10, 10, 0, 10);
        container.add(exportInstructions, c);

        c.gridy = 1;
        c.weighty = 0.5;
        c.fill = GridBagConstraints.VERTICAL;
        container.add(exportTypePanel, c);

        c.gridy = 2;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(0, 15, 0, 15);
        container.add(exportLocationPanel, c);

        c.gridy = 3;
        c.anchor = GridBagConstraints.EAST;
        c.weighty = 0;
        c.fill = GridBagConstraints.NONE;
        c.insets = new Insets(10, 0, 10, 10);
        container.add(buttons, c);
        return container;
    }

    /* (non-Javadoc)
	 * @see net.sourceforge.atunes.gui.views.dialogs.IExportOptionsDialog#getExportLocation()
	 */
    @Override
	public String getExportLocation() {
        return exportLocationFileChooser.getResult();
    }

    /* (non-Javadoc)
	 * @see net.sourceforge.atunes.gui.views.dialogs.IExportOptionsDialog#startDialog()
	 */
    @Override
	public void startDialog() {
        cancel = false;
        super.setVisible(true);
    }

    /* (non-Javadoc)
	 * @see net.sourceforge.atunes.gui.views.dialogs.IExportOptionsDialog#isCancel()
	 */
    @Override
	public boolean isCancel() {
        return cancel;
    }

    /* (non-Javadoc)
	 * @see net.sourceforge.atunes.gui.views.dialogs.IExportOptionsDialog#isExportNavigatorSelection()
	 */
    @Override
	public boolean isExportNavigatorSelection() {
        return currentNavigatorSelectionRadioButton.isSelected();
    }
}
