/*
 * The MIT License
 *
 * Copyright 2016 Pawel Marynowski.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package pattypan;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.geometry.HPos;
import javafx.scene.layout.ColumnConstraints;

public final class Util {

  private Util() {}

  public static int WINDOW_WIDTH = 750;
  public static int WINDOW_HEIGHT = 550;

  /* row and column utils */
  
  public static ColumnConstraints newColumn(int value) {
    return newColumn(value, "%", HPos.CENTER);
  }
  
  public static ColumnConstraints newColumn(int value, String unit) {
    return newColumn(value, unit, HPos.CENTER);
  }
  
  public static ColumnConstraints newColumn(int value, String unit, HPos position) {
    ColumnConstraints col = new ColumnConstraints();
    if(unit.equals("%")) {
      col.setPercentWidth(value);
    }
    if(unit.equals("px")) {
      col.setMaxWidth(value);
      col.setMinWidth(value);
    }
    
    if(position != null) {
      col.setHalignment(position);
    }
    return col;
  }
  
  /* file utils */
  private final static ArrayList<String> allowedExtentionImage =
          new ArrayList<>(Arrays.asList("png", "gif", "jpg", "jpeg", "tiff", "tif", "xcf"));
  
  public static ArrayList<String> getVariablesFromString(String text) {
    final Pattern pattern = Pattern.compile("\\$\\{(.*?)\\}");
    Matcher m = pattern.matcher(text);
    ArrayList<String> results = new ArrayList<>();

    while (m.find()) {
      results.add(m.group(1));
    }
    return results;
  }
  
  public static String getExtFromFilename(String filename) {
    String extension = "";

    int i = filename.lastIndexOf('.');
    if (i >= 0) {
      extension = filename.substring(i + 1).toLowerCase();
    }
    return extension;
  }
   
  public static File[] getFilesAllowedToUpload(File directory) {
      return directory.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File directory, String name) {
          return isFileAllowedToUpload(name);
        }
      });
  }
  
  public static File[] getFilesAllowedToUpload(File directory, String ext) {
      return directory.listFiles((File dir, String name)
              -> name.toLowerCase().endsWith(ext)
      );
  }
  
  public static Map<String, Integer> getFilesByExtention(File[] files) {
    Map<String, Integer> map = new HashMap<>();

    for(File file : files) {
      String ext = getExtFromFilename(file.getName());
      if (map.containsKey(ext)) {
        int count = map.get(ext);
        map.replace(ext, ++count);
      } else {
        map.put(ext, 1);
      }
    }
    return map;
  }
  
  public static boolean isFileAllowedToUpload(String name) {
    return allowedExtentionImage.indexOf(getExtFromFilename(name)) > -1;
  }
}

@@ -1301,24 +1301,100 @@
 a.util.Map;%0A
+import java.util.MissingResourceException;%0Aimport java.util.ResourceBundle;%0A
 import java.
@@ -1649,16 +1649,269 @@
 T = 550;
+%0A  static ResourceBundle bundle = ResourceBundle.getBundle(%22pattypan/text/messages%22);%0A  %0A  public static String text(String key) %7B%0A    try %7B%0A      return bundle.getString(key);%0A    %7D catch (final MissingResourceException ex) %7B%0A      return %22%22;%0A    %7D%0A  %7D
 %0A%0A  /* r

add l10n