/*
 * Go2PartStudTipZ.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/zGo2PartStudTipZ.h>
#include <z/zGo2PartTypes.h>
#include <z/zGo2System.h>
#include <string.h>

/*
 * Go2PartStudTipZ Class 
 */

GO2_EXPORT(Go2Status) Go2PartStudTipZ_Construct(Go2PartStudTipZ* measurement, Go2UInt32 id, Go2Object system)
{
    Go2PartStudTipZ output = zNIL; 
    Go2Status status = zOK; 

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

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

    *measurement = output; 

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_Init(Go2PartStudTipZ measurement, Go2UInt32 id, Go2Object system)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);
    z32u i; 

    zCHECK(Go2PartMeasurement_Init(measurement, GO2_TYPE_PART_STUD_TIP_Z, id, system)); 

    obj->Release = Go2PartStudTipZ_VRelease;
    obj->Read = Go2PartStudTipZ_VRead; 
    obj->Write = Go2PartStudTipZ_VWrite; 

    obj->studRadius = 5.0;
    obj->studHeight = 20.0;
    obj->baseHeight = 0.0;
    obj->tipHeight = 0.0;

    for (i = 0; i < zCOUNT(obj->refRegions); ++i)
    {
        zCHECK(Go2PartRegion2d_Construct(&obj->refRegions[i], system));
    }
    //construct all ref areas, but initialize the measurement to only use 2
    obj->refRegionCount = 2;

    obj->refRegionsEnabled = GO2_FALSE;
    obj->autoRefRegionEnabled = GO2_TRUE;
    obj->autoTiltCorrectionEnabled = GO2_TRUE;
    obj->tiltXAngle = 0.0;
    obj->tiltYAngle = 0.0;

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_VRelease(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);
    z32u i; 

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

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

GO2_EXPORT(Go2Status) Go2PartStudTipZ_VRead(Go2PartStudTipZ measurement, zXml xml, zXmlItem item)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);
    zXmlItem regionItem = zNIL;

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

    obj->refRegionCount = 0; 
    //ensure we at least have one refArea
    zCHECK(!zIS_NIL(regionItem = zXml_Child(xml, item, "RefRegions/RefRegion")));     
    while (!zIS_NIL(regionItem))
    {
        zCHECK(Go2PartRegion2d_Read(obj->refRegions[obj->refRegionCount], xml, regionItem));  
        obj->refRegionCount++;  
        regionItem = zXml_NextSibling(xml, regionItem);
    }

    zCHECK(zXml_Child64f(xml, item, "StudRadius", &obj->studRadius)); 
    zCHECK(zXml_Child64f(xml, item, "StudHeight", &obj->studHeight)); 
    zCHECK(zXml_Child64f(xml, item, "BaseHeight", &obj->baseHeight)); 
    zCHECK(zXml_Child64f(xml, item, "TipHeight", &obj->tipHeight)); 

    zCHECK(zXml_Child32s(xml, item, "RefRegionsEnabled", &obj->refRegionsEnabled));
    zCHECK(zXml_Child32s(xml, item, "AutoRefRegionEnabled", &obj->autoRefRegionEnabled));
    zCHECK(zXml_Child32s(xml, item, "AutoTiltCorrectionEnabled", &obj->autoTiltCorrectionEnabled));
    zCHECK(zXml_Child64f(xml, item, "TiltXAngle", &obj->tiltXAngle)); 
    zCHECK(zXml_Child64f(xml, item, "TiltYAngle", &obj->tiltYAngle)); 

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_VWrite(Go2PartStudTipZ  measurement, zXml xml, zXmlItem item)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);
    zXmlItem regionItem = zNIL;
    zXmlItem refRegionItem = zNIL;
    z32u i; 

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

    zCHECK(zXml_AddItem(xml, item, "RefRegions", &refRegionItem));  
    for (i = 0; i < obj->refRegionCount; ++i)
    {
        zCHECK(zXml_AddItem(xml, refRegionItem, "RefRegion", &regionItem)); 
        zCHECK(Go2PartRegion2d_Write(obj->refRegions[i], xml, regionItem)); 
    }

    zCHECK(zXml_SetChild64f(xml, item, "StudRadius", obj->studRadius)); 
    zCHECK(zXml_SetChild64f(xml, item, "StudHeight", obj->studHeight)); 
    zCHECK(zXml_SetChild64f(xml, item, "BaseHeight", obj->baseHeight)); 
    zCHECK(zXml_SetChild64f(xml, item, "TipHeight", obj->tipHeight)); 

    zCHECK(zXml_SetChild32s(xml, item, "RefRegionsEnabled", obj->refRegionsEnabled)); 
    zCHECK(zXml_SetChild32s(xml, item, "AutoRefRegionEnabled", obj->autoRefRegionEnabled)); 
    zCHECK(zXml_SetChild32s(xml, item, "AutoTiltCorrectionEnabled", obj->autoTiltCorrectionEnabled)); 
    zCHECK(zXml_SetChild64f(xml, item, "TiltXAngle", obj->tiltXAngle)); 
    zCHECK(zXml_SetChild64f(xml, item, "TiltYAngle", obj->tiltYAngle)); 

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetId(Go2PartStudTipZ measurement, Go2UInt32 id)
{
    return Go2Measurement_SetId(measurement, id); 
}

GO2_EXPORT(Go2UInt32) Go2PartStudTipZ_Id(Go2PartStudTipZ measurement)
{
    return Go2Measurement_Id(measurement); 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetName(Go2PartStudTipZ measurement, const Go2Char* name)
{
    return Go2Measurement_SetName(measurement, name); 
}

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

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetDecisionMin(Go2PartStudTipZ measurement, Go2Double min)
{
    return Go2Measurement_SetDecisionMin(measurement, min); 
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_DecisionMin(Go2PartStudTipZ measurement)
{
    return Go2Measurement_DecisionMin(measurement); 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetDecisionMax(Go2PartStudTipZ measurement, Go2Double max)
{
    return Go2Measurement_SetDecisionMax(measurement, max); 
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_DecisionMax(Go2PartStudTipZ measurement)
{
    return Go2Measurement_DecisionMax(measurement); 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_EnableHold(Go2PartStudTipZ measurement, Go2Bool enable)
{
    return Go2Measurement_EnableHold(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2PartStudTipZ_HoldEnabled(Go2PartStudTipZ measurement)
{
    return Go2Measurement_HoldEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_EnableSmoothing(Go2PartStudTipZ measurement, Go2Bool enable)
{
    return Go2Measurement_EnableSmoothing(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2PartStudTipZ_SmoothingEnabled(Go2PartStudTipZ measurement)
{
    return Go2Measurement_SmoothingEnabled(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2PartStudTipZ_SmoothingWindowMin(Go2PartStudTipZ measurement)
{
    return Go2Measurement_SmoothingWindowMin(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2PartStudTipZ_SmoothingWindowMax(Go2PartStudTipZ measurement)
{
    return Go2Measurement_SmoothingWindowMax(measurement);  
} 

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetSmoothingWindow(Go2PartStudTipZ measurement, Go2UInt32 window)
{
    return Go2Measurement_SetSmoothingWindow(measurement, window);  
} 

GO2_EXPORT(Go2UInt32) Go2PartStudTipZ_SmoothingWindow(Go2PartStudTipZ measurement)
{
    return Go2Measurement_SmoothingWindow(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2PartStudTipZ_RefRegionCount(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 
    return obj->refRegionCount;      
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetRefRegionCount(Go2PartStudTipZ measurement, Go2UInt32 count)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 
    Go2Status status = GO2_OK;

    if(count > zCOUNT(obj->refRegions)){
        return GO2_ERROR_PARAMETER;
    }
    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    status = Go2System_Set32u(obj->system, &obj->refRegionCount, count, 1, GO2_MAX_REF_REGIONS); 
    zCHECK(Go2System_SetModified(obj->system)); 

    return status;      
}

GO2_EXPORT(Go2PartRegion2d) Go2PartStudTipZ_RefRegionAt(Go2PartStudTipZ measurement, Go2UInt32 index)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 
    return obj->refRegions[index];      
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_StudRadius(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 
    return obj->studRadius;      
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetStudRadius(Go2PartStudTipZ measurement, Go2Double radius)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 

    return Go2System_Set64f(obj->system, &obj->studRadius, radius, 0.0, z64F_MAX);
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_StudHeight(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 
    return obj->studHeight;      
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetStudHeight(Go2PartStudTipZ measurement, Go2Double height)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 

    return Go2System_Set64f(obj->system, &obj->studHeight, height, 0.0, z64F_MAX); 
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_BaseHeight(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 
    return obj->baseHeight;      
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetBaseHeight(Go2PartStudTipZ measurement, Go2Double height)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 

    return Go2System_Set64f(obj->system, &obj->baseHeight, height, 0.0, z64F_MAX);
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_TipHeight(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 
    return obj->tipHeight;      
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetTipHeight(Go2PartStudTipZ measurement, Go2Double height)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 

    return Go2System_Set64f(obj->system, &obj->tipHeight, height, 0.0, z64F_MAX);     
}

GO2_EXPORT(Go2Bool) Go2PartStudTipZ_AutoRefRegionEnabled(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);
    return obj->autoRefRegionEnabled; 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_EnableAutoRefRegion(Go2PartStudTipZ measurement, Go2Bool enable)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->autoRefRegionEnabled = enable; 
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK;
}

GO2_EXPORT(Go2Bool) Go2PartStudTipZ_AutoTiltCorrectionEnabled(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);
    return obj->autoTiltCorrectionEnabled; 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_EnableAutoTiltCorrection(Go2PartStudTipZ measurement, Go2Bool enable)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->autoTiltCorrectionEnabled = enable; 
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK;
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_TiltXAngle(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);
    return obj->tiltXAngle; 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetTiltXAngle(Go2PartStudTipZ measurement, Go2Double tiltXAngle)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 

    return Go2System_Set64f(obj->system, &obj->tiltXAngle, tiltXAngle, -z64F_MAX, z64F_MAX);   
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_TiltYAngle(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);
    return obj->tiltYAngle; 
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetTiltYAngle(Go2PartStudTipZ measurement, Go2Double tiltYAngle)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement); 

    return Go2System_Set64f(obj->system, &obj->tiltYAngle, tiltYAngle, -z64F_MAX, z64F_MAX);   
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetScale(Go2PartStudTipZ measurement, Go2Double scale){
    return Go2Measurement_SetScale(measurement, scale);
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_Scale(Go2PartStudTipZ measurement){
    return Go2Measurement_Scale(measurement);
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetOffset(Go2PartStudTipZ measurement, Go2Double offset){
    return Go2Measurement_SetOffset(measurement, offset);
}

GO2_EXPORT(Go2Double) Go2PartStudTipZ_Offset(Go2PartStudTipZ measurement){
    return Go2Measurement_Offset(measurement);
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_EnableLink(Go2PartStudTipZ measurement, Go2Bool enable)  
{
    return Go2Measurement_EnableLink(measurement, enable);  
}

GO2_EXPORT(Go2Bool) Go2PartStudTipZ_LinkEnabled(Go2PartStudTipZ measurement)  
{
    return Go2Measurement_LinkEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_SetLink(Go2PartStudTipZ measurement, Go2UInt32 link) 
{
    return Go2Measurement_SetLink(measurement, link); 
}

GO2_EXPORT(Go2UInt32) Go2PartStudTipZ_Link(Go2PartStudTipZ measurement) 
{
    return Go2Measurement_Link(measurement); 
}

GO2_EXPORT(Go2PartRegion3d) Go2PartStudTipZ_Region(Go2PartStudTipZ measurement)
{
    return Go2PartMeasurement_Region(measurement);
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_EnableRegion(Go2PartStudTipZ measurement, Go2Bool enable)  
{
    return Go2PartMeasurement_EnableRegion(measurement, enable);  
}

GO2_EXPORT(Go2Bool) Go2PartStudTipZ_RegionEnabled(Go2PartStudTipZ measurement)  
{
    return Go2PartMeasurement_RegionEnabled(measurement);  
}

GO2_EXPORT(Go2Bool) Go2PartStudTipZ_RefRegionsEnabled(Go2PartStudTipZ measurement)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);

    return obj->refRegionsEnabled;
}

GO2_EXPORT(Go2Status) Go2PartStudTipZ_EnableRefRegions(Go2PartStudTipZ measurement, Go2Bool enable)
{
    Go2PartStudTipZClass* obj = zHANDLE_POINTER(measurement);

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->refRegionsEnabled = enable;
    zCHECK(Go2System_SetModified(obj->system));

    return GO2_OK;
}

GO2_EXPORT(Go2UInt32) Go2PartStudTipZ_LinkOptionCount(Go2PartStudTipZ measurement)
{
    return Go2Measurement_LinkOptionCount(measurement);
}

GO2_EXPORT(Go2UInt32) Go2PartStudTipZ_LinkOptionAt(Go2PartStudTipZ measurement, Go2UInt32 index)
{
    return Go2Measurement_LinkOptionAt(measurement, index);
}
