#include <stdio.h>
#define OPENMAYA_EXPORT
#include <maya/MIOStream.h>
#include <maya/MGlobal.h>
#include <maya/MString.h>
#include <maya/MStringArray.h>
#include <maya/MConditionMessage.h>
#include <maya/MFnPlugin.h>
#include <maya/MPxCommand.h>
#include <maya/MSyntax.h>
#include <maya/MArgDatabase.h>
#define kMessageFlag        "m"
#define kMessageFlagLong    "message"
static void conditionChangedCB(bool state, void * data);
typedef MCallbackId* MCallbackIdPtr;
static MCallbackIdPtr callbackId = NULL;
{
public:
    conditionTest();
    virtual                 ~conditionTest(); 
    static void*            creator();
private:
    bool                    addMessage;
    bool                    delMessage;
};
conditionTest::conditionTest()
:   addMessage(false)
,   delMessage(false)
{
    conditions.clear();
}
conditionTest::~conditionTest()
{
    
}
void* conditionTest::creator()
{
    return (void *) (new conditionTest);
}
{
    return syntax;
}
{
    if (argData.isFlagSet(kMessageFlag))
    {
        bool flag;
        status = argData.getFlagArgument(kMessageFlag, 0, flag);
        if (!status)
        {
            status.
perror(
"could not parse message flag");
            return status;
        }
        if (flag)
        {
            addMessage = true;
        }
        else
        {
            delMessage = true;
        }
    }
    status = argData.getObjects(conditions);
    if (!status)
    {
        status.
perror(
"could not parse condition names");
    }
    
    
    if (conditions.length() == 0)
    {
        
        
        
        conditions = conditionNames;
    }
    return status;
}
{
    status = parseArgs(args);
    if (!status)
    {
        return status;
    }
    
    
    
    
    
    
    
    int * indices = new int [conditions.length()];
    int i, j;
    for (i = 0; i < (int)conditions.length(); ++i)
    {
        
        
        indices[i] = -1;
        
        
        for (j = 0; j < (int)conditionNames.
length(); ++j)
 
        {
            if (conditions[i] == conditionNames[j])
            {
                
                
                indices[i] = j;
                break;
            }
        }
    }
    for (i = 0; i < (int)conditions.length(); ++i)
    {
        j = indices[i];
        if (j == -1)
        {
                                    MString(
"is not a valid condition name\n"));
 
            break;
        }
        if (addMessage && callbackId[j] == 0)
        {
                conditions[i],
                conditionChangedCB,
                (void*)(size_t)j,
                &status);
            
            if (!status)
            {
                status.
perror(
"failed to add callback for " + conditions[i]);
                callbackId[j] = 0;
            }
        }
        else if (delMessage && callbackId[j] != 0)
        {
            if (!status)
            {
                status.
perror(
"failed to remove callback for " + conditions[i]);
            }
            callbackId[j] = 0;
        }
    }
    
    
    char tmpStr[128];
    bool state, msgs;
    
    for (i = 0; i < (int)conditions.length(); ++i)
    {
        j = indices[i];
        if (j == -1)
        {
            continue;
        }
        if (!status)
        {
            status.
perror(
"failed to get status for " + conditions[i]);
            state = false;
        }
        msgs = (callbackId[j] != 0);
        sprintf(tmpStr, "%-20s  %-5s  %s\n",
                conditions[i].asChar(),
                state ? "true" : "false",
                msgs  ? "yes"  : "no");
    }
    
    
    delete [] indices;
    return status;
}
{ 
    MFnPlugin plugin( obj, PLUGIN_COMPANY, 
"3.0", 
"Any");
 
    if (!status)
    {
        return status;
    }
    cout<<
"conditionTest: "<<conditionNames.
length()<<
" conditions are defined."<<endl;
    callbackId = 
new MCallbackId [conditionNames.
length()];
    if (!callbackId)
    {
    }
    for (
unsigned int i = 0; i < conditionNames.
length(); ++i)
 
    {
        callbackId[i] = 0;
    }
    
    
    status = plugin.registerCommand("conditionTest",
                                    conditionTest::creator,
                                    conditionTest::newSyntax);
    if (!status)
    {
        status.
perror(
"registerCommand");
    }
    return status;
}
{
    
    
    int i;
    int len = conditionNames.
length();
 
    for (i = 0; i < len; ++i)
    {
        if (callbackId[i] != 0)
        {
                                    conditionNames[i] +
                                    "\n");
            callbackId[i] = 0;
        }
    }
    delete [] callbackId;
    
    
    status = plugin.deregisterCommand("conditionTest");
    if (!status)
    {
        status.
perror(
"deregisterCommand");
    }
    return status;
}
static void conditionChangedCB(bool state, void * data)
{
    int i = (int)(size_t)data;
    if (i > 0 && i < (MIntPtrSz)conditionNames.
length())
 
    {
                             conditionNames[i] +
                             " changed to " +
                             (state ? "true\n" : "false\n"));
    }
    else
    {
    }
}