package model;

class Field {
	public static final ACCESS_UNSET : Int = 0;
	public static final ACCESS_PRIVATE : Int = 1;
	public static final ACCESS_PROTECTED : Int = 2;
	public static final ACCESS_PUBLIC : Int = 3;

	public static function getVisibilitySymbol(value : Int) : String {
		return switch (value) {
			case Field.ACCESS_PRIVATE : "-";
			case Field.ACCESS_PROTECTED : "#";
			case Field.ACCESS_PUBLIC : "+";
			default : " ";
		}
	}

	public var name(default, set) : String;
	public var className(default, null) : String;
	public var type(default, set) : String;
	public var dbType(default, null) : String;
	public var classType(default, null) : String;
	public var languageType : String;
	public var visibility : Int; //UML (class field context)
	public var isStatic : Bool; //UML (class field context)

	public function new(name : String, type : String, visibility : Int = 0, isStatic : Bool = false) {
		this.name = name;
		this.type = type;
		this.visibility = visibility;
		this.isStatic = isStatic;
	}

	function set_name(name : String) : String {
		this.name = name;
		className = Obj.fixName(name);
		return this.name;
	}

	function set_type(type : String) : String {
		this.type = type;
		dbType = type.toUpperCase() == "COUNTER" ? "INT" : type.toUpperCase();
		classType = classOf(type);
		languageType = classType;
		return this.type;
	}

	public static function classOf(typeName : String) : String {
		var upperName : String = typeName.toUpperCase();
		return	if (upperName == "FLOAT") "Float";
				else if (upperName == "DOUBLE") "Double";
				else if (upperName == "BOOLEAN") "Bool";
				else if (upperName == "DATE") "Date";
				else if (upperName == "TIME") "Time";
				else if (upperName == "DATETIME") "DateTime";
				else if (upperName == "INTEGER" || upperName == "INT" || upperName == "COUNTER") "Int";
				else if (upperName.indexOf("CHAR(") == 0 || upperName.indexOf("VARCHAR(") == 0 || upperName == "TEXT") "String";
				else typeName;

				//~ else if (upperName == "TINYINT" || upperName == "SMALLINT" || upperName == "MEDIUMINT" || 
					//~ upperName == "INT1" ||  upperName == "INT2" ||  upperName == "INT3" || upperName == "INT4") "Int";
				//~ else if (upperName == "BIGINT" || upperName == "INT8") "Long";
	}

	public static function arrayOf(typeName : String) : String {
		return "Array<" + classOf(typeName) + ">";
	}

	public function isArray() : Bool {
		return classType.substring(0, 5) == "Array";
	}

	public function toDomainString() : String {
		return getVisibilitySymbol(visibility) + " " + className;
	}

	public function toUMLString() : String {
		var str : String = getVisibilitySymbol(visibility) + " " + className;
		if (classType != "") {
			str += " : " + classType;
		}
		return str;
	}
}
