/*
*	Created:			08.01.10
*	Author:				009
*	Last Modifed:		-
*	Description:		Функции упрощения работы с диалоговыми окнами 
*/

#if defined _mode_dialogs_included
	#endinput
#endif

#define _mode_dialogs_included
#pragma library mode_dialogs

// --------------------------------------------------
// includes
// --------------------------------------------------
#include "mode_debug"
#include "mode_property"

// --------------------------------------------------
// defines
// --------------------------------------------------
#define INVALID_MODE_DIALOG_ID -1

// --------------------------------------------------
// enums
// --------------------------------------------------
enum ModeDialogsPlayerInfo {
	pShowedDialog,
	pShowedDialogStyle
};

// --------------------------------------------------
// news
// --------------------------------------------------
new ModeDialogs_Player[MAX_PLAYERS][ModeDialogsPlayerInfo];

// --------------------------------------------------
// forwards
// --------------------------------------------------
forward OnModeDialogResponse(playerid,dialogid,response,listitem,inputtext[]);

// --------------------------------------------------
// publics
// --------------------------------------------------
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	Debug(DEBUG_START,"OnDialogResponse(%d,%d,%d,%d,'%s')",playerid,dialogid,response,listitem,inputtext);
	ModeDialogs_Player[playerid][pShowedDialog] = INVALID_MODE_DIALOG_ID;
	OnModeDialogResponse(playerid,dialogid,response,listitem,inputtext);
	Debug(DEBUG_END,"OnDialogResponse(reason: complete)");
}

// --------------------------------------------------
// natives list (for pawno functions list)
// --------------------------------------------------
/*
native ShowPlayerModeDialog(playerid,dialogid,style,caption[],info[],button1[],button2[]);
native HidePlayerModeDialog(playerid);
native SetPlayerModeDialogListData(playerid,listitem,data);
native GetPlayerModeDialogListData(playerid,listitem);
native GetPlayerModeDialogStyle(playerid);
*/

// --------------------------------------------------
// stocks
// --------------------------------------------------
stock ShowPlayerModeDialog(playerid,dialogid,style,caption[],info[],button1[],button2[])
{
	Debug(DEBUG_START,"ShowModePlayerDialog(%d,%d,%d,'%s','%s','%s','%s')",playerid,dialogid,style,caption,info,button1,button2);
	if(!IsPlayerConnected(playerid))
	{
		Debug(DEBUG_END,"ShowModePlayerDialog(reason: error)");
		printf("[ERROR] Mode dialogs -> ShowModePlayerDialog (invalid player - %d)",playerid);
		return 0;
	}
	if(ModeDialogs_Player[playerid][pShowedDialog] != INVALID_MODE_DIALOG_ID)
	{
		Debug(DEBUG_END,"ShowModePlayerDialog(reason: error)");
		printf("[ERROR] Mode dialogs -> ShowModePlayerDialog (dialog always showed , player- %d)",playerid);
		return 0;
	}
	if(!strlen(info)) 
	{
		format(info,10,"Пусто");
		if(style == DIALOG_STYLE_LIST) style = DIALOG_STYLE_MSGBOX;
	}
	ShowPlayerDialog(playerid,dialogid,style,caption,info,button1,button2);
	ModeDialogs_Player[playerid][pShowedDialog] = dialogid;
	ModeDialogs_Player[playerid][pShowedDialogStyle] = style;
	Debug(DEBUG_END,"ShowModePlayerDialog(reason: complete)");
	return 1;
}

stock HidePlayerModeDialog(playerid)
{
	Debug(DEBUG_START,"HideModePlayerDialog(%d)",playerid);
	if(!IsPlayerConnected(playerid))
	{
		Debug(DEBUG_END,"HideModePlayerDialog(reason: error)");
		printf("[ERROR] Mode dialogs -> HideModePlayerDialog (invalid player - %d)",playerid);
		return 0;
	}
	if(ModeDialogs_Player[playerid][pShowedDialog] == INVALID_MODE_DIALOG_ID)
	{
		Debug(DEBUG_END,"HideModePlayerDialog(reason: error)");
		printf("[ERROR] Mode dialogs -> HideModePlayerDialog (dialog not showed , player- %d)",playerid);
		return 0;
	}
	ShowPlayerDialog(playerid,-1,0,"","","","");
	ModeDialogs_Player[playerid][pShowedDialog] = INVALID_MODE_DIALOG_ID;
	Debug(DEBUG_END,"HideModePlayerDialog(reason: complete)");
	return 1;
}

stock GetPlayerModeDialogStyle(playerid)
{
	Debug(DEBUG_START,"GetPlayerModeDialogStyle(%d)",playerid);
	if(!IsPlayerConnected(playerid))
	{
		Debug(DEBUG_END,"GetPlayerModeDialogStyle(reason: error)");
		printf("[ERROR] Mode dialogs -> GetPlayerModeDialogStyle (invalid player - %d)",playerid);
		return 0;
	}
	if(ModeDialogs_Player[playerid][pShowedDialog] == INVALID_MODE_DIALOG_ID)
	{
		Debug(DEBUG_END,"GetPlayerModeDialogStyle(reason: error)");
		printf("[ERROR] Mode dialogs -> GetPlayerModeDialogStyle (dialog not showed , player- %d)",playerid);
		return 0;
	}
	Debug(DEBUG_END,"GetPlayerModeDialogStyle(reason: complete)");
	return ModeDialogs_Player[playerid][pShowedDialogStyle];
}

stock SetPlayerModeDialogListData(playerid,listitem,data)
{
	Debug(DEBUG_START,"SetModePlayerDialogListData(%d,%d,%d)",playerid,listitem,data);
	if(!IsPlayerConnected(playerid))
	{
		Debug(DEBUG_END,"SetModePlayerDialogListData(reason: error)");
		printf("[ERROR] Mode dialogs -> SetModePlayerDialogListData (invalid player - %d)",playerid);
		return 0;
	}
	if(ExistModeProperty(MODE_PROPERTY_DIALOGS_MAIN,playerid,listitem))
	{
		Debug(DEBUG_ACTION,"delete old data");
		DeleteModeProperty(MODE_PROPERTY_DIALOGS_MAIN,playerid,listitem);
	}
	SetModePropertyInt(MODE_PROPERTY_DIALOGS_MAIN,playerid,listitem,data);
	Debug(DEBUG_END,"SetModePlayerDialogListData(reason: complete)");
	return 1;
}

stock GetPlayerModeDialogListData(playerid,listitem)
{
	Debug(DEBUG_START,"GetModePlayerDialogListData(%d,%d)",playerid,listitem);
	if(!IsPlayerConnected(playerid))
	{
		Debug(DEBUG_END,"GetModePlayerDialogListData(reason: error)");
		printf("[ERROR] Mode dialogs -> GetModePlayerDialogListData (invalid player - %d)",playerid);
		return 0;
	}
	if(!ExistModeProperty(MODE_PROPERTY_DIALOGS_MAIN,playerid,listitem))
	{
		Debug(DEBUG_END,"GetModePlayerDialogListData(reason: error)");
		printf("[ERROR] Mode dialogs -> GetModePlayerDialogListData (list item not exist!)");
		return 0;
	}
	Debug(DEBUG_END,"GetModePlayerDialogListData(reason: complete)");
	return GetModePropertyInt(MODE_PROPERTY_DIALOGS_MAIN,playerid,listitem);
}