#include "gpuCacheUtil.h"
namespace GPUCache {
InstanceMaterialLookup::InstanceMaterialLookup(
const MDagPath& dagPath)
    : fInstObjGroupsPlug(findInstObjGroupsPlug(dagPath))
{}
InstanceMaterialLookup::~InstanceMaterialLookup()
{}
bool InstanceMaterialLookup::hasWholeObjectMaterial()
{
    
    
    return fInstObjGroupsPlug.isSource();
}
MObject InstanceMaterialLookup::findWholeObjectShadingGroup()
 
{
    
    if (!hasWholeObjectMaterial()) {
    }
    
    return findShadingGroupByPlug(fInstObjGroupsPlug);
}
MObject InstanceMaterialLookup::findWholeObjectSurfaceMaterial()
 
{
    
    if (!hasWholeObjectMaterial()) {
    }
    
    MObject shadingGroup = findWholeObjectShadingGroup();
 
    
    return findSurfaceMaterialByShadingGroup(shadingGroup);
}
bool InstanceMaterialLookup::hasComponentMaterials()
{
    
    std::vector<MPlug> ogPlugs;
    findObjectGroupsPlug(fInstObjGroupsPlug, ogPlugs);
    
    
    BOOST_FOREACH (
const MPlug& ogPlug, ogPlugs) {
            return true;
        }
    }
    return false;
}
bool InstanceMaterialLookup::findShadingGroups(std::vector<MObject>& shadingGroups)
{
    
    if (!hasComponentMaterials()) {
        return false;
    }
    
    std::vector<MPlug> ogPlugs;
    findObjectGroupsPlug(fInstObjGroupsPlug, ogPlugs);
    
    BOOST_FOREACH (
const MPlug& ogPlug, ogPlugs) {
        shadingGroups.push_back(findShadingGroupByPlug(ogPlug));
    }
    return true;
}
bool InstanceMaterialLookup::findSurfaceMaterials(std::vector<MObject>& surfaceMaterials)
{
    
    if (!hasComponentMaterials()) {
        return false;
    }
    
    std::vector<MObject> shadingGroups;
    if (!findShadingGroups(shadingGroups)) {
        return false;
    }
    
    BOOST_FOREACH (
const MObject& shadingGroup, shadingGroups) {
        surfaceMaterials.push_back(findSurfaceMaterialByShadingGroup(shadingGroup));
    }
    return true;
}
MPlug InstanceMaterialLookup::findInstObjGroupsPlug(
const MDagPath& dagPath)
 
{
    
    
    
    return plug;
}
MObject InstanceMaterialLookup::findShadingGroupByPlug(
const MPlug& srcPlug)
 
{
    
        
        assert(plugArray.
length() == 1);
        
            MObject shadingGroup = plugArray[0].node();
 
                return shadingGroup;
            }
        }
    }
}
MObject InstanceMaterialLookup::findSurfaceMaterialByShadingGroup(
const MObject& shadingGroup)
 
{
    
    MPlug ssPlug = dgNode.findPlug(
"surfaceShader", 
false);
 
    
        assert(plugArray.
length() == 1);
        
            MObject shader = plugArray[0].node();
 
            return shader;
        }
    }
}
void InstanceMaterialLookup::findObjectGroupsPlug(
const MPlug& iogPlug, std::vector<MPlug>& ogPlugs)
 
{
    
    ogPlugs.reserve(ogPlugCount);
    for (unsigned int i = 0; i < ogPlugCount; i++) {
        
        ogPlugs.push_back(ogPlug[i]);
    }
}
bool ShadedModeColor::evaluateBool(
    const MaterialProperty::Ptr& prop,
    double                       timeInSeconds
)
{
    assert(prop && prop->type() == MaterialProperty::kBool);
    if (!prop || prop->type() != MaterialProperty::kBool) {
        return false;
    }
    const MaterialNode::Ptr     srcNode = prop->srcNode();
    const MaterialProperty::Ptr srcProp = prop->srcProp();
    if (srcNode && srcProp) {
        
        return prop->getDefaultAsBool();
    }
    else {
        
        return prop->asBool(timeInSeconds);
    }
}
float ShadedModeColor::evaluateFloat(
    const MaterialProperty::Ptr& prop,
    double                       timeInSeconds
)
{
    assert(prop && prop->type() == MaterialProperty::kFloat);
    if (!prop || prop->type() != MaterialProperty::kFloat) {
        return 0.0f;
    }
    const MaterialNode::Ptr     srcNode = prop->srcNode();
    const MaterialProperty::Ptr srcProp = prop->srcProp();
    if (srcNode && srcProp) {
        
        return prop->getDefaultAsFloat();
    }
    else {
        
        return prop->asFloat(timeInSeconds);
    }
}
MColor ShadedModeColor::evaluateDefaultColor(
 
    const MaterialProperty::Ptr& prop,
    double                       timeInSeconds
)
{
    assert(prop && prop->type() == MaterialProperty::kRGB);
    if (!prop || prop->type() != MaterialProperty::kRGB) {
    }
    
    const MaterialNode::Ptr     srcNode = prop->srcNode();
    const MaterialProperty::Ptr srcProp = prop->srcProp();
    if (srcNode && srcProp) {
        
        const boost::shared_ptr<const Texture2d> srcTex = 
            boost::dynamic_pointer_cast<const Texture2d>(srcNode);
        if (srcTex && srcTex->OutColor == srcProp) {
            
            
            
            return srcTex->DefaultColor->asColor(timeInSeconds);
        }
        else {
            
            
            return prop->getDefaultAsColor();
        }
    }
    
    return prop->asColor(timeInSeconds);
}
MColor ShadedModeColor::evaluateColor(
 
    const MaterialProperty::Ptr& prop,
    double                       timeInSeconds
)
{
    assert(prop && prop->type() == MaterialProperty::kRGB);
    if (!prop || prop->type() != MaterialProperty::kRGB) {
    }
    
    const MaterialNode::Ptr     srcNode = prop->srcNode();
    const MaterialProperty::Ptr srcProp = prop->srcProp();
    if (srcNode && srcProp) {
        
        return prop->getDefaultAsColor();
    }
    else {
        
        return prop->asColor(timeInSeconds);
    }
}
}