package _VisualDVM.GlobalData.CompilerOption;
import Common.Database.Objects.DBObject;
import Common.Utils.Utils_;

import java.util.Arrays;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CompilerOption extends DBObject {
    public String name = "";
    public String parameterSeparator = "";
    public String parameterName = "";
    public String parameterValue = "";
    public Vector<String> parameterVariants = new Vector<>();
    public Vector<String> description = new Vector<>();
    //--------------------------------------
    public boolean hasParameter() {
        return !parameterName.isEmpty();
    }
    @Override
    public Object getPK() {
        return name;
    }
    @Override
    public String toString() {
        return name + (hasParameter() ? (parameterSeparator +
                (parameterValue.contains(" ") ? Utils_.DQuotes(parameterValue) : parameterValue)) : "");
    }
    public void CheckParameterVariants() {
        for (String line : description)
            if (CheckLine(line)) break;
    }
    public boolean CheckLine(String line) {
        if (hasParameter()) {
            Pattern DVM_PARAM_VALUES_REGEX = Pattern.compile(Utils_.TBrackets(parameterName) + "\\s*=\\s*" + "\\w+(\\|\\w+)+", Pattern.CASE_INSENSITIVE);
            Matcher matcher = DVM_PARAM_VALUES_REGEX.matcher(line);
            if (matcher.find()) {
                String s = line.substring(matcher.start(), matcher.end());
                String packed = s.substring(s.lastIndexOf('=') + 1).trim();
                parameterVariants = new Vector<>(Arrays.asList(packed.split("\\|")));
                return true;
            }
        }
        return false;
    }
}
