// ImageCollatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ImageCollator.h"
#include "ImageCollatorDlg.h"
#include <ImageLib\Image.h>
#include "ProgDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageCollatorDlg dialog

CImageCollatorDlg::CImageCollatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CImageCollatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CImageCollatorDlg)
	m_BaseName = _T("");
	m_OutDir = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CImageCollatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CImageCollatorDlg)
	DDX_Text(pDX, IDC_BASENAME, m_BaseName);
	DDX_Text(pDX, IDC_OUTPUTDIR, m_OutDir);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CImageCollatorDlg, CDialog)
//{{AFX_MSG_MAP(CImageCollatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_GO, OnGo)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
ON_BN_CLICKED(IDC_BROWSEOUT, OnBrowseout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageCollatorDlg message handlers

BOOL CImageCollatorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CImageCollatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CImageCollatorDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CImageCollatorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CImageCollatorDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	//	CDialog::OnCancel();
}

void CImageCollatorDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//	CDialog::OnOK();
}

void CImageCollatorDlg::OnGo() 
{
	UpdateData(TRUE);
	
	WIN32_FIND_DATA fd;
	HANDLE han;
	CString FName;
	int num,max,min;
	
	han=FindFirstFile(m_BaseName+"*.tga",&fd);
	
	if(INVALID_HANDLE_VALUE!=han)
	{
		FName=fd.cFileName;
		max=min=GetFileNumber(FName);
		do
		{
			FName=fd.cFileName;
			num=GetFileNumber(FName);
			max=num>max?num:max;
			min=num<min?num:min;
		} 
		while(FindNextFile(han,&fd));
	}
	
	CString DirName;
	DirName=m_BaseName.Left(m_BaseName.ReverseFind('\\'))+"\\";
	Image FinalImage,TmpImg;
	han=FindFirstFile(m_BaseName+"*.tga",&fd);
	int sliwid;
	
	if(INVALID_HANDLE_VALUE!=han)
	{
		FName=DirName+fd.cFileName;
		num=GetFileNumber(FName);
		TmpImg.LoadTga((char *)(LPCSTR)(FName));
		sliwid=TmpImg.GetWidth();
		FinalImage.CreateBlank(((max-min)+1)*sliwid,TmpImg.GetHeight(),ITYPE_RGB,TmpImg.GetBpp());
		
		CProgressDlg pdlg;
		pdlg.Create(this);
		
		int imgcnt=0;
		pdlg.SetRange(0,max-min);
		do
		{
			FName=DirName+fd.cFileName;
			num=GetFileNumber(FName);
			TmpImg.LoadTga((char *)(LPCSTR)(FName));
			FinalImage.Blt(TmpImg,
				CRect((num-min)*sliwid,0,((num-min)*sliwid)+sliwid,FinalImage.GetHeight()),
				CRect(0,0,TmpImg.GetWidth(),TmpImg.GetHeight()));
			pdlg.SetPos(imgcnt++);
		} 
		while(FindNextFile(han,&fd));
		
	}
	
	FinalImage.SaveTga((char *)(LPCSTR)m_OutDir);
	
}

void CImageCollatorDlg::OnExit() 
{
	
	CDialog::OnOK();
}

void CImageCollatorDlg::OnBrowse() 
{
	CFileDialog dlg(TRUE,"tga",NULL,NULL,"Targas (*.tga)|*.tga||",this);
	UpdateData(TRUE);
	if(dlg.DoModal()==IDOK)
	{
		
		m_BaseName=dlg.GetPathName();
		m_BaseName=m_BaseName.Left(m_BaseName.ReverseFind('.'));
		
		int clip=m_BaseName.GetLength()-1;
		while(clip>0 && isdigit(m_BaseName[clip])) 
			clip--;
		if(clip>0)
			m_BaseName=m_BaseName.Left(clip);
	}
	UpdateData(FALSE);
	
	
}


void CImageCollatorDlg::OnBrowseout() 
{
	CFileDialog dlg(FALSE,"tga",NULL,NULL,"Targas (*.tga)|*.tga||",this);
	if(dlg.DoModal()==IDOK)
	{
		m_OutDir=dlg.GetPathName();
		UpdateData(FALSE);
	}
}

int CImageCollatorDlg::GetFileNumber(CString FName)
{
	int start,end,idx;
	
	idx=start=end=FName.GetLength()-1;
	
	while(idx>0 && !isdigit(FName[idx]) ) 		idx--;
	end=idx+1;
	
	while(idx>0 && isdigit(FName[idx]) ) 		idx--;
	start=idx+1;
	
	if(start<end)
	{
		CString Textnum=FName.Mid(start,end-start);
		return atoi((LPCSTR)Textnum);
	}
	return -1;
	
}

void __stdcall GetGameOSEnvironment( char* CommandLine )
{
}

