/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.vutbr.feec.utko.fojtoval.krypto.funkce;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.netbeans.validation.api.ui.ValidationPanel;
import org.netbeans.validation.api.builtin.Validators;
import org.netbeans.validation.api.ui.ValidationGroup;

/**
 * Validace vstupu od uzivatele (napr. pri zadavani parametru sifrovych systemu atd.).
 * @author fojtoval
 */
public class Validator {
    static int screenH = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
    static int screenW = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;

    /**
     * Validuje, jestli je zadany parametr (integer) ze spravneho rozsahu atd.
     * @param text vstupni parametr
     * @param min  spodni hranice rozsahu
     * @param max  horni hranice rozsahu
     * @return
     * @throws Exception
     */
    public static int validint(String text, int min, int max) throws Exception {
    int status=-1;

    JPanel inner = new JPanel();
    //inner.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint());

    JLabel lbl = new JLabel(text);

    JTextField f = new JTextField();

    f.setColumns(10);

    f.setName("Vstup");

    inner.add(lbl);

    inner.add(f);

    ValidationPanel panel = new ValidationPanel();

    panel.setInnerComponent(inner);

    ValidationGroup group = panel.getValidationGroup();

    group.add(f, Validators.REQUIRE_VALID_INTEGER, Validators.numberRange(min, max));

    if (panel.showOkCancelDialog("Vstup"))
        status=Integer.parseInt(f.getText());
    return status;
  }

    /**
     * Validuje, jestli je zadany parametr (string) ze spravneho rozsahu atd.
     * @param text vstupni parametr
     * @param min spodni hranice rozsahu
     * @param max horni hranice rozsahu
     * @return
     * @throws Exception
     */
    public static String validstring(String text, int min, int max) throws Exception {
    
      String status = "";

      JPanel inner = new JPanel();

      JLabel lbl = new JLabel(text);

      JTextField f = new JTextField();

      f.setColumns(25);

      f.setName("Vstup");

      inner.add(lbl);

      inner.add(f);

      ValidationPanel panel = new ValidationPanel();
    //inner.setLayout(new BoxLayout(inner, BoxLayout.Y_AXIS));
    //inner.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

      panel.setInnerComponent(inner);

      ValidationGroup group = panel.getValidationGroup();

      group.add(f, Validators.REQUIRE_NON_EMPTY_STRING,
                Validators.NO_WHITESPACE,
                Validators.regexp("[a-z]{"+min+","+max+"}", "pouze znaky a-z", false));

      if (panel.showOkCancelDialog("Vstup")) {
        String normalized_string = java.text.Normalizer.normalize(f.getText(), java.text.Normalizer.Form.NFD);  // odstraneni diakritiky
        String ascii = normalized_string.replaceAll("[^\\p{ASCII}]","");                                  // odstraneni diakritiky
        String mala = ascii.toLowerCase();  // vse na mala pismena
        status = mala.replaceAll("[^abcdefghijklmnopqrstuvwxyz]","");
      }
    return status;
  }
}
