// FeatureLibDiag.cpp : implementation file
//

#include "stdafx.h"
#include "ImageGrinder.h"
#include "FeatureLibDiag.h"
#include "FeatureLibView.h"
#include "ImageGrinderDoc.h"
#include "FeatureProps.h"
#include "BackGroundProps.h"

#include <Compost\FeaturePool.hpp>

/////////////////////////////////////////////////////////////////////////////
// CFeatureLibDiag dialog

#define SPACEING 5


CFeatureLibDiag::CFeatureLibDiag(CWnd* pParent /*=NULL*/)
: CDialog(CFeatureLibDiag::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFeatureLibDiag)
	//}}AFX_DATA_INIT
	OwnerView=NULL;
	CurSel=-1;
}



void CFeatureLibDiag::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFeatureLibDiag)
	DDX_Control(pDX, IDC_BACKGROUND, m_BgdProps);
	DDX_Control(pDX, IDC_DELETE, m_delbut);
	DDX_Control(pDX, IDC_ADD, m_addbut);
	DDX_Control(pDX, IDC_FEATURELIST, m_FeatureList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFeatureLibDiag, CDialog)
//{{AFX_MSG_MAP(CFeatureLibDiag)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_NOTIFY(NM_DBLCLK, IDC_FEATURELIST, OnDblclkFeaturelist)
ON_NOTIFY(NM_RCLICK, IDC_FEATURELIST, OnRclickFeaturelist)
ON_NOTIFY(NM_CLICK, IDC_FEATURELIST, OnLclickFeaturelist)
ON_NOTIFY(LVN_BEGINDRAG, IDC_FEATURELIST, OnBegindragFeaturelist)
ON_WM_MOUSEMOVE()
ON_LBN_SELCHANGE(IDC_FEATURELIST, OnSelchangeFeaturelist)
ON_LBN_KILLFOCUS(IDC_FEATURELIST, OnKillfocusFeaturelist)
ON_LBN_SETFOCUS(IDC_FEATURELIST, OnSetfocusFeaturelist)
ON_BN_CLICKED(IDC_BACKGROUND, OnBackground)
ON_LBN_DBLCLK(IDC_FEATURELIST, OnDblclkFeaturelist)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFeatureLibDiag message handlers

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

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


void CFeatureLibDiag::OnAdd() 
{
	CFeatureProps dlg;
	Compost::Tool_Feature *TmpFet=new Compost::Tool_Feature;
	Register_Pointer(TmpFet);
	TmpFet->SetName((LPCSTR)MakeDefaultName());
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	if(dlg.DoModal(TmpFet,pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize(),pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize())==IDOK)
	{
		pDoc->FPool->Add(TmpFet);
		RefreshList();
		
	}
	else
	{
		
		Unregister_Pointer(TmpFet);
		delete TmpFet;
	}
}

int CFeatureLibDiag::DoModal() 
{
	// TODO: Add your specialized code here and/or call the base class
	return CDialog::DoModal();
}

CString CFeatureLibDiag::MakeDefaultName()
{
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	CString tmp("UnNamed");
	return pDoc->MakeNameUnique(tmp);
}



void CFeatureLibDiag::OnDblclkFeaturelist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	
	if(OwnerView==NULL) return;
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	int curSel = m_FeatureList.GetCurSel();
	
	if(curSel>=0)
	{
		Compost::Tool_Feature *CurFet;
		CurFet=(Compost::Tool_Feature *)m_FeatureList.GetItemData(curSel);
		
		pDoc->AddFeature(CurFet);
		pDoc->UpdateAllViews(OwnerView);
		
		*pResult = 0;
	}
}

void CFeatureLibDiag::RefreshList()
{
	
	if(OwnerView==NULL) return;
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	if(pDoc->FPool==NULL || pDoc->FGrid==NULL) return;
	
	Compost::Tool_Feature *FTexture;
	Compost::Feature *Fet;
	
	m_FeatureList.ResetContent();
	
	int itmtot=pDoc->FPool->GetNumberOfFeatures();
	for(int i=1;i<itmtot;i++)
	{
		Fet=pDoc->FPool->GetFeatureNumber(i);
		CString feature_name((char *)(*(Fet->GetName())));
		if(feature_name.GetLength()>0)			
		{
			//			feature_name=pDoc->MakeNameUnique(feature_name);
			FTexture=(Compost::Tool_Feature *)Fet;
			FTexture->SetName((const char *)(LPCSTR)feature_name);
			int pos;
			pos=m_FeatureList.AddString((LPCSTR)feature_name);
			m_FeatureList.SetItemDataPtr(pos,FTexture);
		}
	}
	SetSelection();
	UpdateHLList();
	
	//	pDoc->FPool->SaveIndex();
	
}


void CFeatureLibDiag::SetView(CFeatureLibView *vwe)
{
	OwnerView=vwe;
	RefreshList();
}

void CFeatureLibDiag::ResizeVert(int cy)
{
	CRect sze,brct,crct;
	GetWindowRect(&sze);
	SetWindowPos(NULL,0,0,sze.Width(),cy,SWP_NOMOVE|SWP_NOZORDER);
	
	int bottom;
	GetWindowRect(&sze);
	bottom=sze.Height();
	
	m_delbut.GetWindowRect(&brct);
	ScreenToClient(brct);
	bottom-=brct.Height()+SPACEING;
	m_delbut.SetWindowPos(NULL,brct.left,bottom,0,0,SWP_NOSIZE|SWP_NOZORDER);
	
	m_addbut.GetWindowRect(&brct);
	ScreenToClient(brct);
	bottom-=brct.Height()+SPACEING;
	m_addbut.SetWindowPos(NULL,brct.left,bottom,0,0,SWP_NOSIZE|SWP_NOZORDER);
	
	m_BgdProps.GetWindowRect(&crct);
	ScreenToClient(crct);
	m_FeatureList.GetWindowRect(&brct);
	ScreenToClient(brct);
	brct.top=SPACEING*2+crct.Height();
	brct.bottom=bottom-SPACEING;
	m_FeatureList.SetWindowPos(NULL,brct.left,brct.top,brct.Width(),brct.Height(),SWP_NOZORDER);
	
	int thgt=crct.Height();
	crct.top=SPACEING;
	crct.bottom=crct.top+thgt;
	m_BgdProps.SetWindowPos(NULL,crct.left,crct.top,crct.Width(),crct.Height(),SWP_NOZORDER);
	
}

void CFeatureLibDiag::ResizeHor(int cx)
{
	CRect sze,brct;
	GetWindowRect(&sze);
	SetWindowPos(NULL,0,0,cx,sze.Height(),SWP_NOMOVE|SWP_NOZORDER);
	
	GetClientRect(&sze);
	
	m_delbut.GetWindowRect(&brct);
	ScreenToClient(brct);
	m_delbut.SetWindowPos(NULL,(sze.Width()-brct.Width())/2,brct.top,0,0,SWP_NOSIZE|SWP_NOZORDER);
	
	m_addbut.GetWindowRect(&brct);
	ScreenToClient(brct);
	m_addbut.SetWindowPos(NULL,(sze.Width()-brct.Width())/2,brct.top,0,0,SWP_NOSIZE|SWP_NOZORDER);
	
	m_BgdProps.GetWindowRect(&brct);
	ScreenToClient(brct);
	m_BgdProps.SetWindowPos(NULL,(sze.Width()-brct.Width())/2,brct.top,0,0,SWP_NOSIZE|SWP_NOZORDER);
	
	m_FeatureList.GetWindowRect(&brct);
	ScreenToClient(brct);
	m_FeatureList.SetWindowPos(NULL,SPACEING,brct.top,sze.Width()-(SPACEING*2),brct.Height(),SWP_NOZORDER);
	
	
}

void CFeatureLibDiag::OnRclickFeaturelist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	
	int nItem = m_FeatureList.RCPos;
	m_FeatureList.SetCurSel(m_FeatureList.RCPos);
	if(nItem==-1) return;
	CFeatureProps dlg;
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	Compost::Tool_Feature *Fet=(Compost::Tool_Feature *)m_FeatureList.GetItemData(nItem);
	
	if(dlg.DoModal(Fet,pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize(),pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize())==IDOK)
	{
		Fet->SetName((LPCSTR)CString(*Fet->GetName()));	
		RefreshList();
		if(OwnerView==NULL) return;
		
		pDoc->FGrid->RedoFeaturePosition();
		pDoc->SetDrawFlags();
		pDoc->UpdateAllViews(OwnerView);
	}
	
	
	
	*pResult = 0;
}

void CFeatureLibDiag::OnLclickFeaturelist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	
	
	
	*pResult = 0;
}


void CFeatureLibDiag::OnBegindragFeaturelist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	
	*pResult = 0;
}

void CFeatureLibDiag::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(nFlags&MK_LBUTTON)
	{
	}
	
	CDialog::OnMouseMove(nFlags, point);
}

void CFeatureLibDiag::OnSelchangeFeaturelist() 
{
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	UpdateHLList();
	
	int curSel = m_FeatureList.GetCurSel();
	
	if(curSel>=0)
	{
		pDoc->CurSelFet=(Compost::Tool_Feature *)m_FeatureList.GetItemData(curSel);
	}
	else
	{
		pDoc->CurSelFet=NULL;
	}
	
	
	if(m_FeatureList.Reordered)
	{
		
		
		pDoc->FPool->Move(m_FeatureList.OldPos+1, m_FeatureList.NewPos+1);
		pDoc->FGrid->RedoFeaturePosition();
		pDoc->SetDrawFlags();
	}
	pDoc->UpdateAllViews(OwnerView);
}

void CFeatureLibDiag::OnKillfocusFeaturelist() 
{
}

void CFeatureLibDiag::OnSetfocusFeaturelist() 
{
}

BOOL CFeatureLibDiag::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
	
	return CDialog::OnNotify(wParam, lParam, pResult);
}


void CFeatureLibDiag::OnBackground() 
{
	if(OwnerView==NULL) return;
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	CBackGroundProps dlg;
	Compost::Tool_Feature *TmpFet = ((Compost::Tool_Feature *)pDoc->FPool->GetFeatureNumber(0));
	
	if(dlg.DoModal((Compost::Tool_Feature *)pDoc->FPool->GetFeatureNumber(0))==IDOK)
	{
		if(OwnerView==NULL) return;
		if(dlg.m_stretch)
		{
			TmpFet->SetBlendMode(Compost::Feature::Blend_Mode::StretchPaste);
			TmpFet->SetFeatureMask0(TmpFet->GetFeatureTexture());
		}
		else
			TmpFet->SetBlendMode(Compost::Feature::Blend_Mode::Paste);
		
		pDoc->SetDrawFlags();
		pDoc->UpdateAllViews(OwnerView);
	}
	
}

void CFeatureLibDiag::InitBackGround()
{
	if(OwnerView==NULL) return;
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	CBackGroundProps dlg;
	
	Compost::Tool_Feature *TmpFet = new Compost::Tool_Feature;
	Register_Pointer(TmpFet);
	TmpFet->SetName((LPCSTR)"BackGround");
	TmpFet->SetFeatureHeight(pDoc->FGrid->GetRows()*pDoc->FGrid->GetGridSize());
	TmpFet->SetFeatureWidth(pDoc->FGrid->GetColumns()*pDoc->FGrid->GetGridSize());
	
	dlg.DoModal(TmpFet);
	if(dlg.m_stretch)
	{
		TmpFet->SetBlendMode(Compost::Feature::Blend_Mode::StretchPaste);
		TmpFet->SetFeatureMask0(TmpFet->GetFeatureTexture());
	}
	else
	{
		TmpFet->SetBlendMode(Compost::Feature::Blend_Mode::Paste);
	}
	
	pDoc->FPool->Insert(TmpFet,0);
	pDoc->SetDrawFlags();
	pDoc->UpdateAllViews(OwnerView);
	
	RefreshList();
	
	
}

void CFeatureLibDiag::SetSelection()
{
	int j;
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	Compost::Tool_Feature *curfet;
	
	
	for(j=0;j<m_FeatureList.GetCount();j++)
	{
		curfet=(Compost::Tool_Feature *)m_FeatureList.GetItemDataPtr(j);
		if(pDoc->IsSelected(curfet))
		{
			m_FeatureList.SetSel(j,TRUE);
		}
	}
	
	UpdateHLList();
	
}

void CFeatureLibDiag::UpdateHLList()
{
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	int *itm=new int[m_FeatureList.GetSelCount()];
	m_FeatureList.GetSelItems(m_FeatureList.GetSelCount(),itm);
	
	pDoc->ClearHL();	
	for(int i=0;i<m_FeatureList.GetSelCount();i++)
	{
		pDoc->AddHLFeature((Compost::Tool_Feature *)m_FeatureList.GetItemDataPtr(itm[i]));
	}
	
	delete itm;
	
	
}

void CFeatureLibDiag::OnDelete() 
{
	int i,selcount,*sel;
	selcount=m_FeatureList.GetSelCount();
	sel=new int[selcount];
	m_FeatureList.GetSelItems(selcount,sel);
	
	Compost::Tool_Feature *fet;
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	for(i=0;i<selcount;i++)
	{
		
		fet=(Compost::Tool_Feature *)m_FeatureList.GetItemDataPtr(sel[i]);
		pDoc->RemoveFeature(fet);
		
		
	}
	
	
	delete sel;
	pDoc->UpdateAllViews(NULL);
}

void CFeatureLibDiag::LoadFeatures(CString fname)
{
	Stuff::FileStream fstream;
	fstream.Open((char *)(LPCSTR)fname,Stuff::FileStream::ReadOnly);
	
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	int selcount,ver;
	fstream>>ver;
	fstream>>selcount;
	
	for(int i=0;i<selcount;i++)
	{
		pDoc->FPool->Add(pDoc->FPool->LoadFeature(&fstream,ver));
	}
	
	
	//	fstream.Close();
	RefreshList();
}

void CFeatureLibDiag::SaveSelectedFeatures(CString str)
{
	Stuff::FileStream fstream;
	fstream.Open((char *)(LPCSTR)str,Stuff::FileStream::WriteOnly);
	
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	int selcount=m_FeatureList.GetSelCount(),ver=Compost::Current_Compost_Version;
	fstream<<ver;
	fstream<<selcount;
	int *itm=new int[selcount];
	m_FeatureList.GetSelItems(selcount,itm);
	
	pDoc->ClearHL();
	Stuff::MString *name = NULL;
	
	for(int i=0;i<selcount;i++)
	{
		name = ((Compost::Tool_Feature *)m_FeatureList.GetItemDataPtr(itm[i]))->GetName();
		
		if(name==NULL)
		{
			fstream << 0;
		}
		else
		{
			fstream << 1;
			fstream	<< *name;
		}
		
		pDoc->FPool->SaveFeature(&fstream,((Compost::Tool_Feature *)m_FeatureList.GetItemDataPtr(itm[i])));
	}
	
	delete itm;
	
	fstream.Close();
}

void CFeatureLibDiag::SelectUnsed()
{
	int j;
	CImageGrinderDoc* pDoc = (CImageGrinderDoc*)OwnerView->GetDocument();
	ASSERT_VALID(pDoc);
	
	Compost::Tool_Feature *curfet;
	
	
	for(j=0;j<m_FeatureList.GetCount();j++)
	{
		curfet=(Compost::Tool_Feature *)m_FeatureList.GetItemDataPtr(j);
		
		
		const Stuff::DynamicArrayOf<Compost::FeatureInstance*> *FList;
		int count;
		int CurFet;
		Compost::Tool_Feature *feature = NULL;
		Compost::Feature_Texture *fTexture = NULL;
		
				curfet->StatFlags|=FIF_SELECTED;
				m_FeatureList.SetSel(j,TRUE);

			FList=pDoc->FGrid->GetUniqueFeatureInstances(count);
		for(CurFet=0;CurFet<count;CurFet++)
		{
			if((*FList)[CurFet]->feature==curfet)
			{
				curfet->StatFlags&=~FIF_SELECTED;
				m_FeatureList.SetSel(j,FALSE);
			}	
		}
		
		
		
		
	}
	
	//UpdateHLList();
}
