/*
 *  Arcanum Editor - ROM area editor
 *  Copyright (C) 1999  Lucas Wall <kthulhu@usa.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  If you make changes to the source and want to make the public you can
 *  mail the diferences to me <kthulhu@usa.net> and I'll add them to the
 *  main distribution. Of course your name will be added to the program with
 *  information about the changes you made.
 *
 *
 *  Packed on Thu Aug 19 03:02:21 1999
 *
 */


/*
 * File: MobList.cpp
 *
 * Changes:
 *
 * 19/08/99 Lucas Wall <kthulhu@usa.net>
 *          First source release. Its quite messy and has no
 *          comments. I never planed to release the source code... :-)
 *
 */


// MobList.cpp : implementation file
//

#include "stdafx.h"
#include "arcaed.h"
#include "MobList.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMobList dialog


CMobList::CMobList(CWnd* pParent /*=NULL*/)
	: CDialog(CMobList::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMobList)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CMobList::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMobList)
	DDX_Control(pDX, IDC_MOBLIST_LIST, m_MobList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMobList, CDialog)
	//{{AFX_MSG_MAP(CMobList)
	ON_LBN_SELCHANGE(IDC_MOBLIST_LIST, OnSelchangeMoblistList)
	ON_BN_CLICKED(IDC_MOBED_EDITAR, OnMobedEditar)
	ON_BN_CLICKED(IDC_MOBED_NUEVO, OnMobedNuevo)
	ON_BN_CLICKED(IDC_MOBED_BORRAR, OnMobedBorrar)
	ON_LBN_DBLCLK(IDC_MOBLIST_LIST, OnMobedEditar)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMobList message handlers

BOOL CMobList::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	MakeList();
	m_curmob = NULL;
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CMobList::MakeList()
{
	int i, index;
	char buf[6000];

	m_MobList.ResetContent();
	for ( i = 0; i < m_list->GetSize(); i++ )
	{
		sprintf( buf, "%i\t%s",
			(*m_list)[i]->m_vnum, (*m_list)[i]->m_name );
		index = m_MobList.AddString( buf );
		if ( index != LB_ERR && index != LB_ERRSPACE )
			m_MobList.SetItemDataPtr( index, (*m_list)[i] );
	}
	m_MobList.SetCurSel( -1 );
	m_curmob = NULL;
}

void CMobList::OnSelchangeMoblistList() 
{
	int index = m_MobList.GetCurSel();
	if ( index == LB_ERR ) return;

	m_curmob = (CMobData *) m_MobList.GetItemDataPtr( index );
}

void CMobList::OnMobedEditar() 
{
	if ( m_doc == NULL ) return;
	if ( m_doc->EditMob( m_curmob ) == IDOK )
	{
		MakeList();
		m_doc->UpdateAllViews( NULL );
		m_doc->SetModifiedFlag();
	}
}

void CMobList::OnMobedNuevo() 
{
	CMobData *mob;

	if ( m_doc == NULL ) return;
	mob = new CMobData();
	mob->m_vnum = m_doc->FindFreeMobVnum();

	if ( mob->m_vnum == -1 )
	{
		MessageBox( "No hay vnums disponibles!", "Error",
			MB_ICONERROR );
		delete mob;
		return;
	}

	if ( m_doc->EditMob( mob ) == IDOK )
	{
		m_doc->m_mobs.Add( mob );
		MakeList();
		m_doc->UpdateAllViews( NULL );
		m_doc->SetModifiedFlag();
	}
	else
		delete mob;

}

void CMobList::OnMobedBorrar() 
{
	int i, mobnum;
	CMobData *mob;

	if ( ( i = m_MobList.GetCurSel() ) == LB_ERR )
		return;
	mob = (CMobData *) m_MobList.GetItemData( i );
	if ( ( mobnum = m_doc->FindMob( mob->m_vnum ) ) == -1 )
		return;
	if ( mobnum < 0 || mobnum >= m_doc->m_mobs.GetSize() )
		return;

	if ( MessageBox( "Attention! You are about to delete a mob.\n"
		"This action is irreversable! Do you wish to continue?", "Attention!", 
		MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2 ) != IDYES ) return;

	m_doc->m_mobs.RemoveAt( mobnum );
	MakeList();
	m_doc->UpdateAllViews( NULL );
	m_doc->SetModifiedFlag();

}