package model;

class Set extends Obj {
	public static final SUBSET_OPT_BOTH : Int = 0;
	public static final SUBSET_OPT_CHILDREN : Int = 1;
	public static final SUBSET_OPT_PARENT : Int = 2;

	public var superset : Set;
	public var subsets : Array<Set>;
	public var subsetType : String; //X, T, XT/P
	public var subsetOption : Int; //MLD
	public var subsetCode : String; //MLD

	public var id : Array<String>; //MCD + MLD
	public var compound : Set;

	public var isTypedef : Bool; //UML, used on reverse engineering only
	public var isInterface : Bool; //UML
	public var isAbstract : Bool; //UML
	public var interfaces : Array<Set>; //UML
	public var constructors : Array<Method>; //UML
	public var methods : Array<Method>; //UML
	public var accessors : Array<Method>; //UML
	public var navFields : Array<Field>; //UML

	public function new(name : String, ?superset : Set, isTypedef = false, isInterface = false, isAbstract = false) {
		super(name);

		this.superset = superset;
		this.subsets = new Array();
		this.subsetType = "";
		this.subsetOption = -1;
		this.subsetCode = "";

		this.id = new Array();
		this.compound = null;

		this.isTypedef = isTypedef;
		this.isInterface = isInterface;
		this.isAbstract = isAbstract;
		this.interfaces = new Array();
		this.constructors = new Array();
		this.methods = new Array();
		this.accessors = new Array();
		this.navFields = new Array();
	}

	public function getFieldCode(fName : String) : Field {
		var field : Field = Lambda.find(fields, f -> f.className == fName);
		if (field == null) {
			field = Lambda.find(navFields, f -> f.className == fName);
		}
		return field;
	}

	public function getFieldFromArrayType(tName : String) : Field {
		var field : Field = Lambda.find(fields, f -> f.classType.substr(6, f.classType.length - 7) == tName);
		if (field == null) {
			field = Lambda.find(navFields, f -> f.classType.substr(6, f.classType.length - 7) == tName);
		}
		return field;
	}

	public function isGeneric() : Bool {
		return Lambda.exists(fields, f -> !f.isStatic && (f.classType.indexOf("<T>") != -1 || f.classType == "T")) ||
			Lambda.exists(methods, m -> !m.isStatic && m.isGeneric());
	}

	public function getPluralClassName() : String { //french language specific
		var lastChar : String = className.charAt(className.length - 1).toLowerCase();
		var endStr : String = className.substring(className.length - 2, className.length).toLowerCase();
		var pluralName : String = className + "s";
		if (endStr == "au" || endStr == "eu") {
			pluralName = className + "x";
		} else if (endStr == "al") {
			pluralName = className.substring(0, className.length - 2) + "aux";
		} else if (lastChar == "z" || lastChar == "x" || lastChar == "s") {
			pluralName = className;
		}
		return pluralName;
	}
}
