/*
 *  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:22 1999
 *
 */


/*
 * File: MobProgList.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... :-)
 *
 */


// MobProgList.cpp : implementation file
//

#include "stdafx.h"
#include "arcaed.h"
#include "MobProgList.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMobProgList dialog


CMobProgList::CMobProgList(CWnd* pParent /*=NULL*/)
	: CDialog(CMobProgList::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMobProgList)
	m_Code = _T("");
	m_Vnum = 0;
	//}}AFX_DATA_INIT
	m_doc = NULL;
}


void CMobProgList::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMobProgList)
	DDX_Control(pDX, IDC_MOBPROGLIST_BORRAR, m_Borrar);
	DDX_Control(pDX, IDC_MOBPROGLIST_VNUM_SPIN, m_VnumSpin);
	DDX_Control(pDX, IDC_MOBPROGLIST_LIST, m_List);
	DDX_Text(pDX, IDC_MOBPROGLIST_CODE, m_Code);
	DDX_Text(pDX, IDC_MOBPROGLIST_VNUM, m_Vnum);
	DDV_MinMaxInt(pDX, m_Vnum, 0, 32767);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMobProgList, CDialog)
	//{{AFX_MSG_MAP(CMobProgList)
	ON_LBN_SELCHANGE(IDC_MOBPROGLIST_LIST, OnSelchangeMobproglistList)
	ON_BN_CLICKED(IDC_MOBPROGLIST_NUEVO, OnMobproglistNuevo)
	ON_EN_CHANGE(IDC_MOBPROGLIST_CODE, OnChangeMobproglistCode)
	ON_BN_CLICKED(IDC_MOBPROGLIST_BORRAR, OnMobproglistBorrar)
	ON_EN_CHANGE(IDC_MOBPROGLIST_VNUM, OnChangeMobproglistVnum)
	ON_EN_KILLFOCUS(IDC_MOBPROGLIST_VNUM, OnKillfocusMobproglistVnum)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMobProgList message handlers

BOOL CMobProgList::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_VnumSpin.SetBuddy( GetDlgItem( IDC_MOBPROGLIST_VNUM ) );
	m_VnumSpin.SetRange( 0, 32767 );

	MakeList();

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

void CMobProgList::OnSelchangeMobproglistList() 
{
	m_Borrar.EnableWindow( FALSE );
	GetDlgItem( IDC_MOBPROGLIST_VNUM )->EnableWindow( FALSE );
	GetDlgItem( IDC_MOBPROGLIST_CODE )->EnableWindow( FALSE );

	int sel = m_List.GetCurSel();
	if ( sel == LB_ERR ) return;

	m_Borrar.EnableWindow();
	GetDlgItem( IDC_MOBPROGLIST_VNUM )->EnableWindow();
	GetDlgItem( IDC_MOBPROGLIST_CODE )->EnableWindow();

	int i = m_List.GetItemData( sel );
	UpdateData( TRUE );
	m_Vnum = (*m_mobprogs)[i]->m_vnum;
	m_Code = (*m_mobprogs)[i]->m_code;
	UpdateData( FALSE );
}

void CMobProgList::OnMobproglistNuevo() 
{
	CMobProgData *mobprog;
	int i, index;
	char buf[500];

	mobprog = new CMobProgData();
	if ( ( mobprog->m_vnum = m_doc->FindFreeMobProgVnum() ) == -1 )
	{
		MessageBox( "No hay vnums disponibles!", "Error", MB_ICONERROR );
		delete mobprog;
		return;
	}
	i = m_mobprogs->Add( mobprog );
	itoa( (*m_mobprogs)[i]->m_vnum, buf, 10 );
	index = m_List.AddString( buf );
	ASSERT( index != LB_ERR && index != LB_ERRSPACE );
	m_List.SetItemData( index, i );
	m_List.SetCurSel( index );
	OnSelchangeMobproglistList();
	m_doc->SetModifiedFlag();
}

void CMobProgList::OnChangeMobproglistCode() 
{
	int sel = m_List.GetCurSel();
	if ( sel == LB_ERR ) return;
	int i = m_List.GetItemData( sel );

	UpdateData( TRUE );
	(*m_mobprogs)[i]->m_code = m_Code;
	m_doc->SetModifiedFlag();
}

void CMobProgList::OnMobproglistBorrar() 
{
	int sel = m_List.GetCurSel();
	if ( sel == LB_ERR ) return;
	int i = m_List.GetItemData( sel );

	if ( MessageBox( 
		"Attention! This action is irreversable! \n"
		"Do you wish to continue?",
		"Attention!", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2 ) == IDNO )
		return;

	m_List.DeleteString( sel );
	m_mobprogs->RemoveAt( i );
	MakeList();
	m_doc->SetModifiedFlag();
}

void CMobProgList::OnChangeMobproglistVnum() 
{
	int sel = m_List.GetCurSel();
	if ( sel == LB_ERR ) return;
	int i = m_List.GetItemData( sel );

	(*m_mobprogs)[i]->m_vnum = m_VnumSpin.GetPos();
	m_doc->SetModifiedFlag();
}

void CMobProgList::OnKillfocusMobproglistVnum() 
{
	char buf[500];

	int sel = m_List.GetCurSel();
	if ( sel == LB_ERR ) return;
	int i = m_List.GetItemData( sel );

	m_List.DeleteString( sel );
	itoa( (*m_mobprogs)[i]->m_vnum, buf, 10 );
	sel = m_List.AddString( buf );
	ASSERT( sel != LB_ERR && sel != LB_ERRSPACE );
	m_List.SetItemData( sel, i );
	m_List.SetCurSel( sel );
	OnSelchangeMobproglistList();

	if ( (*m_mobprogs)[i]->m_vnum > m_doc->m_maxvnum || (*m_mobprogs)[i]->m_vnum < m_doc->m_minvnum )
	{
		sprintf( buf, "Vnum fuera de rango! ( %i a %i )", m_doc->m_minvnum, m_doc->m_maxvnum );
		MessageBox( buf, "Error", MB_ICONERROR );
		GotoDlgCtrl( GetDlgItem( IDC_MOBPROGLIST_VNUM ) );
		return;
	}

	if ( m_doc->FindMobProg( (*m_mobprogs)[i]->m_vnum, i ) != -1 )
	{
		if ( MessageBox( "Este Vnum ya esta en uso!\nDesea ajustarlo automaticamente?", 
			"Error", MB_YESNO | MB_ICONWARNING ) == IDYES )
		{
			if ( ( (*m_mobprogs)[i]->m_vnum = m_doc->FindFreeMobProgVnum() ) == -1 )
			{
				MessageBox( "No hay vnums disponibles!", "Error", MB_ICONERROR );
				return;
			}
			m_VnumSpin.SetPos( (*m_mobprogs)[i]->m_vnum );
			m_List.DeleteString( sel );
			itoa( (*m_mobprogs)[i]->m_vnum, buf, 10 );
			int index = m_List.AddString( buf );
			ASSERT( index != LB_ERR && index != LB_ERRSPACE );
			m_List.SetItemData( index, i );
			m_List.SetCurSel( index );
			OnSelchangeMobproglistList();
			m_doc->SetModifiedFlag();
		}
	}
}

void CMobProgList::MakeList()
{
	int i, index;
	char buf[500];

	m_List.ResetContent();
	for ( i = 0; i < m_mobprogs->GetSize(); i++ )
	{
		itoa( (*m_mobprogs)[i]->m_vnum, buf, 10 );
		index = m_List.AddString( buf );
		ASSERT( index != LB_ERR && index != LB_ERRSPACE );
		m_List.SetItemData( index, i );
	}
	m_List.SetCurSel( 0 );
	OnSelchangeMobproglistList();
}