#include "MAXProxyHeaders.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
FindMAXErrors::FindMAXErrors(Stuff::NotationFile *data_file, 
							 Interface *i, 
							 int p, 
							 bool bSuppress,
							 LPVOID fcn
							 ):
	FindErrorsProcess(data_file,bSuppress,fcn),
	autoSelect(false),
	iPointer(i),
	progress(p)
{
	Check_Object(data_file);

	Page *page = data_file->FindPage("FindErrors");
	if (page) {
		page->GetEntry("Verbose", &suppress);
		suppress = !suppress;
	}
}

typedef int (*LPERROR_CALLBACKFN)(char *,bool);

void
	FindMAXErrors::FindErrorsCallback(
		Proxies::GenericProxy *proxy,
		int type
	)
{
	Check_Object(this);
	Check_Object(proxy);
	//
	//--------------------------------------------
	// Do what we can based upon the type of error
	//--------------------------------------------
	//
	MString name;
	char buffer[200];
	switch (type)
	{
	case StatusCheck:
		//
		//----------------------------
		// Print the name of the proxy
		//----------------------------
		//
		proxy->GetName(&name);
		if (!suppress)
		{
			sprintf(buffer, "Checking %s for errors...", (char*)name);
			iPointer->ProgressUpdate(++progress,false,_T(buffer));
		}
		
		//
		//-------------------------------------------------------------
		// If this is a polygon mesh, we need to clear out the tags and
		// selections prior to it running
		//-------------------------------------------------------------
		//

		if (autoSelect && proxy->IsDerivedFrom(MAXPolygonMesh::DefaultData))
		{
			//
			//--------------------------------------------
			// Clear all the tagged vertices from the mesh
			//--------------------------------------------
			//
			MAXPolygonMesh *mesh = Cast_Object(MAXPolygonMesh*, proxy);
			const INode *scene = mesh->GetSceneProxy()->GetProxiedScene();
			Check_Pointer(scene);

		}
		return;

	//
	//-------------------------------------------------------------------------
	// Handle the texture problem.  For now, we will just print out the info to
	// the status line
	//-------------------------------------------------------------------------
	//
	case ERROR_Bad_Texture_Size:
		{
			Proxies::TextureProxy *texture = Cast_Object(Proxies::TextureProxy*, proxy);
			texture->GetName(&name);
			sprintf(buffer, "%s: Missing or has bad textures size!", (char*)name);
		}
		break;

	//
	//--------------------------------------------------
	// For polygon errors, we need to select the polygon
	//--------------------------------------------------
	//
	case ERROR_Bad_Normal:
		{
			sprintf(buffer, "%s: Has bad normals", name);
		}
		break;
	case ERROR_Mismatched_Vertex_Colors:
		{
			sprintf(buffer, "%s: Has mismatched vertex colors", (char*)name);
		}
		break;
	case ERROR_Mismatched_UVs:
		{
			sprintf(buffer, "%s: Has mismatched UVs", (char*)name);
		}
		break;
	case ERROR_Mismatched_Normals:
		{
			sprintf(buffer, "%s: Has mismatched normals", (char*)name);
		}
		break;
	case ERROR_Duplicate_Vertex:
		{
			MAXProxies::MAXPolygon *polygon = Cast_Object(MAXProxies::MAXPolygon*, proxy);
			MAXProxies::MAXPolygonMesh *mesh = polygon->GetPolygonMeshProxy();
			INode *pNode = mesh->GetProxiedMesh();
			char *name = pNode->GetName();
			sprintf(buffer, "%s: Has duplicate vertex", (char*)name);
		}
		break;
	case ERROR_Degenerate_Polygon:
		{
			MAXProxies::MAXPolygon *polygon = Cast_Object(MAXProxies::MAXPolygon*, proxy);
			MAXProxies::MAXPolygonMesh *mesh = polygon->GetPolygonMeshProxy();
			INode *pNode = mesh->GetProxiedMesh();
			char *name = pNode->GetName();
			sprintf(buffer, "%s: Has degenerate polygon", (char*)name);
		}
		break;
	case ERROR_Noncoplanar_Polygon:
		{
			sprintf(buffer, "%s: Has noncoplanar polygon", (char*)name);
		}
		break;
	case ERROR_Nonconvex_Polygon:
		{
			sprintf(buffer, "%s: Has nonconvex polygon", (char*)name);
		}
		break;

	}

	if (errorfn)
	{
		LPERROR_CALLBACKFN fcn = (LPERROR_CALLBACKFN)errorfn;
		fcn(buffer,suppress);
	}
	else
		MessageBox(iPointer->GetMAXHWnd(),_T(buffer),_T("Oh Shit!"),MB_OK);				
}
