/*
 * Go2ProfileGap.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/zGo2ProfileGap.h>
#include <z/zGo2ProfileTypes.h>
#include <z/zGo2System.h>
#include <string.h>

/*
 * Go2ProfileGap Class 
 */


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

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

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

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileGap_Init(Go2ProfileGap measurement, Go2UInt32 id, Go2Object system)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement);

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

    obj->Release = Go2ProfileGap_VRelease; 
    obj->Read = Go2ProfileGap_VRead; 
    obj->Write = Go2ProfileGap_VWrite; 

    zCHECK(Go2ProfileEdge_Construct(&obj->leftEdge, system));   
    zCHECK(Go2ProfileEdge_Construct(&obj->rightEdge, system));   

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileGap_VRelease(Go2ProfileGap measurement)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement);

    zCHECK(Go2Object_Destroy(obj->leftEdge)); 
    zCHECK(Go2Object_Destroy(obj->rightEdge)); 

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

GO2_EXPORT(Go2Status) Go2ProfileGap_VRead(Go2ProfileGap measurement, zXml xml, zXmlItem item)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 
    zChar* text = zNULL;
    z32u textCapacity = GO2_MEASUREMENT_SOURCE_TEXT_CAPACITY; 
    zXmlItem edgeItem = zNIL; 

    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_Child32s(xml, item, "Source", (z32s*) &obj->source)); 
    zCHECK(zXml_Child32s(xml, item, "ReferenceSide", (z32s*) &obj->referenceSide)); 
    zCHECK(zXml_Child64f(xml, item, "GapWidthMax", &obj->gapWidthMax)); 
    zCHECK(zXml_Child32s(xml, item, "MeasurementAxis", (z32s*) &obj->axis)); 

    edgeItem = zXml_Child(xml, item, "LeftSide");
    zCHECK(!zIS_NIL(edgeItem));
    zCHECK(Go2ProfileEdge_Read(obj->leftEdge, xml, edgeItem)); 

    edgeItem = zXml_Child(xml, item, "RightSide");
    zCHECK(!zIS_NIL(edgeItem));
    zCHECK(Go2ProfileEdge_Read(obj->rightEdge, xml, edgeItem)); 

    zCHECK(zFree(text));

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileGap_VWrite(Go2ProfileGap measurement, zXml xml, zXmlItem item)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 
    zXmlItem edgeItem = zNIL; 

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

    zCHECK(zXml_SetChild32u(xml, item, "Source", obj->source)); 
    zCHECK(zXml_SetChild32s(xml, item, "ReferenceSide", obj->referenceSide)); 
    zCHECK(zXml_SetChild64f(xml, item, "GapWidthMax", obj->gapWidthMax)); 
    zCHECK(zXml_SetChild32s(xml, item, "MeasurementAxis", obj->axis)); 

    zCHECK(zXml_AddItem(xml, item, "LeftSide", &edgeItem)); 
    zCHECK(Go2ProfileEdge_Write(obj->leftEdge, xml, edgeItem)); 

    zCHECK(zXml_AddItem(xml, item, "RightSide", &edgeItem)); 
    zCHECK(Go2ProfileEdge_Write(obj->rightEdge, xml, edgeItem)); 
    
    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetId(Go2ProfileGap measurement, Go2UInt32 id)
{
    return Go2Measurement_SetId(measurement, id);     
}

GO2_EXPORT(Go2UInt32) Go2ProfileGap_Id(Go2ProfileGap measurement)
{
    return Go2Measurement_Id(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetName(Go2ProfileGap measurement, const Go2Char* name)
{
    return Go2Measurement_SetName(measurement, name);     
}

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

GO2_EXPORT(Go2UInt32) Go2ProfileGap_SourceOptionCount(Go2ProfileGap measurement)
{
    return Go2Measurement_SourceOptionCount(measurement);     
}

GO2_EXPORT(Go2ProfileSource) Go2ProfileGap_SourceOptionAt(Go2ProfileGap measurement, Go2UInt32 index)
{
    return Go2Measurement_SourceOptionAt(measurement, index);     
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetSource(Go2ProfileGap measurement, Go2ProfileSource source)
{
    return Go2Measurement_SetSource(measurement, source);     
}

GO2_EXPORT(Go2ProfileSource) Go2ProfileGap_Source(Go2ProfileGap measurement)
{
    return Go2Measurement_Source(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetDecisionMin(Go2ProfileGap measurement, Go2Double min)
{
    return Go2Measurement_SetDecisionMin(measurement, min);     
}

GO2_EXPORT(Go2Double) Go2ProfileGap_DecisionMin(Go2ProfileGap measurement)
{
    return Go2Measurement_DecisionMin(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetDecisionMax(Go2ProfileGap measurement, Go2Double max)
{
    return Go2Measurement_SetDecisionMax(measurement, max);     
}

GO2_EXPORT(Go2Double) Go2ProfileGap_DecisionMax(Go2ProfileGap measurement)
{
    return Go2Measurement_DecisionMax(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileGap_EnableHold(Go2ProfileGap measurement, Go2Bool enable)
{
    return Go2Measurement_EnableHold(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2ProfileGap_HoldEnabled(Go2ProfileGap measurement)
{
    return Go2Measurement_HoldEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2ProfileGap_EnableSmoothing(Go2ProfileGap measurement, Go2Bool enable)
{
    return Go2Measurement_EnableSmoothing(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2ProfileGap_SmoothingEnabled(Go2ProfileGap measurement)
{
    return Go2Measurement_SmoothingEnabled(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileGap_SmoothingWindowMin(Go2ProfileGap measurement)
{
    return Go2Measurement_SmoothingWindowMin(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileGap_SmoothingWindowMax(Go2ProfileGap measurement)
{
    return Go2Measurement_SmoothingWindowMax(measurement);  
} 

GO2_EXPORT(Go2Status) Go2ProfileGap_SetSmoothingWindow(Go2ProfileGap measurement, Go2UInt32 window)
{
    return Go2Measurement_SetSmoothingWindow(measurement, window);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileGap_SmoothingWindow(Go2ProfileGap measurement)
{
    return Go2Measurement_SmoothingWindow(measurement);  
} 

GO2_EXPORT(Go2ProfileEdge) Go2ProfileGap_LeftEdge(Go2ProfileGap measurement)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 
    return obj->leftEdge;
}

GO2_EXPORT(Go2ProfileEdge) Go2ProfileGap_RightEdge(Go2ProfileGap measurement)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 
    return obj->rightEdge;
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetReferenceSide(Go2ProfileGap measurement, Go2Side side)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->referenceSide = side; 
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK; 
}
 
GO2_EXPORT(Go2Side) Go2ProfileGap_ReferenceSide(Go2ProfileGap measurement)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 
    return obj->referenceSide;   
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetAxis(Go2ProfileGap measurement, Go2GapAxis axis)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 

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

    return GO2_OK; 
}
 
GO2_EXPORT(Go2GapAxis) Go2ProfileGap_Axis(Go2ProfileGap measurement)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 
    return obj->axis;   
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetGapWidthMax(Go2ProfileGap measurement, Go2Double gapWidthMax)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 

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

    return GO2_OK; 
}
 
GO2_EXPORT(Go2Double) Go2ProfileGap_GapWidthMax(Go2ProfileGap measurement)
{
    Go2ProfileGapClass* obj = zHANDLE_POINTER(measurement); 
    return obj->gapWidthMax;   
}



GO2_EXPORT(Go2Status) Go2ProfileGap_SetScale(Go2ProfileGap measurement, Go2Double scale){
    return Go2Measurement_SetScale(measurement, scale);
}

GO2_EXPORT(Go2Double) Go2ProfileGap_Scale(Go2ProfileGap measurement){
    return Go2Measurement_Scale(measurement);
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetOffset(Go2ProfileGap measurement, Go2Double offset){
    return Go2Measurement_SetOffset(measurement, offset);
}

GO2_EXPORT(Go2Double) Go2ProfileGap_Offset(Go2ProfileGap measurement){
    return Go2Measurement_Offset(measurement);
}

GO2_EXPORT(Go2UInt32) Go2ProfileGap_LinkOptionCount(Go2ProfileGap measurement)
{
    return Go2Measurement_LinkOptionCount(measurement);
}

GO2_EXPORT(Go2UInt32) Go2ProfileGap_LinkOptionAt(Go2ProfileGap measurement, Go2UInt32 index)
{
    return Go2Measurement_LinkOptionAt(measurement, index);
}

GO2_EXPORT(Go2Status) Go2ProfileGap_EnableLink(Go2ProfileGap measurement, Go2Bool enable)  
{
    return Go2Measurement_EnableLink(measurement, enable);  
}

GO2_EXPORT(Go2Bool) Go2ProfileGap_LinkEnabled(Go2ProfileGap measurement)  
{
    return Go2Measurement_LinkEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2ProfileGap_SetLink(Go2ProfileGap measurement, Go2UInt32 link) 
{
    return Go2Measurement_SetLink(measurement, link); 
}

GO2_EXPORT(Go2UInt32) Go2ProfileGap_Link(Go2ProfileGap measurement) 
{
    return Go2Measurement_Link(measurement); 
}
