package model;

class Obj { //abstract
	public var name(default, set) : String;
	public var dbName(default, null) : String;
	public var className(default, null) : String;

	public var fields : Array<Field>;
	//drawing informations
	public var svg : Xml;
	public var row : Int;
	public var col : Int;

	function new(name : String) {
		this.name = name;
		this.fields = new Array();
	}

	function set_name(name : String) : String {
		this.name = name;
		dbName = fixName(name);
		className = dbName.charAt(0).toUpperCase() + dbName.substr(1);
		return this.name;
	}

	public function getField(fName : String) : Field {
		return Lambda.find(fields, f -> f.name == fName);
	}

	public function getFieldIndex(fName : String) : Int {
		return Lambda.findIndex(fields, f -> f.name == fName);
	}

	@SuppressWarnings("checkstyle:CyclomaticComplexity")
	public static function fixName(iStr : UnicodeString, camelCase : Bool = true) : String {
		var oStr : String = "";
		var toUpperCase : Bool = false;
		for (i in 0...iStr.length) {
			var c : String = iStr.charAt(i);
			oStr += switch (c) {
				case "à" |  "â" | "ä" 		: "a";
				case "À" | "Â" | "Ä" 		: "A";
				case "é" | "è" | "ê" | "ë" 	: "e";
				case "É" | "È" | "Ê" | "Ë" 	: "E";
				case "î" | "ï" 				: "i";
				case "Î" | "Ï" 				: "I";
				case "ô" | "ö" 				: "o";
				case "Ô" | "Ö" 				: "O";
				case "ù" | "û" | "ü" 		: "u";
				case "Ù" | "Û" | "Ü" 		: "U";
				case "æ" 					: "ea";
				case "Æ" 					: "EA";
				case "œ" 					: "eo";
				case "Œ" 					: "EO";
				case "ç" 					: "c";
				case "Ç" 					: "C";
				case " " 					: camelCase ? { toUpperCase = true; ""; } : "_";
				case "."					: "";
				default : toUpperCase ? { toUpperCase = false; c.toUpperCase(); } : c;
			}
		}
		return oStr;
	}
}
