//===========================================================================//
// File:	LightExport.cpp                                                 
// Project: MechWarrior 4
// Contents: Plugin to export Collision Volume objects
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 07/29/99  jkyle
//---------------------------------------------------------------------------//
// Copyright (C) 1998, Fasa Interactive, Inc.
// All Rights reserved worldwide
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL
//===========================================================================//


#include "stdafx.h"

#ifdef STRICT							// Already defined by stdafx,
#undef STRICT							// so we avoid warning linking msg
#endif
#ifdef _MBCS							// The same as above
#undef _MBCS
#endif

#include "LightExport.h"
#include <GameOS\ToolOS.hpp>
#include <Stuff\StuffHeaders.hpp>
#include <DLLPlatform\DLLPlatform.hpp>
#include <mlr\mlrheaders.hpp>

#include "MaxProxies\MaxProxyHeaders.hpp"
#include "proxies\burnlights.hpp"

#include "cvobject.h"
#include "mw4.hpp"
#include "exportUtil.h"

extern HGOSHEAP AnimSuiteHeap;

//undef Verify

#ifndef _DEBUG						//This is a hack To make it link in Release
#undef Spew
//void Spew() {}
#endif

//===========================================================================//

int	LightExp::DoExport(const TCHAR *name,ExpInterface *ei,Interface *i, BOOL suppressPrompts,DWORD options)
{	
	int return_value;
	__try
	{
		return_value = DoExportWrap(name, ei, i, suppressPrompts);
		//return_value = true;
		GetSystemSetting(SYSSET_CLEAR_UNDO);
		Verify(!theHold.Holding());
	}
	__except(ProcessException(GetExceptionInformation()))
	{
	}

	return return_value;
}

int	LightExp::DoExportWrap(const TCHAR *name,ExpInterface *ei,Interface *ip, BOOL suppressPrompts)
{

	TurnOnExceptions();
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	MAXProxies::MAXScene *scene = MAXProxies::MAXScene::MakeProxy(ip,"");
	Check_Object(scene);

	Proxies::BurnLightsProcess process;
	scene->FindLights(&process);

	ChainIteratorOf<Proxies::LightProxy*> light_itr(&process.lightsToBurn);
	Proxies::LightProxy* light;
	int light_count = 0;

	gos_PushCurrentHeap(AnimSuiteHeap);

	//open this notefile on up...
	Stuff::NotationFile note_file;

	//FileStream f(name, FileStream::WriteOnly);

	while ((light = light_itr.ReadAndNext()) != NULL)
	{
		Check_Object(light);

		++light_count;
		MAXProxies::MAXLight *maxlight = (MAXProxies::MAXLight *)light;
		LightState *lightState = maxlight->GetLightState();

		if (lightState)
		{
			MidLevelRenderer::MLRLight *mlrlight = NULL;
			switch (lightState->type)
			{
			case OMNI_LGT:
				{
					MidLevelRenderer::MLRPointLight *pointlight;
					mlrlight = pointlight = new MidLevelRenderer::MLRPointLight();
					if (lightState->useAtten)
						pointlight->SetFalloffDistance(0,lightState->attenEnd);
					//lightState->SetLightMap();
					break;
				}
			case SPOT_LGT:
				{
					MidLevelRenderer::MLRSpotLight *spotlight;
					mlrlight = spotlight = new MidLevelRenderer::MLRSpotLight();
					if (lightState->useAtten)
						spotlight->SetFalloffDistance(0,lightState->attenEnd);
					spotlight->SetSpreadAngle((Stuff::Radian)lightState->fallsize);
					//lightState->SetLightMap();
					break;
				}
			case DIRECT_LGT:
				{
					MidLevelRenderer::MLRInfiniteLight *inflight;
					mlrlight = inflight = new MidLevelRenderer::MLRInfiniteLight();
					break;
				}
			case AMBIENT_LGT:
				{
					MidLevelRenderer::MLRAmbientLight *amblight;
					mlrlight = amblight = new MidLevelRenderer::MLRAmbientLight();
					break;
				}
			default:
				{
					ReportError(CString(name)+" Contains invalid light type.",suppressPrompts!=0);
					//STOP(("INVALID LIGHT TYPE"));
				}
			}
			Verify(NULL != mlrlight);
			Stuff::RGBColor color;
			maxlight->GetColor(&color);
			mlrlight->SetColor(color);

			//mlrlight->SetDynamicLight();
			//mlrlight->SetStaticLight();
			
			mlrlight->SetIntensity(lightState->intens);

			
			{
				// Local matrix already converted
				Stuff::LinearMatrix4D mat;
				maxlight->GetLocalToParent(&mat);
				mlrlight->SetLightToShapeMatrix(mat);
			}

			{
	/*		Matrix3 parentTM = maxlight->GetProxiedLight()->GetObjTMAfterWSM(maxlight->GetSceneProxy()->GetTime(),NULL);
				Matrix3 myTM = maxlight->GetProxiedLight()->GetObjTMAfterWSM(maxlight->GetSceneProxy()->GetTime(), NULL);

				Matrix3 mat = Inverse(parentTM) * myTM;

				AffineParts parts;
				decomp_affine(mat, &parts);

				Point3D translation;
				translation=ConvertMaxToMW(parts.t);

				UnitQuaternion rotation;
				rotation=ConvertMaxToMW(parts.q);


				matrix->BuildRotation(rotation);
				matrix->BuildTranslation(translation);
	*/
				Stuff::LinearMatrix4D mat;
				maxlight->GetLocalToWorld(&mat);
				mlrlight->SetLightToWorldMatrix(mat);
			}
			
			// Write the data to the notefile
			Stuff::MString section_name;
			if (maxlight->GetName(&section_name))
				mlrlight->Write(note_file.SetPage(section_name));
			// Write the file
		}
	}
	// Write the file
	note_file.SaveAs(name);
	// Clean up

	gos_PopCurrentHeap();
//	Unregister_Pointer(scene);
//	scene->Destroy();

	TurnOffExceptions();
	return true;
}
