/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.parameter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.hft.stuttgart.citydoctor2.error.ErrorHandler;

/**
 * Class for parsing input arguments
 * 
 * @author Matthias Betz
 *
 */
public class ArgumentParser {

	public static final String CONFIG = "config";

	private final HashMap<String, List<String>> arguments;

	public ArgumentParser(String[] args) {
		arguments = parseArguments(args);
	}

	public List<String> getValues(String option) {
		return arguments.get(option.toLowerCase());
	}

	public boolean containsOption(String option) {
		return arguments.containsKey(option);
	}

	private HashMap<String, List<String>> parseArguments(String[] args) {
		HashMap<String, List<String>> params = new HashMap<>();

		List<String> options = null;
		for (int i = 0; i < args.length; i++) {
			final String a = args[i];

			if (a.charAt(0) == '-') {
				if (a.length() < 2) {
					ErrorHandler.printHelpAndTerminate("Error while parsing argument " + a);
				}
				String flag = a.substring(1).toLowerCase();
				options = new ArrayList<>();
				params.put(flag, options);
			} else if (options != null) {
				options.add(a);
			} else {
				ErrorHandler.printHelpAndTerminate("Illegal parameter usage at " + a);
			}
		}
		return params;
	}

}
