/*
 * Go2ProfileWidth.c
 * 
 * Gocator system communication library
 * Copyright (C) 2013 by LMI Technologies Inc.
 * 
 * Licensed under The MIT License.
 * Redistributions of files must retain the above copyright notice.
 */

#include <z/Go2Measurements/zGo2ProfileWidth.h>
#include <z/zGo2ProfileTypes.h>
#include <z/zGo2System.h>
#include <string.h>

/*
 * Go2ProfileWidth Class 
 */

GO2_EXPORT(Go2Status) Go2ProfileWidth_Construct(Go2ProfileWidth* measurement, Go2UInt32 id, Go2Object system)
{
    Go2ProfileWidth output = zNIL; 
    Go2Status status = GO2_OK; 

    zCHECK(zCalloc(&output, 1, sizeof(Go2ProfileWidthClass))); 

    if (!zSUCCESS(status = Go2ProfileWidth_Init(output, id, system)))
    {
        zCHECK(Go2Object_Destroy(output)); 
        return status; 
    }
    
    *measurement = output; 

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_Init(Go2ProfileWidth measurement, Go2UInt32 id, Go2Object system)
{
    Go2ProfileWidthClass* obj = zHANDLE_POINTER(measurement);
    z32u i; 

    zCHECK(Go2Measurement_Init(measurement, GO2_TYPE_PROFILE_WIDTH, id, system)); 

    obj->Release = Go2ProfileWidth_VRelease; 
    obj->Read = Go2ProfileWidth_VRead; 
    obj->Write = Go2ProfileWidth_VWrite; 

    for (i = 0; i < zCOUNT(obj->features); ++i)
    {
        zCHECK(Go2ProfileFeature_Construct(&obj->features[i], system));       
    }

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_VRelease(Go2ProfileWidth measurement)
{
    Go2ProfileWidthClass* obj = zHANDLE_POINTER(measurement);
    z32u i; 

    for (i = 0; i < zCOUNT(obj->features); ++i)
    {
        zCHECK(Go2Object_Destroy(obj->features[i])); 
    }

    zCHECK(Go2Measurement_VRelease(measurement));  
        
    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_VRead(Go2ProfileWidth measurement, zXml xml, zXmlItem item)
{
    Go2ProfileWidthClass* obj = zHANDLE_POINTER(measurement); 
    zXmlItem featureItem = zNIL; 
    zChar* text = zNULL;
    z32u textCapacity = GO2_MEASUREMENT_SOURCE_TEXT_CAPACITY; 

    z32u i; 

    zCHECK(Go2Measurement_VRead(measurement, xml, item)); 
    
    zCHECK(zCalloc(&text, textCapacity, sizeof(zChar)));
    zCHECK(zXml_ChildText(xml, item, "SourceOptions", text, textCapacity)); 
    zCHECK(Go2OptionList_ParseList(text, obj->sourceOptions));  

    zCHECK(zXml_Child32u(xml, item, "Source", &obj->source)); 
    

    featureItem = zXml_Child(xml, item, "Feature");  

    for (i = 0; i < zCOUNT(obj->features); ++i)
    {
        zCHECK(!zIS_NIL(featureItem)); 
        zCHECK(Go2ProfileFeature_Read(obj->features[i], xml, featureItem)); 

        featureItem = zXml_NextSibling(xml, featureItem);  
    }

    zCHECK(zFree(text));

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_VWrite(Go2ProfileWidth measurement, zXml xml, zXmlItem item)
{
    Go2ProfileWidthClass* obj = zHANDLE_POINTER(measurement); 
    zXmlItem featureItem = zNIL; 
    z32u i; 

    zCHECK(Go2Measurement_VWrite(measurement, xml, item));

    zCHECK(zXml_SetChild32u(xml, item, "Source", obj->source)); 

    for (i = 0; i < zCOUNT(obj->features); ++i)
    {
        zCHECK(zXml_AddItem(xml, item, "Feature", &featureItem)); 
        zCHECK(Go2ProfileFeature_Write(obj->features[i], xml, featureItem)); 
    }

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_SetId(Go2ProfileWidth measurement, Go2UInt32 id)
{
    return Go2Measurement_SetId(measurement, id);     
}

GO2_EXPORT(Go2UInt32) Go2ProfileWidth_Id(Go2ProfileWidth measurement)
{
    return Go2Measurement_Id(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_SetName(Go2ProfileWidth measurement, const Go2Char* name)
{
    return Go2Measurement_SetName(measurement, name);     
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_Name(Go2ProfileWidth measurement, Go2Char* name, Go2UInt32 capacity)
{
    return Go2Measurement_Name(measurement, name, capacity);     
}

GO2_EXPORT(Go2UInt32) Go2ProfileWidth_SourceOptionCount(Go2ProfileWidth measurement)
{
    return Go2Measurement_SourceOptionCount(measurement);     
}

GO2_EXPORT(Go2ProfileSource) Go2ProfileWidth_SourceOptionAt(Go2ProfileWidth measurement, Go2UInt32 index)
{
    return Go2Measurement_SourceOptionAt(measurement, index);     
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_SetSource(Go2ProfileWidth measurement, Go2ProfileSource source)
{
    return Go2Measurement_SetSource(measurement, source);     
}

GO2_EXPORT(Go2ProfileSource) Go2ProfileWidth_Source(Go2ProfileWidth measurement)
{
    return Go2Measurement_Source(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_SetDecisionMin(Go2ProfileWidth measurement, Go2Double min)
{
    return Go2Measurement_SetDecisionMin(measurement, min);     
}

GO2_EXPORT(Go2Double) Go2ProfileWidth_DecisionMin(Go2ProfileWidth measurement)
{
    return Go2Measurement_DecisionMin(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_SetDecisionMax(Go2ProfileWidth measurement, Go2Double max)
{
    return Go2Measurement_SetDecisionMax(measurement, max);     
}

GO2_EXPORT(Go2Double) Go2ProfileWidth_DecisionMax(Go2ProfileWidth measurement)
{
    return Go2Measurement_DecisionMax(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_EnableHold(Go2ProfileWidth measurement, Go2Bool enable)
{
    return Go2Measurement_EnableHold(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2ProfileWidth_HoldEnabled(Go2ProfileWidth measurement)
{
    return Go2Measurement_HoldEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_EnableSmoothing(Go2ProfileWidth measurement, Go2Bool enable)
{
    return Go2Measurement_EnableSmoothing(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2ProfileWidth_SmoothingEnabled(Go2ProfileWidth measurement)
{
    return Go2Measurement_SmoothingEnabled(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileWidth_SmoothingWindowMin(Go2ProfileWidth measurement)
{
    return Go2Measurement_SmoothingWindowMin(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileWidth_SmoothingWindowMax(Go2ProfileWidth measurement)
{
    return Go2Measurement_SmoothingWindowMax(measurement);  
} 

GO2_EXPORT(Go2Status) Go2ProfileWidth_SetSmoothingWindow(Go2ProfileWidth measurement, Go2UInt32 window)
{
    return Go2Measurement_SetSmoothingWindow(measurement, window);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileWidth_SmoothingWindow(Go2ProfileWidth measurement)
{
    return Go2Measurement_SmoothingWindow(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileWidth_FeatureCount(Go2ProfileWidth measurement)
{
    Go2ProfileWidthClass* obj = zHANDLE_POINTER(measurement); 
    return zCOUNT(obj->features); 
}

GO2_EXPORT(Go2ProfileFeature) Go2ProfileWidth_FeatureAt(Go2ProfileWidth measurement, Go2UInt32 index)
{
    Go2ProfileWidthClass* obj = zHANDLE_POINTER(measurement); 
    return obj->features[index];  
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_SetAbsoluteResult(Go2ProfileWidth measurement, Go2Bool absoluteResult)
{
    Go2ProfileWidthClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->absoluteResult = absoluteResult; 
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK; 
}
 
GO2_EXPORT(Go2Bool) Go2ProfileWidth_AbsoluteResult(Go2ProfileWidth measurement)
{
    Go2ProfileWidthClass* obj = zHANDLE_POINTER(measurement); 
    return obj->absoluteResult;   
}



GO2_EXPORT(Go2Status) Go2ProfileWidth_SetScale(Go2ProfileWidth measurement, Go2Double scale){
    return Go2Measurement_SetScale(measurement, scale);
}

GO2_EXPORT(Go2Double) Go2ProfileWidth_Scale(Go2ProfileWidth measurement){
    return Go2Measurement_Scale(measurement);
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_SetOffset(Go2ProfileWidth measurement, Go2Double offset){
    return Go2Measurement_SetOffset(measurement, offset);
}

GO2_EXPORT(Go2Double) Go2ProfileWidth_Offset(Go2ProfileWidth measurement){
    return Go2Measurement_Offset(measurement);
}

GO2_EXPORT(Go2UInt32) Go2ProfileWidth_LinkOptionCount(Go2ProfileWidth measurement)
{
    return Go2Measurement_LinkOptionCount(measurement);
}

GO2_EXPORT(Go2UInt32) Go2ProfileWidth_LinkOptionAt(Go2ProfileWidth measurement, Go2UInt32 index)
{
    return Go2Measurement_LinkOptionAt(measurement, index);
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_EnableLink(Go2ProfileWidth measurement, Go2Bool enable)  
{
    return Go2Measurement_EnableLink(measurement, enable);  
}

GO2_EXPORT(Go2Bool) Go2ProfileWidth_LinkEnabled(Go2ProfileWidth measurement)  
{
    return Go2Measurement_LinkEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2ProfileWidth_SetLink(Go2ProfileWidth measurement, Go2UInt32 link) 
{
    return Go2Measurement_SetLink(measurement, link); 
}

GO2_EXPORT(Go2UInt32) Go2ProfileWidth_Link(Go2ProfileWidth measurement) 
{
    return Go2Measurement_Link(measurement); 
}
