package cn.edu.seu.ise.dca.util;

import java.util.regex.Pattern;

public class StringUtil {
	
	/**
	 * Locate the position of specific string in string array 
	 * @param list specifies the string array
	 * @param word specifies the string need to locate
	 * @return
	 */
	public static int indexOf(String[] list, String word) {
		if(list.length == 0) {
			return -1;
		}
		int index = 0;
		for(String element : list) {
			if(word.equals(element)) {
				return index;
			} else {
				index ++;
			}
		}
		return -1;
	}
	
	/**
	 * Fine all characters that are not contained in quotes 
	 * @param line
	 * @param a
	 * @return
	 */
	public static int findAllNotInQuote(String line, char a) {
		int number = 0;
		boolean isInDoubleQuote = false;
		boolean isInQuote = false;
		char[] chars = line.toCharArray();
		for(char ch : chars) {
			
			if(ch == '\"') {
				if(isInDoubleQuote) {
					isInDoubleQuote = false;
				} else {
					isInDoubleQuote = true;
				}	
			} else if(ch == '\'') {
				if(isInDoubleQuote) {
					//Do nothing
				} else {
					if(isInQuote) {
						isInQuote = false;
					} else {
						isInQuote = true;
					}
				}
			} else {
				if(!isInDoubleQuote && !isInQuote && ch ==a)
					number ++;
			}
		}
		return number;
	}
	
	public static boolean isActualLine(String line) {
		boolean isActualLine = true;
		if(line.equalsIgnoreCase("")) {
			isActualLine = false;
		} else {
			if(line.contains("//")) {
				if(!(Pattern.compile(".*\".*\\/\\/.*\".*").matcher(line).find() || Pattern.compile(".*\'.*\\/\\/.*\'.*").matcher(line).find())) {
					//For single line comment , remove it.
					line = line.substring(0, line.indexOf("//")).trim();
					//If the whole line is comment
					if(line.equalsIgnoreCase("")) {
						isActualLine = false;
					}
				}
			}
			if(line.contains("#")){
				//If the # is not in "" or ''
				if(!(Pattern.compile(".*\".*\\#.*\".*").matcher(line).find() || Pattern.compile(".*\'.*\\#.*\'.*").matcher(line).find())) {
					//For single UNIX comment line, remove it.
					line = line.substring(0, line.indexOf("#")).trim();
					//If the whole line is comment
					if(line.equalsIgnoreCase("")) {
						isActualLine = false;
					}
				}
			} 
		
			if(line.contains("/*") && !line.contains("*/")){
				line = line.substring(0, line.indexOf("/*")).trim();
				//If the whole line is comment
				if(line.equalsIgnoreCase("")) {
					isActualLine = false;
				}
			}
			if(line.startsWith("*") && !line.contains("*/")) {
				isActualLine = false;		
			} 
			if(line.contains("*/")) {
				line = line.substring(line.indexOf("*/") + 2, line.length()).trim();
				//If the whole line is comment
				if(line.equalsIgnoreCase("")) {
					isActualLine = false;
				}
			}
			if(line.contains("/*") && line.contains("*/") && line.indexOf("/*") < line.indexOf("*/")){
				//For those contain multiple line comments in single line, remove it
				line = line.replaceAll("/\\*.*\\*/", "").trim();
				//If the whole line is comment
				if(line.equalsIgnoreCase("")) {
					isActualLine = false;
				}
			} 
		}
		return isActualLine;
	}
}
