/*
 * Copyright (C) 2016 Angad Singh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cc.aoeiuv020.filepicker.utils;

import java.io.File;
import java.io.FileFilter;
import java.util.Locale;

import cc.aoeiuv020.filepicker.model.DialogConfigs;
import cc.aoeiuv020.filepicker.model.DialogProperties;

/**
 * <p>
 * Created by Angad Singh on 11-07-2016.
 * </p>
 */

/*  Class to filter the list of files.
 */
public class ExtensionFilter implements FileFilter {
    private final String[] validExtensions;
    private DialogProperties properties;

    public ExtensionFilter(DialogProperties properties) {
        if (properties.extensions != null) {
            this.validExtensions = properties.extensions;
        } else {
            this.validExtensions = new String[]{""};
        }
        this.properties = properties;
    }

    /**
     * Function to filter files based on defined rules.
     */
    @Override
    public boolean accept(File file) {
        //All directories are added in the least that can be read by the Application
        if (file.isDirectory() && file.canRead()) {
            return true;
        } else if (properties.selection_type == DialogConfigs.DIR_SELECT) {   /*  True for files, If the selection type is Directory type, ie.
         *  Only directory has to be selected from the list, then all files are
         *  ignored.
         */
            return false;
        } else {   /*  Check whether name of the file ends with the extension. Added if it
         *  does.
         */
            String name = file.getName().toLowerCase(Locale.getDefault());
            for (String ext : validExtensions) {
                if (name.endsWith(ext)) {
                    return true;
                }
            }
        }
        return false;
    }
}
