/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */
package com.sun.glass.ui;

import java.util.List;
import java.util.ArrayList;
import java.io.File;

public class CommonDialogs {

    /**
     * Available file chooser types.
     *
     * @see #show
     */
    public static final class Type {
        public static final int OPEN = 0;
        public static final int SAVE = 1;
    };

    /**
     * Provides a mechanism to filter different kinds of file name extensions.
     *
     * The following example creates an {@code ExtensionFilter} that will show
     * {@code txt} files:
     * <pre>
     *     List<String> extensions = new ArrayList<String>();
     *     extensions.add("*.txt");
     *     ExtensionFilter filter = new ExtensionFilter("Text files", extensions);
     *     List<ExtensionFilter> filters = new ArrayList<ExtensionFilter>();
     *     filters.add(filters);
     *     FileChooser.show(..., filters);
     * </pre>
     *
     * @see #show
     */
    public static class ExtensionFilter {
        private final String description;
        private final List<String> extensions;

        /**
         * Creates an {@code ExtensionFilter} with the specified description
         * and the file name extensions.
         *
         * @param description    the textual description for the filter
         * @param extensions     the accepted file name extensions
         * @throws   IllegalArgumentException
         *           if the description of the filter is {@code null} or empty
         * @throws   IllegalArgumentException
         *           if the extensions is {@code null}, empty or contains an empty string
         * @see #show
         */
        public ExtensionFilter(String description, List<String> extensions) {
            if (description == null || description.length() == 0) {
                throw new IllegalArgumentException("Description parameter must be non-null and not empty");
            }

            if (extensions == null || extensions.isEmpty()) {
                throw new IllegalArgumentException("Extensions parameter must be non-null and not empty");
            }

            for (String extension : extensions) {
                if (extension == null || extension.length() == 0) {
                    throw new IllegalArgumentException("Each extension must be non-null and not empty");
                }
            }

            this.description = description;
            this.extensions = extensions;
        }

        public String getDescription() {
            return description;
        }

        public List<String> getExtensions() {
            return extensions;
        }

        public String[] extensionsToArray() {
            return extensions.toArray(new String[0]);
        }
    }

    /**
     * Creates a native file chooser that lets the user select files.
     *
     * @param owner             the owner window for this file chooser (may be null)
     * @param folder            the initial folder, may be {@code null}
     * @param title             the title of the file chooser
     * @param type              the type of the file chooser, one of the constants from {@link Type}
     * @param multipleMode      enables or disable multiple file selections
     * @param extensionFilters  the filters of the file chooser
     * @throws IllegalArgumentException
     *         if the initial folder is an invalid folder
     * @throws IllegalArgumentException
     *         if the type doesn't equal one of the constants from {@link Type}
     * @return the files that the user selects. If the user cancels the file chooser,
     *         the method returns empty non-null array.
     */
    public static List<File> showFileChooser(Window owner, File folder, String title, int type,
                                        boolean multipleMode, List<ExtensionFilter> extensionFilters)
    {
        String _folder = convertFolder(folder);

        if (type != Type.OPEN && type != Type.SAVE) {
            throw new IllegalArgumentException("Type parameter must be equal to one of the constants from Type");
        }

        ExtensionFilter[] _extensionFilters = null;
        if (extensionFilters != null) {
            _extensionFilters = extensionFilters.toArray(new ExtensionFilter[0]);
        }

        String[] _files = Application.GetApplication().
                              staticCommonDialogs_showFileChooser(owner, _folder, convertTitle(title), type, multipleMode, _extensionFilters);

        List<File> files = new ArrayList<File>();
        for (String file : _files) {
            files.add(new File(file));
        }
        return files;
    }

    /**
     * Creates a native folder chooser that lets the user selects folder.
     *
     * @param owner  the owner window for this folder chooser (may be null)
     * @param folder the initial folder, may be {@code null}
     * @param title  the title of the folder chooser
     * @return the folder that the user selects. If the user cancels the folder chooser,
     *         the method returns null.
     */
    public static File showFolderChooser(Window owner, File folder, String title)
    {
        String selectedFolder = Application.GetApplication().staticCommonDialogs_showFolderChooser(owner, convertFolder(folder), convertTitle(title));
        return (selectedFolder != null) ? new File(selectedFolder) : null;
    }

    private static String convertFolder(File folder) {
        if (folder != null) {
            if (folder.isDirectory()) {
                return folder.getAbsolutePath();
            } else {
                throw new IllegalArgumentException("Folder parameter must be a valid folder");
            }
        }

        return "";
    }

    private static String convertTitle(String title) {
        return (title != null) ? title : "";
    }
}
