#include "stdafx.h"
#include <GosFx\GosFx.hpp>
#include <GosFx\Card.hpp>
#include "resource.h"
#include "CurveSet.h"
#include "TextPanel.h"
#include "BlankPanel.h"
#include "DVPanel.h"
#include "TexturePanel.h"
#include "TexturePanel2.h"
#include "TFPanel.h"
#include "NumPanel.h"
#include "FloatPanel.h"
#include "ShapePanel.h"
#include "ShapeCloudPanel.h"
#include "ProfilePanel.h"
#include "EBFPanel.h"
#include "Point3DPanel.h"
#include "FloatPanel.h"
#include "EditMLRStatePanel.h"
void CurveSet::Reset() 
{
	StackLevel=0;
	if(Stream)
	{
		Unregister_Object(Stream);
		delete Stream;
	}
	EffSpec=NULL;
	Curve=NULL; 
	Stream=NULL;
	Type=BLANK; 
//	Min=-99999999.9f;
//	Max=99999999.9f;
	if(Panel) 
	{
		delete Panel;
		Panel=NULL;
	}
}

void CurveSet::SetType(ConTypeTag tg,void *dat)
{
	Verify(Panel==NULL);
	Type=tg;
	
	switch(Type)
	{
	case BLANK:				Panel=new CBlankPanel;			break;
	case TEXT:				Panel=new CTextPanel;			break;
	case TEXTUREBROWSER:	Panel=new CTexturePanel;		break;
	case TEXTUREBROWSER2:	Panel=new CTexturePanel2;		break;
	case TFPANEL:			Panel=new CTFPanel;				break;
	case FLOATVAL:			Panel=new CFloatPanel;			break;
	case NUMVAL:			Panel=new CNumPanel;			break;
	case POINTPANEL:		Panel=new CPoint3DPanel;		break;
	case SHAPEPANEL:		Panel=new CShapePanel;			break;
	case SHAPECLOUDPANEL:	Panel=new CShapeCloudPanel;		break;
	case EBFPANEL:			Panel=new CEBFPanel;			break;
	case PROFILEPANEL:		Panel=new CProfilePanel;		break;
	case CURVEPANEL:		Panel=new DVPanel;				break;
	case MLRSTATEPANEL:		Panel=new CEditMLRStatePanel;	break;
	default:
		Panel=new CBlankPanel; 
		break;
	}
	Verify(Panel);  
	Panel->Set(dat);
	Panel->MyCSet=this;
	
}

bool CurveSet::IsDataValid()
{
	switch(Type)
	{
	case BLANK:				
	case TEXT:				
	case TEXTUREBROWSER:	
	case TEXTUREBROWSER2:	
	case TFPANEL:			
	case POINTPANEL:		
	case SHAPEPANEL:		
	case SHAPECLOUDPANEL:	
	case EBFPANEL:			
	case PROFILEPANEL:		
	case VARVAL:			
	case MLRSTATEPANEL:		
		break;
		
	case NUMVAL:			
		{
			CNumPanel *pan=(CNumPanel *)Panel;
//			if(*(pan->intval)<Min || *(pan->intval)>Max)
//				return false;

			if(EffSpec!=NULL)
			{
			if(EffSpec->IsDataValid()==false) return false;
				switch(EffSpec->GetClassID())
				{
				case gosFX::TubeClassID:
					gosFX::Tube__Specification *tspec;
					tspec=Cast_Pointer(gosFX::Tube__Specification *,EffSpec);
					Check_Object(tspec);
					if(!tspec->CalculateUBias(false)) return false;
					break;
				}
			}
		}
		
	case CURVEPANEL:			
		{
	
			if(EffSpec!=NULL)
			{
			if(!EffSpec->IsDataValid()) return false;

				switch(EffSpec->GetClassID())
				{
				case gosFX::TubeClassID:
					{
						gosFX::Tube__Specification *tspec;
						tspec=Cast_Pointer(gosFX::Tube__Specification *,EffSpec);
						Check_Object(tspec);
						if(!tspec->CalculateUBias(false)) return false;
					}
					break;
					
				case gosFX::CardCloudClassID:
					{
						gosFX::CardCloud__Specification *tspec;
						tspec=Cast_Pointer(gosFX::CardCloud__Specification *,EffSpec);
						Check_Object(tspec);
						tspec->SetWidth();
					}
					break;
					
				case gosFX::CardClassID:
					{
						gosFX::Card__Specification *tspec;
						tspec=Cast_Pointer(gosFX::Card__Specification *,EffSpec);
						Check_Object(tspec);
						tspec->SetWidth();
					}
					break;
					
				}
			}
			
		}
	}
	
	return true;
}

void CurveSet::PushState()
{
	if(StackLevel>0) STOP(("Nested Pushes Not Supported yet"));
	Verify(!(StackLevel==0 && Stream!=NULL));
	
	if(Stream==NULL)
	{
		Stream=new Stuff::DynamicMemoryStream;
		Register_Object(Stream);
	}
	
	
	switch(Type)
	{
	case BLANK:				
	case TEXT:				
	case TEXTUREBROWSER:	
	case TEXTUREBROWSER2:	
	case TFPANEL:			
	case POINTPANEL:		
	case SHAPEPANEL:		
	case SHAPECLOUDPANEL:	
	case EBFPANEL:			
	case PROFILEPANEL:		
	case VARVAL:			
	case MLRSTATEPANEL:		
	case FLOATVAL:			
	case NUMVAL:			
		STOP(("Push Method Unimplemented"));
		break;
		
		
	case CURVEPANEL:			
		{
			Verify(Curve);
			Curve->Save(Stream);
		}
	}
	
	
	StackLevel++;
}

void CurveSet::PopState()
{
	Verify(StackLevel>0);
	Check_Pointer(Stream);
	
	Stream->Rewind();
	
	
	switch(Type)
	{
	case BLANK:				
	case TEXT:				
	case TEXTUREBROWSER:	
	case TEXTUREBROWSER2:	
	case TFPANEL:			
	case POINTPANEL:		
	case SHAPEPANEL:		
	case SHAPECLOUDPANEL:	
	case EBFPANEL:			
	case PROFILEPANEL:		
	case VARVAL:			
	case MLRSTATEPANEL:		
	case FLOATVAL:			
	case NUMVAL:			
		STOP(("Pop Method Unimplemented"));
		break;
		
		
	case CURVEPANEL:			
		{
			Verify(Curve);
			Curve->Load(Stream, gosFX::CurrentGFXVersion);
		}
	}
	
	
	
	StackLevel--;
	if(StackLevel==0)
	{
		Unregister_Object(Stream);
		delete Stream;
		Stream=0;
	}
	else
		if(StackLevel<0)
		{
			STOP(("Save State Stack Underflow"));
		}
}

void CurveSet::PopNoSave()
{
	Verify(StackLevel>0);
	Check_Pointer(Stream);
	
	
	StackLevel--;
	if(StackLevel==0)
	{
		Unregister_Object(Stream);
		delete Stream;
		Stream=0;
	}
	else
		if(StackLevel<0)
		{
			STOP(("Save State Stack Underflow"));
		}
}

void CurveSet::VerifyAndPop()
{
	if(IsDataValid())
		PopNoSave();
	else
		PopState();
}
