// WaterPanel.cpp : implementation file
//

#include "stdafx.h"
#include "MapCreator.h"
#include "WaterPanel.h"
#include <Stuff\Stuff.hpp>
#include <GameOS\ToolOS.hpp>
#include <ImageLib\Image.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWaterPanel dialog


CWaterPanel::CWaterPanel(CWnd* pParent /*=NULL*/)
: CDialog(CWaterPanel::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWaterPanel)
	m_WaterLevel = 0.0f;
	m_UseWater = FALSE;
	m_DTText = _T("");
	m_UWText = _T("");
	m_WDRatio = 1;
	m_WText = _T("");
	m_WTRatio = 8;
	//}}AFX_DATA_INIT
	
	Create(IDD,pParent);
	SetWindowPos(NULL,5,25,0,0,SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
	
}


void CWaterPanel::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWaterPanel)
	DDX_Control(pDX, IDC_WTEXTRATIO, m_WTRCon);
	DDX_Control(pDX, IDC_WTEXTBROWSE, m_WTBCon);
	DDX_Control(pDX, IDC_WTEXT, m_WTCon);
	DDX_Control(pDX, IDC_WDTBROWSE, m_WDTBCon);
	DDX_Control(pDX, IDC_UWTEXTBROWSE, m_UWTBCon);
	DDX_Control(pDX, IDC_WDETAILRATIO, m_WDRCon);
	DDX_Control(pDX, IDC_WATERLEVEL, m_WLevCon);
	DDX_Control(pDX, IDC_UWTEXT, m_UWTCon);
	DDX_Control(pDX, IDC_DTTEXT, m_DTCon);
	DDX_Text(pDX, IDC_WATERLEVEL, m_WaterLevel);
	DDV_MinMaxFloat(pDX, m_WaterLevel, 0.f, 10.f);
	DDX_Check(pDX, IDC_USEWATER, m_UseWater);
	DDX_Text(pDX, IDC_DTTEXT, m_DTText);
	DDX_Text(pDX, IDC_UWTEXT, m_UWText);
	DDX_Text(pDX, IDC_WDETAILRATIO, m_WDRatio);
	DDV_MinMaxInt(pDX, m_WDRatio, 1, 16);
	DDX_Text(pDX, IDC_WTEXT, m_WText);
	DDX_Text(pDX, IDC_WTEXTRATIO, m_WTRatio);
	DDV_MinMaxInt(pDX, m_WTRatio, 1, 16);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWaterPanel, CDialog)
//{{AFX_MSG_MAP(CWaterPanel)
ON_BN_CLICKED(IDC_UWTEXTBROWSE, OnUwtextbrowse)
ON_BN_CLICKED(IDC_WDTBROWSE, OnWdtbrowse)
ON_BN_CLICKED(IDC_WTEXTBROWSE, OnWtextbrowse)
	ON_BN_CLICKED(IDC_USEWATER, OnUsewater)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWaterPanel message handlers
void CWaterPanel::WriteEntries(Stuff::Page *page,float lowhfval)
{
	UpdateData(TRUE);
	
	if(m_UseWater)
	{
		CString str;
		int pos;
			
		str=m_WText;
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		str=str.Left(str.ReverseFind('.'));
		page->SetEntry("WaterTexture",m_BasePath+str);

		str=m_UWText;
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		str=str.Left(str.ReverseFind('.'));
		page->SetEntry("UnderwaterdetailTexture",m_BasePath+str);

		str=m_DTText;
		pos=str.ReverseFind('\\');
		if(pos>0) str=str.Mid(pos+1);
		str=str.Left(str.ReverseFind('.'));
		page->SetEntry("WaterDetailTexture",m_BasePath+str);

		page->SetEntry("WaterLevel",m_WaterLevel+lowhfval);
		page->SetEntry("WaterTextureRatio",m_WTRatio);
		page->SetEntry("WaterDetailRatio",m_WDRatio);

	}
	
	
}

bool CWaterPanel::ValidateEntries()
{
	UpdateData(TRUE);
	
	if(!m_UseWater) return true;
	
	
	if(m_WText=="" || !gos_DoesFileExist(m_WText)) 
	{
		MessageBox("Water Texture Does Not Exist","Bad Water Texture",MB_OK|MB_ICONERROR);
		return false;
	}
	
	
	if(m_DTText=="" || !gos_DoesFileExist(m_DTText)) 
	{
		MessageBox("Water Detail Texture Does Not Exist","Bad Water Texture",MB_OK|MB_ICONERROR);
		return false;
	}
	
	if(m_UWText=="" || !gos_DoesFileExist(m_UWText)) 
	{
		MessageBox("UnderWater Texture Does Not Exist","Bad Water Texture",MB_OK|MB_ICONERROR);
		return false;
	}
	
	
	{
		Image img;
		img.Load((char *)(LPCSTR)m_WText);
		
		if(img.GetWidth()!=256 || img.GetHeight()!=256)
		{
			MessageBox("Water Texture Texture MUST be 256x256","Texture Size Error",MB_OK|MB_ICONERROR);
			return false;
		}
	}
	
	
	{
		Image img;
		img.Load((char *)(LPCSTR)m_DTText);
		
		if(img.GetWidth()!=256 || img.GetHeight()!=256)
		{
			MessageBox("Water Detail Texture Texture MUST be 256x256","Texture Size Error",MB_OK|MB_ICONERROR);
			return false;
		}
	}
	{
		Image img;
		img.Load((char *)(LPCSTR)m_UWText);
		
		if(img.GetWidth()!=256 || img.GetHeight()!=256)
		{
			MessageBox("Under Water Texture Texture MUST be 256x256","Texture Size Error",MB_OK|MB_ICONERROR);
			return false;
		}
	}
	
	
	return true;
}

void CWaterPanel::OnUwtextbrowse() 
{
	UpdateData(TRUE);
	// TODO: Add your control notification handler code here
	CFileDialog fdlg(TRUE,NULL,m_UWText,OFN_NOCHANGEDIR,"Bitmap Images (*.tif,*.gif,*.tga,*.png)|*.tif;*.gif;*.tga;*.png||",NULL);	
	if(fdlg.DoModal()==IDOK) 
	{
		m_UWText=fdlg.GetPathName();	
		UpdateData(FALSE);
		
	}
	
}

void CWaterPanel::OnWdtbrowse() 
{
	UpdateData(TRUE);
	// TODO: Add your control notification handler code here
	CFileDialog fdlg(TRUE,NULL,m_DTText,OFN_NOCHANGEDIR,"Bitmap Images (*.tif,*.gif,*.tga,*.png)|*.tif;*.gif;*.tga;*.png||",NULL);	
	if(fdlg.DoModal()==IDOK) 
	{
		m_DTText=fdlg.GetPathName();	
		UpdateData(FALSE);
		
	}
}

void CWaterPanel::OnWtextbrowse() 
{
	UpdateData(TRUE);
	// TODO: Add your control notification handler code here
	CFileDialog fdlg(TRUE,NULL,m_WText,OFN_NOCHANGEDIR,"Bitmap Images (*.tif,*.gif,*.tga,*.png)|*.tif;*.gif;*.tga;*.png||",NULL);	
	if(fdlg.DoModal()==IDOK) 
	{
		m_WText=fdlg.GetPathName();	
		UpdateData(FALSE);
		
	}
}

void CWaterPanel::UpdateControlStates()
{
	UpdateData(TRUE);
	
 		m_WTRCon.EnableWindow(m_UseWater);
		m_WTBCon.EnableWindow(m_UseWater);
		m_WTCon.EnableWindow(m_UseWater);
		m_WDTBCon.EnableWindow(m_UseWater);
		m_UWTBCon.EnableWindow(m_UseWater);
		m_WDRCon.EnableWindow(m_UseWater);
		m_WLevCon.EnableWindow(m_UseWater);
		m_UWTCon.EnableWindow(m_UseWater);
		m_DTCon.EnableWindow(m_UseWater);

}


void CWaterPanel::OnUsewater() 
{
	UpdateControlStates();
	
}

BOOL CWaterPanel::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	UpdateControlStates();

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
