/*
 * Go2PartTypes.c
 * 
 * Gocator system communication library
 * Copyright (C) 2010 by LMI Technologies Inc.
 * 
 * Licensed under The MIT License.
 * Redistributions of files must retain the above copyright notice.
 */
#include <z/zGo2PartTypes.h>
#include <z/zGo2System.h>
#include <string.h>

/*
 * Go2PartCircleRegion Class 
 */

GO2_EXPORT(Go2Status) Go2PartCircleRegion_Construct(Go2PartCircleRegion* area, Go2System system)
{
    Go2PartCircleRegion output = zNIL; 
    Go2Status status = GO2_OK; 

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

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

    *area= output; 

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartCircleRegion_Init(Go2PartCircleRegion region, Go2System system)
{
    Go2PartCircleRegionClass* obj = zHANDLE_POINTER(region);

    zCHECK(Go2Object_Init(region, GO2_TYPE_PART_CIRCLE_REGION)); 

    obj->system = system; 
    obj->x = 0.0;
    obj->y = 0.0;
    obj->radius = 1.0;
    
    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartCircleRegion_Read(Go2PartCircleRegion region, zXml xml, zXmlItem item)
{
    Go2PartCircleRegionClass* obj = zHANDLE_POINTER(region);

    zCHECK(zXml_Child64f(xml, item, "X", &obj->x)); 
    zCHECK(zXml_Child64f(xml, item, "Y", &obj->y)); 
    zCHECK(zXml_Child64f(xml, item, "Radius", &obj->radius)); 
    
    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartCircleRegion_Write(Go2PartCircleRegion region, zXml xml, zXmlItem item)
{
    Go2PartCircleRegionClass* obj = zHANDLE_POINTER(region);

    zCHECK(zXml_SetChild64f(xml, item, "X", obj->x)); 
    zCHECK(zXml_SetChild64f(xml, item, "Y", obj->y)); 
    zCHECK(zXml_SetChild64f(xml, item, "Radius", obj->radius));

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartCircleRegion_SetX(Go2PartCircleRegion region, Go2Double x)
{
    Go2PartCircleRegionClass* obj = zHANDLE_POINTER(region);

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

GO2_EXPORT(Go2Double) Go2PartCircleRegion_X(Go2PartCircleRegion region)
{
    Go2PartCircleRegionClass* obj = zHANDLE_POINTER(region);

    return obj->x;
}

GO2_EXPORT(Go2Status) Go2PartCircleRegion_SetY(Go2PartCircleRegion region, Go2Double y)
{
    Go2PartCircleRegionClass* obj = zHANDLE_POINTER(region);

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

GO2_EXPORT(Go2Double) Go2PartCircleRegion_Y(Go2PartCircleRegion region)
{
    Go2PartCircleRegionClass* obj = zHANDLE_POINTER(region);

    return obj->y;
}

GO2_EXPORT(Go2Status) Go2PartCircleRegion_SetRadius(Go2PartCircleRegion region, Go2Double radius)
{
    Go2PartCircleRegionClass* obj = zHANDLE_POINTER(region);

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

GO2_EXPORT(Go2Double) Go2PartCircleRegion_Radius(Go2PartCircleRegion region)
{
    Go2PartCircleRegionClass* obj = zHANDLE_POINTER(region);

    return obj->radius;
}

/*
 * Go2PartRegion2d Class 
 */

GO2_EXPORT(Go2Status) Go2PartRegion2d_Construct(Go2PartRegion2d* area, Go2System system)
{
    Go2PartRegion2d output = zNIL; 
    Go2Status status = GO2_OK; 

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

    if (!zSUCCESS(status = Go2PartRegion2d_Init(output, system)))
    {
        zCHECK(Go2Object_Destroy(output)); 
        return status; 
    }
    
    *area= output; 

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartRegion2d_Init(Go2PartRegion2d area, Go2System system)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);

    zCHECK(Go2Object_Init(area, GO2_TYPE_PART_REGION_2D)); 

    obj->system = system; 
    obj->length = 1.0;
    obj->width = 1.0;

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartRegion2d_Read(Go2PartRegion2d area, zXml xml, zXmlItem item)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);

    zCHECK(zXml_Child64f(xml, item, "X", &obj->x)); 
    zCHECK(zXml_Child64f(xml, item, "Y", &obj->y)); 
    zCHECK(zXml_Child64f(xml, item, "Width", &obj->width)); 
    zCHECK(zXml_Child64f(xml, item, "Length", &obj->length));
    
    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartRegion2d_Write(Go2PartRegion2d area, zXml xml, zXmlItem item)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);

    zCHECK(zXml_SetChild64f(xml, item, "X", obj->x)); 
    zCHECK(zXml_SetChild64f(xml, item, "Y", obj->y)); 
    zCHECK(zXml_SetChild64f(xml, item, "Width", obj->width)); 
    zCHECK(zXml_SetChild64f(xml, item, "Length", obj->length));
    
    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartRegion2d_SetX(Go2PartRegion2d area, Go2Double x)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->x, x, -z64F_MAX, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion2d_X(Go2PartRegion2d area)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);
    return obj->x; 
}

GO2_EXPORT(Go2Status) Go2PartRegion2d_SetY(Go2PartRegion2d area, Go2Double y)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->y, y, -z64F_MAX, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion2d_Y(Go2PartRegion2d area)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);
    return obj->y; 
}

GO2_EXPORT(Go2Status) Go2PartRegion2d_SetWidth(Go2PartRegion2d area, Go2Double width)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->width, width, 0.0, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion2d_Width(Go2PartRegion2d area)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);
    return obj->width; 
}

GO2_EXPORT(Go2Status) Go2PartRegion2d_SetLength(Go2PartRegion2d area, Go2Double length)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->length, length, 0.0, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion2d_Length(Go2PartRegion2d area)
{
    Go2PartRegion2dClass* obj = zHANDLE_POINTER(area);
    return obj->length; 
}

/*
 * Go2PartRegion3d Class 
 */

GO2_EXPORT(Go2Status) Go2PartRegion3d_Construct( Go2PartRegion3d* volume, Go2System system )
{
    Go2PartRegion3d output = zNIL; 
    Go2Status status = GO2_OK; 

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

    if (!zSUCCESS(status = Go2PartRegion3d_Init(output, system)))
    {
        zCHECK(Go2Object_Destroy(output)); 
        return status; 
    }
    
    *volume= output; 

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartRegion3d_Init( Go2PartRegion3d volume, Go2System system )
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(volume);

    zCHECK(Go2Object_Init(volume, GO2_TYPE_PART_REGION_3D)); 

    obj->system = system; 
    obj->length = 1.0;
    obj->width = 1.0;
    obj->height = 1.0;

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartRegion3d_Read( Go2PartRegion3d volume, zXml xml, zXmlItem item )
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(volume);

    zCHECK(zXml_Child64f(xml, item, "X", &obj->x)); 
    zCHECK(zXml_Child64f(xml, item, "Y", &obj->y)); 
    zCHECK(zXml_Child64f(xml, item, "Z", &obj->z)); 
    zCHECK(zXml_Child64f(xml, item, "Width", &obj->width)); 
    zCHECK(zXml_Child64f(xml, item, "Length", &obj->length));
    zCHECK(zXml_Child64f(xml, item, "Height", &obj->height));

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartRegion3d_Write( Go2PartRegion3d volume, zXml xml, zXmlItem item )
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(volume);

    zCHECK(zXml_SetChild64f(xml, item, "X", obj->x)); 
    zCHECK(zXml_SetChild64f(xml, item, "Y", obj->y)); 
    zCHECK(zXml_SetChild64f(xml, item, "Z", obj->z)); 
    zCHECK(zXml_SetChild64f(xml, item, "Width", obj->width)); 
    zCHECK(zXml_SetChild64f(xml, item, "Length", obj->length));
    zCHECK(zXml_SetChild64f(xml, item, "Height", obj->height));

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartRegion3d_SetX(Go2PartRegion3d area, Go2Double x)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->x, x, -z64F_MAX, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion3d_X(Go2PartRegion3d area)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return obj->x; 
}

GO2_EXPORT(Go2Status) Go2PartRegion3d_SetY(Go2PartRegion3d area, Go2Double y)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->y, y, -z64F_MAX, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion3d_Y(Go2PartRegion3d area)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return obj->y; 
}

GO2_EXPORT(Go2Status) Go2PartRegion3d_SetZ(Go2PartRegion3d area, Go2Double z)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->z, z, -z64F_MAX, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion3d_Z(Go2PartRegion3d area)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return obj->z; 
}

GO2_EXPORT(Go2Status) Go2PartRegion3d_SetWidth(Go2PartRegion3d area, Go2Double width)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->width, width, 0.0, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion3d_Width(Go2PartRegion3d area)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return obj->width; 
}

GO2_EXPORT(Go2Status) Go2PartRegion3d_SetLength(Go2PartRegion3d area, Go2Double length)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->length, length, 0.0, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion3d_Length(Go2PartRegion3d area)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return obj->length; 
}

GO2_EXPORT(Go2Status) Go2PartRegion3d_SetHeight(Go2PartRegion3d area, Go2Double height)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return Go2System_Set64f(obj->system,  &obj->height, height, 0.0, z64F_MAX);     
}

GO2_EXPORT(Go2Double) Go2PartRegion3d_Height(Go2PartRegion3d area)
{
    Go2PartRegion3dClass* obj = zHANDLE_POINTER(area);
    return obj->height; 
}

/*
 * Go2PartFeature3d Class 
 */

GO2_EXPORT(Go2Status) Go2PartFeature3d_Construct(Go2PartFeature3d* volume, Go2System system)
{
    Go2PartFeature3d output = zNIL; 
    Go2Status status = GO2_OK; 

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

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

    *volume = output; 

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2PartFeature3d_Init(Go2PartFeature3d volume, Go2System system)
{
    Go2PartFeature3dClass* obj = zHANDLE_POINTER(volume);

    zCHECK(Go2Object_Init(volume, GO2_TYPE_PART_FEATURE_3D)); 

    obj->system = system;
    zCHECK(Go2PartRegion3d_Construct(&obj->region, system));
    zCHECK(Go2PartRegion3d_Init(obj->region, system));
    obj->featureType = GO2_PART_2D_CENTROID;
    obj->regionEnabled = GO2_FALSE;
    
    return GO2_OK;
}

GO2_EXPORT(Go2Status) Go2PartFeature3d_VRelease(Go2PartFeature3d volume)
{
    Go2PartFeature3dClass* obj = zHANDLE_POINTER(volume);

    zCHECK(Go2Object_Destroy(obj->region));

    return Go2Object_Destroy(volume); 
}

GO2_EXPORT(Go2Status) Go2PartFeature3d_Read(Go2PartFeature3d volume, zXml xml, zXmlItem item)
{
    Go2PartFeature3dClass* obj = zHANDLE_POINTER(volume);
    zXmlItem regionItem = GO2_NULL;

    zCHECK(zXml_Child32s(xml, item, "RegionEnabled", &obj->regionEnabled));
    regionItem = zXml_Child(xml, item, "Region");     
    zCHECK(Go2PartRegion3d_Read(obj->region, xml, regionItem));
    zCHECK(zXml_Child32s(xml, item, "Type", (Go2Int32*) &obj->featureType));

    return GO2_OK;
}

GO2_EXPORT(Go2Status) Go2PartFeature3d_Write(Go2PartFeature3d volume, zXml xml, zXmlItem item)
{
    Go2PartFeature3dClass* obj = zHANDLE_POINTER(volume);
    zXmlItem regionItem = GO2_NULL;

    zCHECK(zXml_SetChild32s(xml, item, "RegionEnabled", obj->regionEnabled));
    zCHECK(zXml_AddItem(xml, item, "Region", &regionItem)); 
    zCHECK(Go2PartRegion3d_Write(obj->region, xml, regionItem));
    zCHECK(zXml_SetChild32s(xml, item, "Type", (Go2Int32) obj->featureType));

    return GO2_OK;
}

GO2_EXPORT(Go2Status) Go2PartFeature3d_SetFeatureType(Go2PartFeature3d feature, Go2PartFeature3dType featureType)
{
    Go2PartFeature3dClass* obj = zHANDLE_POINTER(feature);

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->featureType = featureType;
    zCHECK(Go2System_SetModified(obj->system)); 
    
    return GO2_OK;
}

GO2_EXPORT(Go2PartFeature3dType) Go2PartFeature3d_FeatureType(Go2PartFeature3d feature)
{
    Go2PartFeature3dClass* obj = zHANDLE_POINTER(feature);

    return obj->featureType;
}

GO2_EXPORT(Go2PartRegion3d) Go2PartFeature3d_Region(Go2PartFeature3d feature)
{
    Go2PartFeature3dClass* obj = zHANDLE_POINTER(feature);

    return obj->region;
}

GO2_EXPORT(Go2Bool) Go2PartFeature3d_RegionEnabled(Go2PartFeature3d feature)
{
    Go2PartFeature3dClass* obj = zHANDLE_POINTER(feature);

    return obj->regionEnabled;
}

GO2_EXPORT(Go2Bool) Go2PartFeature3d_EnableRegion(Go2PartFeature3d feature, Go2Bool enable)
{
    Go2PartFeature3dClass* obj = zHANDLE_POINTER(feature);

    obj->regionEnabled = enable;

    return GO2_OK;
}
