// ------------------------------------------------------------------------------
// <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.Common;
using Xbim.Common.Exceptions;

namespace Xbim.Ifc4x3.MeasureResource
{
	[ExpressType("IfcBinary", 986)]
	[DefinedType(typeof(byte[]))]
    // ReSharper disable once PartialTypeWithSinglePart
	public partial struct IfcBinary : IfcSimpleValue, IExpressValueType, IExpressBinaryType, System.IEquatable<byte[]>
	{ 
		private byte[] _value;
        
		public object Value
        {
            get { return _value; }
        }

 
		byte[] IExpressBinaryType.Value { get { return _value; } }

		public override string ToString()
        {
			if (_value == null)
				return "";
            var hex = new System.Text.StringBuilder(_value.Length * 2);
            foreach (byte b in _value)
                hex.AppendFormat("{0:X2}", b);
            return hex.ToString();
        }
        public IfcBinary(byte[] val)
        {
            _value = val;
        }

		public IfcBinary(string val)
        {
			var hex = val.Trim('"').Substring(1); //trim eventual leading and ending apostrophe and leading offset number
			int numChars = hex.Length;
            _value = new byte[numChars / 2]; 
            for (int i = 0; i < numChars; i += 2)
                _value[i / 2] = System.Convert.ToByte(hex.Substring(i, 2), 16);
        }

        public static implicit operator IfcBinary(byte[] value)
        {
            return new IfcBinary(value);
        }

        public static implicit operator byte[](IfcBinary obj)
        {
            return obj._value;

        }


        public override bool Equals(object obj)
        {
			if (obj == null && Value == null)
                return true;

            if (obj == null)
                return false;

            if (GetType() != obj.GetType())
                return false;

            return ((IfcBinary) obj)._value == _value;
        }

		public bool Equals(byte[] other)
	    {
	        return this == other;
	    }

        public static bool operator ==(IfcBinary obj1, IfcBinary obj2)
        {
            return Equals(obj1, obj2);
        }

        public static bool operator !=(IfcBinary obj1, IfcBinary obj2)
        {
            return !Equals(obj1, obj2);
        }

        public override int GetHashCode()
        {
            return Value != null ? _value.GetHashCode() : base.GetHashCode();
        }

		#region IPersist implementation
		void IPersist.Parse(int propIndex, IPropertyValue value, int[] nestedIndex)
		{
			if (propIndex != 0)
				throw new XbimParserException(string.Format("Attribute index {0} is out of range for {1}", propIndex + 1, GetType().Name.ToUpper()));
            _value = value.HexadecimalVal;
            
		}
		#endregion

		#region IExpressValueType implementation
        System.Type IExpressValueType.UnderlyingSystemType { 
			get 
			{
				return typeof(byte[]);
			}
		}
		#endregion


	}
}
