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

/*
 * Go2ProfileStripX Class
 */

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

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

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

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_Init(Go2ProfileStripX measurement, Go2UInt32 id, Go2Object system)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement);

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

    obj->Release = Go2ProfileStripX_VRelease; 
    obj->Read = Go2ProfileStripX_VRead; 
    obj->Write = Go2ProfileStripX_VWrite; 

    obj->baseType = GO2_PROFILE_STRIP_BASE_NONE;
    obj->location = GO2_PROFILE_STRIP_LOCATION_LEFT;
    obj->leftEdgeType = 0xF; //enable all
    obj->rightEdgeType = 0xF; //enable all
    obj->tiltEnabled = 1;
    obj->selectType = GO2_PROFILE_STRIP_SELECT_BEST;
    obj->selectN = 0; //not applicable with BEST, set it anyways
    obj->transitionWidth = 2.0;
    obj->supportWidth = 5.0;
    obj->edgeHeightMin = 2.0;
    obj->voidMax = 0;
    obj->widthMin = 0;

    zCHECK(Go2ProfileArea_Construct(&obj->area, system));       

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_VRelease(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement);

    zCHECK(Go2Object_Destroy(obj->area)); 

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

GO2_EXPORT(Go2Status) Go2ProfileStripX_VRead(Go2ProfileStripX measurement, zXml xml, zXmlItem item)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    zChar* text = zNULL;
    z32u textCapacity = GO2_MEASUREMENT_SOURCE_TEXT_CAPACITY; 

    zXmlItem areaItem = 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_Child32u(xml, item, "Source", &obj->source)); 
    
    zCHECK(zXml_Child32s(xml, item, "BaseType", (z32s*) &obj->baseType)); 
    zCHECK(zXml_Child32s(xml, item, "Location", (z32s*) &obj->location)); 
    zCHECK(zXml_Child32u(xml, item, "LeftEdgeType", &obj->leftEdgeType)); 
    zCHECK(zXml_Child32u(xml, item, "RightEdgeType", &obj->rightEdgeType)); 
    zCHECK(zXml_Child32s(xml, item, "TiltEnabled", &obj->tiltEnabled)); 
    zCHECK(zXml_Child32s(xml, item, "SelectType", (z32s*) &obj->selectType));
    zCHECK(zXml_Child32u(xml, item, "SelectN", &obj->selectN));
    zCHECK(zXml_Child64f(xml, item, "EdgeSupportWidth", &obj->supportWidth));
    zCHECK(zXml_Child64f(xml, item, "EdgeTransitionWidthMax", &obj->transitionWidthMax));
    zCHECK(zXml_Child64f(xml, item, "EdgeTransitionWidth", &obj->transitionWidth));
    zCHECK(zXml_Child64f(xml, item, "VoidMaxLimitMax", &obj->voidMaxLimitMax));
    zCHECK(zXml_Child64f(xml, item, "VoidMax", &obj->voidMax));
    zCHECK(zXml_Child64f(xml, item, "WidthMin", &obj->widthMin));
    zCHECK(zXml_Child64f(xml, item, "EdgeHeightMin", &obj->edgeHeightMin));

    zCHECK(!zIS_NIL(areaItem = zXml_Child(xml, item, "Area"))); 
    zCHECK(Go2ProfileArea_Read(obj->area, xml, areaItem)); 

    zCHECK(zFree(text));

    return GO2_OK; 
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_VWrite(Go2ProfileStripX measurement, zXml xml, zXmlItem item)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    zXmlItem areaItem = zNIL; 

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

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

    zCHECK(zXml_SetChild32s(xml, item, "BaseType", obj->baseType)); 
    zCHECK(zXml_SetChild32s(xml, item, "Location", obj->location)); 
    zCHECK(zXml_SetChild32u(xml, item, "LeftEdgeType", obj->leftEdgeType)); 
    zCHECK(zXml_SetChild32u(xml, item, "RightEdgeType", obj->rightEdgeType)); 
    zCHECK(zXml_SetChild32s(xml, item, "TiltEnabled", obj->tiltEnabled)); 
    zCHECK(zXml_SetChild32s(xml, item, "SelectType", obj->selectType)); 
    zCHECK(zXml_SetChild32u(xml, item, "SelectN", obj->selectN)); 
    zCHECK(zXml_SetChild64f(xml, item, "EdgeSupportWidth", obj->supportWidth));
    zCHECK(zXml_SetChild64f(xml, item, "EdgeTransitionWidth", obj->transitionWidth));
    zCHECK(zXml_SetChild64f(xml, item, "VoidMax", obj->voidMax));
    zCHECK(zXml_SetChild64f(xml, item, "WidthMin", obj->widthMin));
    zCHECK(zXml_SetChild64f(xml, item, "EdgeHeightMin", obj->edgeHeightMin));

    zCHECK(zXml_AddItem(xml, item, "Area", &areaItem)); 
    zCHECK(Go2ProfileArea_Write(obj->area, xml, areaItem)); 

    return GO2_OK; 
}


GO2_EXPORT(Go2Status) Go2ProfileStripX_SetId(Go2ProfileStripX measurement, Go2UInt32 id)
{
    return Go2Measurement_SetId(measurement, id);     
}

GO2_EXPORT(Go2UInt32) Go2ProfileStripX_Id(Go2ProfileStripX measurement)
{
    return Go2Measurement_Id(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetName(Go2ProfileStripX measurement, const Go2Char* name)
{
    return Go2Measurement_SetName(measurement, name);     
}

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

GO2_EXPORT(Go2UInt32) Go2ProfileStripX_SourceOptionCount(Go2ProfileStripX measurement)
{
    return Go2Measurement_SourceOptionCount(measurement);     
}

GO2_EXPORT(Go2ProfileSource) Go2ProfileStripX_SourceOptionAt(Go2ProfileStripX measurement, Go2UInt32 index)
{
    return Go2Measurement_SourceOptionAt(measurement, index);     
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetSource(Go2ProfileStripX measurement, Go2ProfileSource source)
{
    return Go2Measurement_SetSource(measurement, source);     
}

GO2_EXPORT(Go2ProfileSource) Go2ProfileStripX_Source(Go2ProfileStripX measurement)
{
    return Go2Measurement_Source(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetDecisionMin(Go2ProfileStripX measurement, Go2Double min)
{
    return Go2Measurement_SetDecisionMin(measurement, min);     
}

GO2_EXPORT(Go2Double) Go2ProfileStripX_DecisionMin(Go2ProfileStripX measurement)
{
    return Go2Measurement_DecisionMin(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetDecisionMax(Go2ProfileStripX measurement, Go2Double max)
{
    return Go2Measurement_SetDecisionMax(measurement, max);     
}

GO2_EXPORT(Go2Double) Go2ProfileStripX_DecisionMax(Go2ProfileStripX measurement)
{
    return Go2Measurement_DecisionMax(measurement);     
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_EnableHold(Go2ProfileStripX measurement, Go2Bool enable)
{
    return Go2Measurement_EnableHold(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2ProfileStripX_HoldEnabled(Go2ProfileStripX measurement)
{
    return Go2Measurement_HoldEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_EnableSmoothing(Go2ProfileStripX measurement, Go2Bool enable)
{
    return Go2Measurement_EnableSmoothing(measurement, enable);  
} 

GO2_EXPORT(Go2Bool) Go2ProfileStripX_SmoothingEnabled(Go2ProfileStripX measurement)
{
    return Go2Measurement_SmoothingEnabled(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileStripX_SmoothingWindowMin(Go2ProfileStripX measurement)
{
    return Go2Measurement_SmoothingWindowMin(measurement);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileStripX_SmoothingWindowMax(Go2ProfileStripX measurement)
{
    return Go2Measurement_SmoothingWindowMax(measurement);  
} 

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetSmoothingWindow(Go2ProfileStripX measurement, Go2UInt32 window)
{
    return Go2Measurement_SetSmoothingWindow(measurement, window);  
} 

GO2_EXPORT(Go2UInt32) Go2ProfileStripX_SmoothingWindow(Go2ProfileStripX measurement)
{
    return Go2Measurement_SmoothingWindow(measurement);  
} 

GO2_EXPORT(Go2ProfileArea) Go2ProfileStripX_Area(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->area;  
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetBaseType(Go2ProfileStripX measurement, Go2ProfileStripBaseType baseType)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

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

    return GO2_OK;      
}
 
GO2_EXPORT(Go2ProfileStripBaseType) Go2ProfileStripX_BaseType(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->baseType;      
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetLocation(Go2ProfileStripX measurement, Go2ProfileStripLocation location)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

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

    return GO2_OK;      
}
 
GO2_EXPORT(Go2ProfileStripLocation) Go2ProfileStripX_Location(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->location;      
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_EnableLeftEdgeType(Go2ProfileStripX measurement, Go2ProfileStripEdgeType type, Go2Bool enable)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    if(enable)
    {
        obj->leftEdgeType |= (Go2UInt32)type; 
    }
    else
    {
        obj->leftEdgeType &= ~((Go2UInt32)type); 
    }
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK;
}

GO2_EXPORT(Go2Bool) Go2ProfileStripX_LeftEdgeTypeEnabled(Go2ProfileStripX measurement, Go2ProfileStripEdgeType type)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return (obj->leftEdgeType & (Go2UInt32)type) != 0;   
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_EnableRightEdgeType(Go2ProfileStripX measurement, Go2ProfileStripEdgeType type, Go2Bool enable)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    if(enable)
    {
        obj->rightEdgeType |= (Go2UInt32)type; 
    }
    else
    {
        obj->rightEdgeType &= ~((Go2UInt32)type); 
    }
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK;
}

GO2_EXPORT(Go2Bool) Go2ProfileStripX_RightEdgeTypeEnabled(Go2ProfileStripX measurement, Go2ProfileStripEdgeType type)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return (obj->rightEdgeType & (Go2UInt32)type) != 0;   
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_EnableTilt(Go2ProfileStripX measurement, Go2Bool enable)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

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

    return GO2_OK;
}

GO2_EXPORT(Go2Bool) Go2ProfileStripX_TiltEnabled(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->tiltEnabled;   
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetSelectType(Go2ProfileStripX measurement, Go2ProfileStripSelectType type)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->selectType = type; 
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK;      
}
 
GO2_EXPORT(Go2ProfileStripSelectType) Go2ProfileStripX_SelectType(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->selectType;      
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetSelectN(Go2ProfileStripX measurement, Go2UInt32 n)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->selectN = n; 
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK;      
}
 
GO2_EXPORT(Go2UInt32) Go2ProfileStripX_SelectN(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->selectN;      
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetEdgeSupportWidth(Go2ProfileStripX measurement, Go2Double width)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->supportWidth = width; 
    zCHECK(Go2System_SyncConfig(obj->system)); 

    return GO2_OK;      
}
 
GO2_EXPORT(Go2Double) Go2ProfileStripX_EdgeSupportWidth(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->supportWidth;      
}

GO2_EXPORT(Go2Double) Go2ProfileStripX_EdgeTransitionWidthMax(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->transitionWidthMax;      
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetEdgeTransitionWidth(Go2ProfileStripX measurement, Go2Double width)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_Set64f(obj->system, &obj->transitionWidth, width, 0, obj->transitionWidthMax));
    zCHECK(Go2System_SyncConfig(obj->system)); 

    return GO2_OK;      
}
 
GO2_EXPORT(Go2Double) Go2ProfileStripX_EdgeTransitionWidth(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->transitionWidth;      
}

GO2_EXPORT(Go2Double) Go2ProfileStripX_VoidMaxLimitMax(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->voidMaxLimitMax;      
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetVoidMax(Go2ProfileStripX measurement, Go2Double width)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    zCHECK(Go2System_Set64f(obj->system, &obj->voidMax, width, 0, obj->voidMaxLimitMax));
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK;      
}
 
GO2_EXPORT(Go2Double) Go2ProfileStripX_VoidMax(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->voidMax;      
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetWidthMin(Go2ProfileStripX measurement, Go2Double width)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->widthMin = width; 
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK;      
}
 
GO2_EXPORT(Go2Double) Go2ProfileStripX_WidthMin(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->widthMin;      
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetEdgeHeightMin(Go2ProfileStripX measurement, Go2Double height)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 

    zCHECK(Go2System_CheckConfigurable(obj->system)); 
    obj->edgeHeightMin = height; 
    zCHECK(Go2System_SetModified(obj->system)); 

    return GO2_OK;      
}
 
GO2_EXPORT(Go2Double) Go2ProfileStripX_EdgeHeightMin(Go2ProfileStripX measurement)
{
    Go2ProfileStripXClass* obj = zHANDLE_POINTER(measurement); 
    return obj->edgeHeightMin;      
}




GO2_EXPORT(Go2Status) Go2ProfileStripX_SetScale(Go2ProfileStripX measurement, Go2Double scale){
    return Go2Measurement_SetScale(measurement, scale);
}

GO2_EXPORT(Go2Double) Go2ProfileStripX_Scale(Go2ProfileStripX measurement){
    return Go2Measurement_Scale(measurement);
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetOffset(Go2ProfileStripX measurement, Go2Double offset){
    return Go2Measurement_SetOffset(measurement, offset);
}

GO2_EXPORT(Go2Double) Go2ProfileStripX_Offset(Go2ProfileStripX measurement){
    return Go2Measurement_Offset(measurement);
}

GO2_EXPORT(Go2UInt32) Go2ProfileStripX_LinkOptionCount(Go2ProfileStripX measurement)
{
    return Go2Measurement_LinkOptionCount(measurement);
}

GO2_EXPORT(Go2UInt32) Go2ProfileStripX_LinkOptionAt(Go2ProfileStripX measurement, Go2UInt32 index)
{
    return Go2Measurement_LinkOptionAt(measurement, index);
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_EnableLink(Go2ProfileStripX measurement, Go2Bool enable)  
{
    return Go2Measurement_EnableLink(measurement, enable);  
}

GO2_EXPORT(Go2Bool) Go2ProfileStripX_LinkEnabled(Go2ProfileStripX measurement)  
{
    return Go2Measurement_LinkEnabled(measurement);  
}

GO2_EXPORT(Go2Status) Go2ProfileStripX_SetLink(Go2ProfileStripX measurement, Go2UInt32 link) 
{
    return Go2Measurement_SetLink(measurement, link); 
}

GO2_EXPORT(Go2UInt32) Go2ProfileStripX_Link(Go2ProfileStripX measurement) 
{
    return Go2Measurement_Link(measurement); 
}
