package eu.gity.jmeter.ddos.common;

import org.apache.http.conn.util.InetAddressUtils;
import org.apache.jmeter.util.JMeterUtils;

import javax.swing.*;
import java.util.regex.Pattern;

import static eu.gity.jmeter.ddos.properties.strings.DdosPropertiesStrings.*;

/**
 * This class takes care of validating user inputs into DDoS GUI components.
 *
 * @author Gity a.s.
 */
public class DDoSInputVerifier extends InputVerifier {

    /**
     * Checks whether the text input is a positive integer number
     *
     * @param value a String to be checked
     * @return true if the String contains a positivDDOS_e integer number, false otherwise
     */
    private boolean isPositiveInt(String value) {
        if (value.length() == 0) {
            return false;
        }
        for (int i = 0; i < value.length(); i++) {
            char c = value.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return Integer.parseInt(value) != 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean verify(JComponent input) {
        String name = input.getName();
        String value = ((JTextField) input).getText();

        switch (name) {
            case "ip":
                if (value.isEmpty()) {
                    ((JTextField) input).setText("192.168.0.1");
                    return true;
                }
                if (InetAddressUtils.isIPv4Address(value)) {
                    return true;
                } else {
                    JOptionPane.showMessageDialog(null, JMeterUtils.getLocaleString(DDOS_ERROR_IPADDRESS),
							JMeterUtils.getLocaleString(DDOS_ERROR_INPUT), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            case "mac":
                if (value.isEmpty()) {
                    ((JTextField) input).setText("aa:bb:cc:dd:ee:ff");
                    return true;
                }
                if (Pattern.matches("^[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}$", value)) {
                    return true;
                } else {
                    JOptionPane.showMessageDialog(null, JMeterUtils.getLocaleString(DDOS_ERROR_MACADDRESS),
							JMeterUtils.getLocaleString(DDOS_ERROR_INPUT), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            case "port":
                if (value.isEmpty()) {
                    ((JTextField) input).setText("1024");
                    return true;
                }
                if (Pattern.matches("^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$", value)) {
                    return true;
                } else {
                    JOptionPane.showMessageDialog(null, JMeterUtils.getLocaleString(DDOS_ERROR_PORT),
							JMeterUtils.getLocaleString(DDOS_ERROR_INPUT), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            case "domain":
                if (value.isEmpty()) {
                    ((JTextField) input).setText("www.gity.eu");
                    return true;
                }
                if (Pattern.matches("^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]+[a-zA-Z0-9]+$", value)) {
                    return true;
                } else {
                    JOptionPane.showMessageDialog(null, JMeterUtils.getLocaleString(DDOS_ERROR_DOMAIN),
							JMeterUtils.getLocaleString(DDOS_ERROR_INPUT), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            case "natural":
                if (value.isEmpty()) {
                    ((JTextField) input).setText("10");
                    return true;
                }
                if (isPositiveInt(value)) {
                    return true;
                } else {
                    JOptionPane.showMessageDialog(null, JMeterUtils.getLocaleString(DDOS_ERROR_INTEGER),
							JMeterUtils.getLocaleString(DDOS_ERROR_INPUT), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            case "naturalOrNothing":
                if (value.isEmpty()) {
                    return true;
                }
                if (isPositiveInt(value)) {
                    return true;
                } else {
                    JOptionPane.showMessageDialog(null, JMeterUtils.getLocaleString(DDOS_ERROR_INTEGER),
							JMeterUtils.getLocaleString(DDOS_ERROR_INPUT), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
        }
        return false;
    }
}
