// ------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool Xbim.CodeGeneration 
//  
//     Changes to this file may cause incorrect behaviour and will be lost if
//     the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------

using Xbim.Ifc2x3.GeometricConstraintResource;
using Xbim.Ifc2x3.RepresentationResource;
using System;
using System.Collections.Generic;
using System.Linq;
using Xbim.Common;
using Xbim.Common.Exceptions;
using Xbim.Ifc2x3.Kernel;
//## Custom using statements
using Xbim.Ifc2x3.ProductExtension;
//##


namespace Xbim.Ifc2x3.Kernel
{
	[ExpressType("IfcProduct", 20)]
	// ReSharper disable once PartialTypeWithSinglePart
	public abstract partial class @IfcProduct : IfcObject, IEquatable<@IfcProduct>
	{

		//internal constructor makes sure that objects are not created outside of the model/ assembly controlled area
		internal IfcProduct(IModel model, int label, bool activated) : base(model, label, activated)  
		{
		}

		#region Explicit attribute fields
		private IfcObjectPlacement _objectPlacement;
		private IfcProductRepresentation _representation;
		#endregion
	
		#region Explicit attribute properties
		[IndexedProperty]
		[EntityAttribute(6, EntityAttributeState.Optional, EntityAttributeType.Class, EntityAttributeType.None, null, null, 11)]
		public IfcObjectPlacement @ObjectPlacement 
		{ 
			get 
			{
				if(_activated) return _objectPlacement;
				Activate();
				return _objectPlacement;
			} 
			set
			{
				if (value != null && !(ReferenceEquals(Model, value.Model)))
					throw new XbimException("Cross model entity assignment.");
				SetValue( v =>  _objectPlacement = v, _objectPlacement, value,  "ObjectPlacement", 6);
			} 
		}	
		[IndexedProperty]
		[EntityAttribute(7, EntityAttributeState.Optional, EntityAttributeType.Class, EntityAttributeType.None, null, null, 12)]
		public IfcProductRepresentation @Representation 
		{ 
			get 
			{
				if(_activated) return _representation;
				Activate();
				return _representation;
			} 
			set
			{
				if (value != null && !(ReferenceEquals(Model, value.Model)))
					throw new XbimException("Cross model entity assignment.");
				SetValue( v =>  _representation = v, _representation, value,  "Representation", 7);
			} 
		}	
		#endregion



		#region Inverse attributes
		[InverseProperty("RelatingProduct")]
		[EntityAttribute(-1, EntityAttributeState.Mandatory, EntityAttributeType.Set, EntityAttributeType.Class, new int [] { 0 }, new int [] { -1 }, 13)]
		public IEnumerable<IfcRelAssignsToProduct> @ReferencedBy 
		{ 
			get 
			{
				return Model.Instances.Where<IfcRelAssignsToProduct>(e => Equals(e.RelatingProduct), "RelatingProduct", this);
			} 
		}
		#endregion

		#region IPersist implementation
		public override void Parse(int propIndex, IPropertyValue value, int[] nestedIndex)
		{
			switch (propIndex)
			{
				case 0: 
				case 1: 
				case 2: 
				case 3: 
				case 4: 
					base.Parse(propIndex, value, nestedIndex); 
					return;
				case 5: 
					_objectPlacement = (IfcObjectPlacement)(value.EntityVal);
					return;
				case 6: 
					_representation = (IfcProductRepresentation)(value.EntityVal);
					return;
				default:
					throw new XbimParserException(string.Format("Attribute index {0} is out of range for {1}", propIndex + 1, GetType().Name.ToUpper()));
			}
		}
		#endregion

		#region Equality comparers and operators
        public bool Equals(@IfcProduct other)
	    {
	        return this == other;
	    }
        #endregion

		#region Custom code (will survive code regeneration)
		//## Custom code
        public Xbim.Ifc4.Interfaces.IIfcSpatialElement IsContainedIn
        {
            get
            {
                return Model.Instances.Where<IfcRelContainedInSpatialStructure>(r => r.RelatedElements.Contains(this)).Select(s => s.RelatingStructure).FirstOrDefault();
            }
        }
		//##
		#endregion
	}
}