package common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import error.ExceptionUtil;

/**
 * Utility functions used across the project
 */
public class Util {

	/**
	 * Return true if c is between a and b.
	 */
	public static boolean isBetween(int a, int b, int c) {
		return b > a ? c > a && c < b : c > b && c < a;
	}

	/**
	 * Parses a config file given its path and returns a map of the configurations
	 * 
	 * @param filepath
	 * @return
	 * @throws FileNotFoundException
	 */
	public static Map<String, Integer> parseConfigFile(String filepath, Logger logger) throws FileNotFoundException {

		Map<String, Integer> systemConfigs = new HashMap<>();

		File file = new File(filepath);
		
		try(BufferedReader br = new BufferedReader(new FileReader(file))) {
			String line;
			
			while ((line = br.readLine()) != null) {
				String config = line.split("\\=")[0];
				Integer value = Integer.parseInt(line.split("\\=")[1]);
				systemConfigs.put(config, value);
			}
		} catch (IOException e) {
			ExceptionUtil.handleCriticalException(e,
					"A critical error has occured while reading the system config file", logger);
		}

		return systemConfigs;
	}
}
