/*
 * Copyright 2014-2015 See AUTHORS file.
 *
 * 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 com.kotcrab.vis.editor.ui.dialog;

import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.ObjectMap;
import com.kotcrab.vis.editor.util.gdx.PrefHeightIfVissibleValue;
import com.kotcrab.vis.ui.util.TableUtils;
import com.kotcrab.vis.ui.util.dialog.DialogUtils;
import com.kotcrab.vis.ui.widget.*;

/**
 * Dialog used to select new file
 * @author Kotcrab
 */
public class SelectFileDialog extends VisWindow {
	private String extension;
	private boolean hideExtension;
	private FileHandle folder;
	private FileDialogListener listener;

	private ObjectMap<String, FileHandle> fileMap = new ObjectMap<>();

	private VisLabel noFilesLabel;
	private VisList<String> fileList;
	private VisTextButton okButton;

	public SelectFileDialog (String extension, FileHandle folder, FileDialogListener listener) {
		this(extension, false, folder, listener);
	}

	public SelectFileDialog (String extension, boolean hideExtension, FileHandle folder, FileDialogListener listener) {
		super("Select File");
		this.extension = extension;
		this.hideExtension = hideExtension;
		this.folder = folder;
		this.listener = listener;

		setModal(true);
		addCloseButton();
		closeOnEscape();

		fileList = new VisList<>();

		VisTextButton cancelButton;

		TableUtils.setSpacingDefaults(this);
		defaults().left();

		VisTable buttonsTable = new VisTable(true);
		buttonsTable.add(cancelButton = new VisTextButton("Cancel"));
		buttonsTable.add(okButton = new VisTextButton("OK"));

		noFilesLabel = new VisLabel("There isn't any available file to select");

		add(noFilesLabel).height(new PrefHeightIfVissibleValue()).center().spaceBottom(0).row();
		add(fileList).expand().fill().height(new PrefHeightIfVissibleValue()).row();
		add(buttonsTable).padBottom(2).right();

		cancelButton.addListener(new ChangeListener() {
			@Override
			public void changed (ChangeEvent event, Actor actor) {
				fadeOut();
			}
		});

		okButton.addListener(new ChangeListener() {
			@Override
			public void changed (ChangeEvent event, Actor actor) {
				finishSelection();
			}
		});

		fileList.addListener(new ClickListener() {
			@Override
			public void clicked (InputEvent event, float x, float y) {
				if (getTapCount() == 2 && event.getButton() == Buttons.LEFT) finishSelection();
			}
		});

		rebuildFileList();
	}

	private void packAndCenter () {
		pack();
		setSize(getWidth() + 80, getHeight());
		centerWindow();
	}

	public void rebuildFileList () {
		fileList.clearItems();
		fileMap.clear();

		buildFileList(folder);

		if (fileMap.size == 0) {
			noFilesLabel.setVisible(true);
			okButton.setDisabled(true);
		} else {
			noFilesLabel.setVisible(false);
			okButton.setDisabled(false);
		}

		packAndCenter();
	}

	private void finishSelection () {
		FileHandle file = fileMap.get(fileList.getSelected());

		if (file == null) {
			DialogUtils.showErrorDialog(getStage(), "You must select file!");
			return;
		}

		listener.selected(file);
		fadeOut();
	}

	private void buildFileList (FileHandle directory) {
		for (FileHandle file : directory.list()) {
			if (file.isDirectory()) buildFileList(file);

			if (file.extension().equals(extension))
				fileMap.put(file.path().substring(folder.path().length() + 1, file.path().length() - (hideExtension ? extension.length() + 1 : 0)), file);
		}

		fileList.setItems(fileMap.keys().toArray());
	}

	public interface FileDialogListener {
		void selected (FileHandle file);
	}
}

@@ -1298,24 +1298,26 @@
 ivate String
+%5B%5D
  extension;%0A
@@ -1306,32 +1306,33 @@
 ring%5B%5D extension
+s
 ;%0A%09private boole
@@ -1586,16 +1586,117 @@
 utton;%0A%0A
+%09/** @param extensions multiple extensions can be passed seprated with %7C for example: mp3%7Cwav%7Cogg */%0A
 %09public 
@@ -1721,32 +1721,33 @@
 String extension
+s
 , FileHandle fol
@@ -1798,16 +1798,17 @@
 xtension
+s
 , false,
@@ -1827,24 +1827,125 @@
 tener);%0A%09%7D%0A%0A
+%09/** @param extensions multiple extensions can be passed seprated with %7C for example: mp3%7Cwav%7Cogg */%0A
 %09public Sele
@@ -1974,16 +1974,17 @@
 xtension
+s
 , boolea
@@ -2078,38 +2078,8 @@
 %22);%0A
-%09%09this.extension = extension;%0A
 %09%09th
@@ -2163,16 +2163,61 @@
 istener;
+%0A%09%09this.extensions = extensions.split(%22%5C%5C%7C%22);
 %0A%0A%09%09setM
@@ -4188,16 +4188,57 @@
 file);%0A%0A
+%09%09%09for(String extension : extensions) %7B%0A%09
 %09%09%09if (f
@@ -4272,16 +4272,17 @@
 nsion))%0A
+%09
 %09%09%09%09file
@@ -4379,16 +4379,21 @@
 nsion ? 
+file.
 extensio
@@ -4393,16 +4393,18 @@
 xtension
+()
 .length(
@@ -4419,24 +4419,29 @@
 0)), file);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09fileL

Add support for multiple extensions in SelectFileDialog