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

/*
 * Go2PartPlaneXAngle Class 
 */

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

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

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

    *measurement = output; 

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_Init(Go2PartPlaneXAngle measurement, Go2UInt32 id, Go2Object system)
{
    Go2PartPlaneXAngleClass* obj = zHANDLE_POINTER(measurement);
    z32u i; 

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

    obj->Release = Go2PartPlaneXAngle_VRelease;
    obj->Read = Go2PartPlaneXAngle_VRead; 
    obj->Write = Go2PartPlaneXAngle_VWrite; 

    obj->regionsEnabled = GO2_FALSE;

    for (i = 0; i < zCOUNT(obj->regions); ++i)
    {
        zCHECK(Go2PartRegion3d_Construct(&obj->regions[i], system));
    }
    //construct all regions and set the initial region usage to be 1
    obj->regionCount = GO2_DEFAULT_PLANE_REGION_COUNT;

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_VRelease(Go2PartPlaneXAngle measurement)
{
    Go2PartPlaneXAngleClass* obj = zHANDLE_POINTER(measurement);
    z32u i; 

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

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

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_VRead(Go2PartPlaneXAngle measurement, zXml xml, zXmlItem item)
{
    Go2PartPlaneXAngleClass* obj = zHANDLE_POINTER(measurement);
    zXmlItem regionItem = zNIL;

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

    obj->regionCount = 0; 

    zCHECK(zXml_Child32s(xml, item, "RegionsEnabled", &obj->regionsEnabled));

    regionItem = zXml_Child(xml, item, "Regions/Region");     
    while (!zIS_NIL(regionItem))
    {
        zCHECK(Go2PartRegion3d_Read(obj->regions[obj->regionCount], xml, regionItem));  
        obj->regionCount++;  
        regionItem = zXml_NextSibling(xml, regionItem);
    }
    
    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_VWrite(Go2PartPlaneXAngle  measurement, zXml xml, zXmlItem item)
{
    Go2PartPlaneXAngleClass* obj = zHANDLE_POINTER(measurement);
    zXmlItem regionItem = zNIL;
    zXmlItem refRegionItem = zNIL;
    z32u i; 

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

    zCHECK(zXml_SetChild32s(xml, item, "RegionsEnabled", obj->regionsEnabled)); 
    zCHECK(zXml_AddItem(xml, item, "Regions", &refRegionItem));  
    for (i = 0; i < obj->regionCount; ++i)
    {
        zCHECK(zXml_AddItem(xml, refRegionItem, "Region", &regionItem)); 
        zCHECK(Go2PartRegion3d_Write(obj->regions[i], xml, regionItem)); 
    }

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_SetId(Go2PartPlaneXAngle measurement, Go2UInt32 id)
{
    return Go2Measurement_SetId(measurement, id); 
}

GO2_EXPORT(Go2UInt32) Go2PartPlaneXAngle_Id(Go2PartPlaneXAngle measurement)
{
    return Go2Measurement_Id(measurement); 
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_SetName(Go2PartPlaneXAngle measurement, const Go2Char* name)
{
    return Go2Measurement_SetName(measurement, name); 
}

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

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_SetDecisionMin(Go2PartPlaneXAngle measurement, Go2Double min)
{
    return Go2Measurement_SetDecisionMin(measurement, min); 
}

GO2_EXPORT(Go2Double) Go2PartPlaneXAngle_DecisionMin(Go2PartPlaneXAngle measurement)
{
    return Go2Measurement_DecisionMin(measurement); 
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_SetDecisionMax(Go2PartPlaneXAngle measurement, Go2Double max)
{
    return Go2Measurement_SetDecisionMax(measurement, max); 
}

GO2_EXPORT(Go2Double) Go2PartPlaneXAngle_DecisionMax(Go2PartPlaneXAngle measurement)
{
    return Go2Measurement_DecisionMax(measurement); 
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_EnableHold(Go2PartPlaneXAngle measurement, Go2Bool enable)
{
    return Go2Measurement_EnableHold(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2PartPlaneXAngle_HoldEnabled(Go2PartPlaneXAngle measurement)
{
    return Go2Measurement_HoldEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_EnableSmoothing(Go2PartPlaneXAngle measurement, Go2Bool enable)
{
    return Go2Measurement_EnableSmoothing(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2PartPlaneXAngle_SmoothingEnabled(Go2PartPlaneXAngle measurement)
{
    return Go2Measurement_SmoothingEnabled(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2PartPlaneXAngle_SmoothingWindowMin(Go2PartPlaneXAngle measurement)
{
    return Go2Measurement_SmoothingWindowMin(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2PartPlaneXAngle_SmoothingWindowMax(Go2PartPlaneXAngle measurement)
{
    return Go2Measurement_SmoothingWindowMax(measurement);  
} 

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_SetSmoothingWindow(Go2PartPlaneXAngle measurement, Go2UInt32 window)
{
    return Go2Measurement_SetSmoothingWindow(measurement, window);  
} 

GO2_EXPORT(Go2UInt32) Go2PartPlaneXAngle_SmoothingWindow(Go2PartPlaneXAngle measurement)
{
    return Go2Measurement_SmoothingWindow(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2PartPlaneXAngle_RegionCount(Go2PartPlaneXAngle measurement)
{
    Go2PartPlaneXAngleClass* obj = zHANDLE_POINTER(measurement); 
    return obj->regionCount;      
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_SetRegionCount(Go2PartPlaneXAngle measurement, Go2UInt32 count)
{
    Go2PartPlaneXAngleClass* obj = zHANDLE_POINTER(measurement); 
    Go2Status status = GO2_OK;

    if(count > zCOUNT(obj->regions)){
        return GO2_ERROR_PARAMETER;
    }
    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    status = Go2System_Set32u(obj->system, &obj->regionCount, count, GO2_MIN_PLANE_REGIONS, GO2_MAX_PLANE_REGIONS); 

    zCHECK(Go2System_SetModified(obj->system)); 

    return status;      
}

GO2_EXPORT(Go2PartRegion3d) Go2PartPlaneXAngle_RegionAt(Go2PartPlaneXAngle measurement, Go2UInt32 index)
{
    Go2PartPlaneXAngleClass* obj = zHANDLE_POINTER(measurement); 

    return obj->regions[index];      
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_EnableRegions(Go2PartPlaneXAngle measurement, Go2Bool enable)  
{
    Go2PartPlaneXAngleClass* obj = zHANDLE_POINTER(measurement);

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

    return GO2_OK;
}

GO2_EXPORT(Go2Bool) Go2PartPlaneXAngle_RegionsEnabled(Go2PartPlaneXAngle measurement)
{
    Go2PartPlaneXAngleClass* obj = zHANDLE_POINTER(measurement);

    return obj->regionsEnabled;
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_SetScale(Go2PartPlaneXAngle measurement, Go2Double scale){
    return Go2Measurement_SetScale(measurement, scale);
}

GO2_EXPORT(Go2Double) Go2PartPlaneXAngle_Scale(Go2PartPlaneXAngle measurement){
    return Go2Measurement_Scale(measurement);
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_SetOffset(Go2PartPlaneXAngle measurement, Go2Double offset){
    return Go2Measurement_SetOffset(measurement, offset);
}

GO2_EXPORT(Go2Double) Go2PartPlaneXAngle_Offset(Go2PartPlaneXAngle measurement){
    return Go2Measurement_Offset(measurement);
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_EnableLink(Go2PartPlaneXAngle measurement, Go2Bool enable)  
{
    return Go2Measurement_EnableLink(measurement, enable);  
}

GO2_EXPORT(Go2Bool) Go2PartPlaneXAngle_LinkEnabled(Go2PartPlaneXAngle measurement)  
{
    return Go2Measurement_LinkEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2PartPlaneXAngle_SetLink(Go2PartPlaneXAngle measurement, Go2UInt32 link) 
{
    return Go2Measurement_SetLink(measurement, link); 
}

GO2_EXPORT(Go2UInt32) Go2PartPlaneXAngle_Link(Go2PartPlaneXAngle measurement) 
{
    return Go2Measurement_Link(measurement); 
}



GO2_EXPORT(Go2UInt32) Go2PartPlaneXAngle_LinkOptionCount(Go2PartPlaneXAngle measurement)
{
    return Go2Measurement_LinkOptionCount(measurement);
}

GO2_EXPORT(Go2UInt32) Go2PartPlaneXAngle_LinkOptionAt(Go2PartPlaneXAngle measurement, Go2UInt32 index)
{
    return Go2Measurement_LinkOptionAt(measurement, index);
}
