/* Supports C-style comments (single line and block)
 new Attribute: case sensitive opens a block for the following parameters:
 parameter key are not case sensitive
 CAPTION: Caption shown in the PM label (single language ONLY)
 TOOLTIP: Detailed description shown in the tooltip
 TYPE_OBJECTS: MySurvey item attributes file
 TYPE_ATTRIBUTE: See enum "TypeField": STRING/INTEGER/SCALE
 EDIT: See enum "EditableState": READ_ONLY/ENABLED
 BUBBLE: See enum "BubbleVisibility": SHOW/COLLAPSED (HIDE doesn't make sense, just don't call showInBubble then).
*/
version "0.9"

//New proposed style
new Attribute ":EDF_D_MHETAVA"
	CAPTION="MHETAVA"
	TOOLTIP="MHETAVA metadata"
	TYPE_ATTRIBUTE=STRING
	EDIT=READ_ONLY
	BUBBLE=COLLAPSED
end

new Attribute "TYPE"
	CAPTION = "Type"
	TOOLTIP="The type"
	TYPE_ATTRIBUT=STRING
	EDIT=ENABLED
	BUBBLE=SHOW
end

new Attribute "AREA"
	CAPTION="Area"
	TOOLTIP="The Surface area of a sphere."
	TYPE_ATTRIBUT=STRING
	EDIT=ENABLED
	BUBBLE=COLLAPSED
end

new Attribute ":EDF-CTETUDES"
	CAPTION="CTETUDES"
	TOOLTIP="CTETUDES metadata"
	TYPE_ATTRIBUT=STRING
	EDIT=ENABLED
	BUBBLE=COLLAPSED
end

new Attribute ":EDF-TOME-CTETUDES"
	CAPTION="TOME-CTETUDES"
	TOOLTIP="A global ID for the entire site"
	TYPE_ATTRIBUT=STRING
	EDIT=ENABLED
	BUBBLE=COLLAPSED
end

new Attribute ":CUTLENGTH"
	CAPTION="Cut Length"
	TOOLTIP="The tube center's arc length"
	TYPE_ATTRIBUT=STRING
	EDIT=ENABLED
	BUBBLE=COLLAPSED
end

new Attribute ":PSIWEIGHT"
	CAPTION="PSI Weight"
	TOOLTIP="Pressure per square inch?"
	TYPE_ATTRIBUT=STRING
	EDIT=ENABLED
	BUBBLE=COLLAPSED
end

new Attribute ":EDF.N_AFFAIR"
	CAPTION="# Affair"
	TOOLTIP="Some unique ID number"
	TYPE_ATTRIBUT=STRING
	EDIT=ENABLED
	BUBBLE=COLLAPSED
end

new Attribute "DESC"
	CAPTION="Description"
	TOOLTIP="Description metadata"
	TYPE_ATTRIBUT=STRING
	EDIT=ENABLED
	BUBBLE=COLLAPSED
end

new Attribute "SPREF"
	CAPTION="Spec. Ref."
	TOOLTIP="Specification Reference"
	TYPE_ATTRIBUT=STRING
	EDIT=ENABLED
	BUBBLE=COLLAPSED
end

setFlag("SHOW_EMPTY_BUBBLES", "FALSE")
setFlag("PULL_UP_ONLY_CHILD", "TRUE")
//String keys and types will need to be surrounded by quotes, such as "SPREF" to avoid conflicts with keywords!

//Create an alias for some code, simple pre-processor style (not a variable because it's constant).
alias type prop("TYPE") //Returns the TYPE in metadata of the current provider which is being interrogated, a shorthand equivalent
//TODO: May need this to handle cases where it's impossible to hover a group (only the leaf items)
//alias typeAnc propAncestor("TYPE", "TYPE", "*") //Returns the TYPE in metadata of the current provider which is being interrogated, a shorthand equivalent

//A method with zero input params doesn't need the parenthesis "type()" can just be "type"
//There are 3 possible OUTCOMES for the bubble NAME output, thus

alias pathType path("TYPE") //Generate the path to the root, for example: ["OBJECTS"/"SITE"/"ZONE"/"PIPE"/"BRAN"/"ELBO"]

// START /////////////////////////////////////////////////////////////////////

//import "Import_RVM_ATT.rules" //Do we want a specific file extension for this file?

/* List of all possible actions:
Global:
	setFlag("SHOW_EMPTY_BUBBLES", "TRUE")
	getFlag("SHOW_EMPTY_BUBBLES")
	propAncestor(<PROP>, <PROP_ANCESTOR>,<VALUE_ANCESTOR_1>,...,<VALUE_ANCESTOR_N>) //Get the value of an ancestor's property
	propAncestorIfUnset
	path(<PROP>) //Get the path (including self) of an item. By default the optional param is "NAME", but can be changed to list the specified property of each ancestor rather than it's name
	name() //get the name of the provider, same as prop("NAME").
PM:
	showInPM(<PROP>,<VALUE_OVERRIDE>)
Bubble Tooltip	:
	showInBubble(<PROP>,<VALUE_OVERRIDE>)
	showInBubble(ICON)
Import:
	Assembly during import
		assemble()
		addColumnAssembly(<PROP>,<VALUE_OVERRIDE>)
	Add/overwrite an item's metadata value once during import.
		addImportMeta
	Remarkables during import
		setAsRemarkable()
ExportCSV:
	addColumnCSV(<PROP>,<VALUE_OVERRIDE>)
*/
// ALPHA /////////////////////////////////////////////////////////////////////

alias ATTR_EDF [":EDF_D_MHETAVA", ":EDF-CTETUDES", ":EDF-TOME-CTETUDES"]
alias PIPE_SUBCOMPS ["TUBI","FLAN","ELBO","GASK","OLET","FBLI","REDU","TEE","BEND"]
alias PIPE_SUBCOMPS2 ["TUBI","FLAN","ELBO","GASK","OLET","FBLI","REDU","TEE","BEND","BRAN"]
alias EQUI_COMPS ["NOZZ","BOX","CTOR","CYLI","SUBE","CBOX","CTOR","GENPRI","ARCHIV"]
alias HVAC_SUBCOMPS ["BRAN","TUBI","DAMP","SILE","ACCES PANEL"]

/*for T in ATTR_EDF
	if type in ["PIPE","EQUI","HVAC","STRU"]
		showInBubble(T)
	else if pathType equals ["*"/"PIPE"/"BRAN"/"?"]  //PIPE and BRAN must be a direct grandparent and parent respectively
//	and type in [PIPE_SUBCOMPS, "BRAN"]   //The type must be one of the ones in PIPE_SUBCOMPS
		showInBubble(T,propAncestorIfUnset(T,"TYPE","BRAN","PIPE"))
	else if pathType contains ["EQUI"/"?"] //EQUI must be an ancestor (parent or higher)
	and type in EQUI_COMPS
		showInBubble(T,propAncestor(T,"TYPE","EQUI"))
	else if pathType contains ["HVAC"/"BRAN"] //Must be BRAN or have BRAN ancestor
	and type in HVAC_SUBCOMPS
		showInBubble(T,propAncestorIfUnset(T,"TYPE","BRAN","HVAC"))
	else if pathType contains ["STRU"/"?"] //STRU must be a direct parent
		showInBubble(T,propAncestor(T,"TYPE","STRU"))
	else if pathType equals ["*"/"STRU"/"EQUI"] //Must be an EQUI with parent STRU (NOTE equals instead of contains!)
		showInBubble(T,propAncestorIfUnset(T,"TYPE","EQUI","STRU"))
	else if pathType contains ["STRU"/"EQUI"/"?"] //STRU and EQUI must be a direct grandparent and parent respectively
		showInBubble(T,propAncestor(T,"TYPE","STRU"))
	end
end*/

//Debugging:
//showInBubble("TYPE")
//showInPM("TYPE")

if prop("TYPE") not equals "" //If it had a TYPE from ATT file, show it under the "Custom" section of the Panel
	showInPM(":EDF_D_MHETAVA")
//	showInPM("TYPE")
//	showInPM(":CUTLENGTH")
//	showInPM(":EDF-CTETUDES")
//	showInPM(":EDF-TOME-CTETUDES")
//	showInPM(":PSIWEIGHT")
//	showInPM(":EDF.N_AFFAIR")
	showInPM("DESC")
	showInPM("SPREF")
end

//if type equals "" //TODO: Tries to guess a TYPE for primitives based on their shape type, but ideally the ATT file should specify this!
//	if prop("TYPE_MS") equals "Cylinder" //Check ancestor to decide whether a cylinder becomes a "TUBI" or "CYLI":
//		if pathType contains ["EQUI"/"?"] //If the ancestor (parent or higher) is an EQUI
//			addImportMeta("TYPE", "CYLI") //During import, overwrites the metadata key+value specified
//		else
//			addImportMeta("TYPE", "TUBI")
//		end
//	else if prop("TYPE_MS") equals "Pyramid"
//		addImportMeta("TYPE", "GENPRI")
//	else if prop("TYPE_MS") equals "Mesh"
//	or prop("TYPE_MS") equals "Extruded polygon"
//		addImportMeta("TYPE", "FTUB")
//	end
//end

//TODO: handle the "for" keyword:
//for (KEY : [":EDF_D_MHETAVA", ":EDF-CTETUDES", ":EDF-TOME-CTETUDES"]) //TODO: implement a for loop to unroll (easy way)
alias MHETAVA ":EDF_D_MHETAVA" //Temporary fix
if type in ["PIPE","EQUI","HVAC","STRU"]
	showInBubble(MHETAVA)
else if pathType equals ["*"/"PIPE"/"BRAN"/"?"]  //PIPE and BRAN must be a direct grandparent and parent respectively
	and type in PIPE_SUBCOMPS2   //The type must be one of the ones in PIPE_SUBCOMPS2
	showInBubble(MHETAVA,propAncestorIfUnset(MHETAVA,"TYPE","BRAN","PIPE"))
else if pathType contains ["EQUI"/"?"] //EQUI must be an ancestor (parent or higher)
and type in EQUI_COMPS
	showInBubble(MHETAVA,propAncestor(MHETAVA,"TYPE","EQUI"))
else if pathType contains ["HVAC"/"BRAN"] //Must be BRAN or have BRAN ancestor
and type in HVAC_SUBCOMPS
	showInBubble(MHETAVA,propAncestorIfUnset(MHETAVA,"TYPE","BRAN","HVAC"))
else if pathType equals ["*"/"STRU"/"?"] //STRU must be a direct parent
	showInBubble(MHETAVA,propAncestor(MHETAVA,"TYPE","STRU"))
else if pathType equals ["*"/"STRU"/"EQUI"] //Must be an EQUI with parent STRU (NOTE equals instead of contains!)
	showInBubble(MHETAVA,propAncestorIfUnset(MHETAVA,"TYPE","EQUI","STRU"))
else if pathType contains ["STRU"/"EQUI"/"?"] //STRU and EQUI must be a direct grandparent and parent respectively
	showInBubble(MHETAVA,propAncestor(MHETAVA,"TYPE","STRU"))
end

alias CTE ":EDF-CTETUDES" //Temporary fix
if type in ["PIPE","EQUI","HVAC","STRU"]
	showInBubble(CTE)
else if pathType equals ["*"/"PIPE"/"BRAN"/"?"]  //PIPE and BRAN must be a direct grandparent and parent respectively
	and type in PIPE_SUBCOMPS2   //The type must be one of the ones in PIPE_SUBCOMPS2
	showInBubble(CTE,propAncestorIfUnset(CTE,"TYPE","BRAN","PIPE"))
else if pathType contains ["EQUI"/"?"] //EQUI must be an ancestor (parent or higher)
and type in EQUI_COMPS
	showInBubble(CTE,propAncestor(CTE,"TYPE","EQUI"))
else if pathType contains ["HVAC"/"BRAN"] //Must be BRAN or have BRAN ancestor
and type in HVAC_SUBCOMPS
	showInBubble(CTE,propAncestorIfUnset(CTE,"TYPE","BRAN","HVAC"))
else if pathType equals ["*"/"STRU"/"?"] //STRU must be a direct parent
	showInBubble(CTE,propAncestor(CTE,"TYPE","STRU"))
else if pathType equals ["*"/"STRU"/"EQUI"] //Must be an EQUI with parent STRU (NOTE equals instead of contains!)
	showInBubble(CTE,propAncestorIfUnset(CTE,"TYPE","EQUI","STRU"))
else if pathType contains ["STRU"/"EQUI"/"?"] //STRU and EQUI must be a direct grandparent and parent respectively
	showInBubble(CTE,propAncestor(CTE,"TYPE","STRU"))
end

alias TOME ":EDF-TOME-CTETUDES" //Temporary fix
if type in ["PIPE","EQUI","HVAC","STRU"]
	showInBubble(TOME)
else if pathType equals ["*"/"PIPE"/"BRAN"/"?"]  //PIPE and BRAN must be a direct grandparent and parent respectively
	and type in PIPE_SUBCOMPS2   //The type must be one of the ones in PIPE_SUBCOMPS2
	showInBubble(TOME,propAncestorIfUnset(TOME,"TYPE","BRAN","PIPE"))
else if pathType contains ["EQUI"/"?"] //EQUI must be an ancestor (parent or higher)
and type in EQUI_COMPS
	showInBubble(TOME,propAncestor(TOME,"TYPE","EQUI"))
else if pathType contains ["HVAC"/"BRAN"] //Must be BRAN or have BRAN ancestor
and type in HVAC_SUBCOMPS
	showInBubble(TOME,propAncestorIfUnset(TOME,"TYPE","BRAN","HVAC"))
else if pathType equals ["*"/"STRU"/"?"] //STRU must be a direct parent
	showInBubble(TOME,propAncestor(TOME,"TYPE","STRU"))
else if pathType equals ["*"/"STRU"/"EQUI"] //Must be an EQUI with parent STRU (NOTE equals instead of contains!)
	showInBubble(TOME,propAncestorIfUnset(TOME,"TYPE","EQUI","STRU"))
else if pathType contains ["STRU"/"EQUI"/"?"] //STRU and EQUI must be a direct grandparent and parent respectively
	showInBubble(TOME,propAncestor(TOME,"TYPE","STRU"))
end
// BRAVO /////////////////////////////////////////////////////////////////////

alias PIPE_REMARKABLES ["VALV","PCOM","INST","FILT","FTUB"]

if type in ["PIPE","EQUI","HVAC"]
	assemble()
	setAsRemarkable()
	if type equals "PIPE"
		addColumnAssembly("LENGTH")
		addColumnAssembly("DIAMETER")
	end

else if pathType equals ["*"/"PIPE"/"BRAN"] //BRAN with a PIPE parent
	setAsRemarkable()
	showInBubble("NAME", propAncestor("NAME","TYPE","PIPE"))

else if pathType contains ["PIPE"/"BRAN"/"?"] //PIPE and BRAN must be direct grandparent and parent respectively (or higher)
	if type equals "ATTA"
		if prop("SPREF") contains "NAAO/AA"
		or prop("SPREF") contains "NBAO/AA"
		or prop("SPREF") contains "NBBO/AA"
		or prop("SPREF") contains "NBAO/AA"
			setAsRemarkable()
		else
			showInBubble("NAME",propAncestor("NAME","TYPE","PIPE"))
		end
	else if type in PIPE_SUBCOMPS
		showInBubble("NAME",propAncestor("NAME","TYPE","PIPE"))
	else if type in PIPE_REMARKABLES //TODO: the electrical mesh items do NOT have any TYPE (type path is SITE/ZONE/PIPE/BRAN/FTUB/?).
		if name not equals "unset" and name not contains " of "
			setAsRemarkable()
			if type equals "VALV"
				showInBubble("NAME","VANNE")
			else
				showInBubble("NAME",propAncestor("NAME","TYPE","PIPE"))
			end
		else
			showInBubble("NAME")
		end
	end

else if pathType contains "EQUI"
	if type equals "NOZZ" and name not equals "unset"
		setAsRemarkable()
	else if type in EQUI_COMPS
		showInBubble("NAME",propAncestor("NAME","TYPE","EQUI"))
	else
		showInBubble("NAME")
	end

else if pathType contains ["HVAC"/"BRAN"]
	if type equals "BRAN"
		setAsRemarkable()
		showInBubble("NAME",propAncestor("NAME","TYPE","PIPE"))
	else if type in ["DAMP","SILE","ACCES PANEL"]
		if name not equals "unset" and name not contains " of "
			setAsRemarkable()
		else
			showInBubble("NAME","INCONNU")
		end
	else //if type equals "TUBI" // Same as "Autre"
		showInBubble("NAME",propAncestor("NAME","TYPE","PIPE"))
	end

else if pathType contains "STRU" //STRU must be in the path
	if type equals "STRU"
		assemble()
		setAsRemarkable()
	else if propAncestor("NAME","TYPE","SITE") contains "Supp"
	and pathType contains ["STRU"/"?"/"?"] //STRU must be a grandparent
		showInBubble("NAME",propAncestor("NAME","TYPE","STRU"))
		
	else if propAncestor("NAME","TYPE", "SITE") contains "Manut"
		if pathType equals ["*"/"STRU"/"EQUI"] //EQUI with a STRU parent
			setAsRemarkable()
		else if pathType contains ["STRU"/"EQUI"/"?"] //STRU and EQUI must be a grandparent and parent respectively
			showInBubble("NAME",propAncestor("NAME","TYPE","STRU"))
		end
	end
end

// CHARLIE /////////////////////////////////////////////////////////////////////

if pathType contains "EQUI" and type in EQUI_COMPS
	//TODO: this rule seems to be necessary to ensure that named groups are visible when parent is assembled
	if type equals "SUBE" and prop("NAME") not equals "" //TODO: should it instead check the pathType to ensure it's NOT a leaf?
		setAsRemarkable()
	end
	
	//If this object is an EQUI type, only show the DESC also if the ancestor SITE contains "DMOL" in name.
	if type equals "EQUI"
		if propAncestor("NAME","TYPE","SITE") contains "DMOL"
			showInBubble("DESC")
		end
	else //Otherwise, this object has an ancestor EQUI and inherit its DESC.
		showInBubble("DESC",propAncestor("DESC","TYPE","EQUI"))
	end
end
