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

import com.sun.glass.ui.Application;
import com.sun.glass.ui.CommonDialogs.Type;
import com.sun.glass.ui.CommonDialogs.ExtensionFilter;
import com.sun.glass.ui.Window;

import java.util.List;
import java.util.ArrayList;


/**
 * MacOSX platform implementation class for CommonDialogs.
 *
 * @author Dmitry Cherepanov
 */
final class MacCommonDialogs {
    native private static void _initIDs();
    static {
        Application.loadNativeLibrary();
        _initIDs();
    }
    
    private static native String[] _showFileOpenChooser(long owner, String folder, String title,
						    boolean multipleMode, Object[] extensions);
    private static native String[] _showFileSaveChooser(long owner, String folder, String title,
						    Object[] extensions);

    private static native String _showFolderChooser(String folder, String title);

    static String[] showFileChooser_impl(Window owner, String folder, String title, int type,
                                         boolean multipleMode, ExtensionFilter[] extensionFilters) {
        List<String> list = new ArrayList<String>();
        for (ExtensionFilter extension : extensionFilters) {
            for (String suffix : extension.getExtensions()) {
                list.add(suffix);
            }
        }
        Object extensions[] = list.toArray();

        final long ownerPtr = owner != null ? owner.getNativeWindow() : 0L;
        
        if (type == Type.OPEN) {
            return _showFileOpenChooser(ownerPtr, folder, title, multipleMode, extensions);
        } else if (type == Type.SAVE) {
            return _showFileSaveChooser(ownerPtr, folder, title, extensions);
        } else {
            return null;
        }
    }
    
    static String showFolderChooser_impl() {
        return _showFolderChooser(null, null);
    }
}
