#ifndef MAKE_ID
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
#endif

/* Libraries */
#include <libraries/mui.h>
#include <libraries/gadtools.h> /* for Barlabel in MenuItem */
#include <exec/memory.h>

/* Prototypes */
#include <proto/muimaster.h>
#include <proto/exec.h>
#ifdef __SASC
#include <clib/alib_protos.h>
#else
#include <proto/alib.h>
#endif /* __SASC */

#include "FreeCheckGUI.h"
#include "FreeCheckExtern.h"
#include "Hook_utility.h"
static char *GetMBString(APTR ref)
{
	char *aux;

	aux = Get_String(ref);
	if (aux[1] == '\0') return(&aux[2]);
	else                return(aux);
}

struct ObjApp * CreateApp(void)
{
	struct ObjApp * ObjectApp;

	APTR	MNlabel1Project, MNlabel1About, MNlabel1AboutMUI, MNlabel1BarLabel0;
	APTR	MNlabel1Iconified, MNlabel1Exit, MNlabel1Settings, MNlabel1LoadConfiguration;
	APTR	GrupoPrincipal, GrupoSubPrincipal, LabelPrincipal, GrupoConnect, GrupoStatisticas;
	APTR	GrupoStatusCall, GrupoBotoes, GrupoDeMensagens, GrupoAbout, GrupoImagem;
	APTR	FreedomImage, GrupoBotoesAbout, AboutBotaoOk;
	static struct Hook CarregaUpdateHook;
	static struct Hook CarregaConfigHook;
	static struct Hook AcertaAboutHook;
	static struct Hook AplicacaoHideHook;
	static struct Hook AboutMUIHook;
	static struct Hook CarregaTempoHook;

	if (!(ObjectApp = AllocVec(sizeof(struct ObjApp),MEMF_PUBLIC|MEMF_CLEAR)))
		return(NULL);

	ObjectApp->STR_LabelPrincipal = GetMBString(MSG_LabelPrincipal);
	ObjectApp->STR_LabelConnect = NULL;
	ObjectApp->STR_LabelOnline = NULL;
	ObjectApp->STR_LabelCost = NULL;
	ObjectApp->STR_LabelStatusCall = GetMBString(MSG_LabelStatusCall);
	ObjectApp->STR_Msg01 = GetMBString(MSG_Msg01);
	ObjectApp->STR_Msg02 = GetMBString(MSG_Msg02);
	ObjectApp->STR_Msg03 = GetMBString(MSG_Msg03);
	ObjectApp->STR_Msg04 = GetMBString(MSG_Msg04);
	ObjectApp->STR_Msg05 = GetMBString(MSG_Msg05);
	ObjectApp->STR_Msg06 = GetMBString(MSG_Msg06);
	ObjectApp->STR_Msg07 = GetMBString(MSG_Msg07);
	ObjectApp->STR_TextAbout = NULL;

	InstallHook(&CarregaUpdateHook,CarregaUpdate,ObjectApp);
	InstallHook(&CarregaConfigHook,CarregaConfig,ObjectApp);
	InstallHook(&AcertaAboutHook,AcertaAbout,ObjectApp);
	InstallHook(&AplicacaoHideHook,AplicacaoHide,ObjectApp);
	InstallHook(&AboutMUIHook,AboutMUI,ObjectApp);
	InstallHook(&CarregaTempoHook,CarregaTempo,ObjectApp);

	LabelPrincipal = TextObject,
		MUIA_Background, MUII_SHADOWFILL,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, ObjectApp->STR_LabelPrincipal,
		MUIA_Text_PreParse, "\0338\033c",
		MUIA_Text_SetMin, TRUE,
	End;

	ObjectApp->LabelConnect = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_FrameTitle, GetMBString(MSG_LabelConnectTitle),
		MUIA_Text_Contents, ObjectApp->STR_LabelConnect,
		MUIA_Text_PreParse, "\033c",
		MUIA_Text_SetMin, TRUE,
	End;

	GrupoConnect = GroupObject,
		Child, ObjectApp->LabelConnect,
	End;

	ObjectApp->LabelOnline = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_FrameTitle, GetMBString(MSG_LabelOnlineTitle),
		MUIA_Text_Contents, ObjectApp->STR_LabelOnline,
		MUIA_Text_PreParse, "\033c",
		MUIA_Text_SetMin, TRUE,
	End;

	ObjectApp->LabelCost = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_FrameTitle, GetMBString(MSG_LabelCostTitle),
		MUIA_Text_Contents, ObjectApp->STR_LabelCost,
		MUIA_Text_PreParse, "\033c",
		MUIA_Text_SetMin, TRUE,
	End;

	GrupoStatisticas = GroupObject,
		MUIA_Group_Columns, 2,
		Child, ObjectApp->LabelOnline,
		Child, ObjectApp->LabelCost,
	End;

	ObjectApp->LabelStatusCall = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_FrameTitle, GetMBString(MSG_LabelStatusCallTitle),
		MUIA_Text_Contents, ObjectApp->STR_LabelStatusCall,
		MUIA_Text_PreParse, "\033c",
		MUIA_Text_SetMin, TRUE,
	End;

	GrupoStatusCall = GroupObject,
		Child, ObjectApp->LabelStatusCall,
	End;

	GrupoSubPrincipal = GroupObject,
		MUIA_Background, MUII_RequesterBack,
		MUIA_Frame, MUIV_Frame_Group,
		Child, LabelPrincipal,
		Child, GrupoConnect,
		Child, GrupoStatisticas,
		Child, GrupoStatusCall,
	End;

	ObjectApp->BotaoExit = SimpleButton(GetMBString(MSG_BotaoExit));

	ObjectApp->BotaoIconified = SimpleButton(GetMBString(MSG_BotaoIconified));

	ObjectApp->BotaoUpdate = SimpleButton(GetMBString(MSG_BotaoUpdate));

	GrupoBotoes = GroupObject,
		MUIA_Group_Horiz, TRUE,
		Child, ObjectApp->BotaoExit,
		Child, ObjectApp->BotaoIconified,
		Child, ObjectApp->BotaoUpdate,
	End;

	GrupoPrincipal = GroupObject,
		Child, GrupoSubPrincipal,
		Child, GrupoBotoes,
	End;

	MNlabel1About = MenuitemObject,
		MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1About),
		MUIA_Menuitem_Shortcut, GetMBString(MSG_MNlabel1AboutChar),
	End;

	MNlabel1AboutMUI = MenuitemObject,
		MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1AboutMUI),
		MUIA_Menuitem_Shortcut, GetMBString(MSG_MNlabel1AboutMUIChar),
	End;

	MNlabel1BarLabel0 = MUI_MakeObject(MUIO_Menuitem, NM_BARLABEL, 0, 0, 0);

	MNlabel1Iconified = MenuitemObject,
		MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1Iconified),
		MUIA_Menuitem_Shortcut, GetMBString(MSG_MNlabel1IconifiedChar),
	End;

	MNlabel1Exit = MenuitemObject,
		MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1Exit),
		MUIA_Menuitem_Shortcut, GetMBString(MSG_MNlabel1ExitChar),
	End;

	MNlabel1Project = MenuitemObject,
		MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1Project),
		MUIA_Family_Child, MNlabel1About,
		MUIA_Family_Child, MNlabel1AboutMUI,
		MUIA_Family_Child, MNlabel1BarLabel0,
		MUIA_Family_Child, MNlabel1Iconified,
		MUIA_Family_Child, MNlabel1Exit,
	End;

	MNlabel1LoadConfiguration = MenuitemObject,
		MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1LoadConfiguration),
		MUIA_Menuitem_Shortcut, GetMBString(MSG_MNlabel1LoadConfigurationChar),
	End;

	MNlabel1Settings = MenuitemObject,
		MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1Settings),
		MUIA_Family_Child, MNlabel1LoadConfiguration,
	End;

	ObjectApp->MN_label_1 = MenustripObject,
		MUIA_Family_Child, MNlabel1Project,
		MUIA_Family_Child, MNlabel1Settings,
	End;

	ObjectApp->JanelaPrincipal = WindowObject,
		MUIA_Window_Title, GetMBString(MSG_JanelaPrincipal),
		MUIA_Window_Menustrip, ObjectApp->MN_label_1,
		MUIA_Window_ID, MAKE_ID('0', 'W', 'I', 'N'),
		WindowContents, GrupoPrincipal,
	End;

	ObjectApp->Msg01 = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, ObjectApp->STR_Msg01,
		MUIA_Text_SetMin, TRUE,
	End;

	ObjectApp->Msg02 = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, ObjectApp->STR_Msg02,
		MUIA_Text_SetMin, TRUE,
	End;

	ObjectApp->Msg03 = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, ObjectApp->STR_Msg03,
		MUIA_Text_SetMin, TRUE,
	End;

	ObjectApp->Msg04 = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, ObjectApp->STR_Msg04,
		MUIA_Text_SetMin, TRUE,
	End;

	ObjectApp->Msg05 = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, ObjectApp->STR_Msg05,
		MUIA_Text_SetMin, TRUE,
	End;

	ObjectApp->Msg06 = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, ObjectApp->STR_Msg06,
		MUIA_Text_SetMin, TRUE,
	End;

	ObjectApp->Msg07 = TextObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, ObjectApp->STR_Msg07,
		MUIA_Text_SetMin, TRUE,
	End;

	GrupoDeMensagens = GroupObject,
		Child, ObjectApp->Msg01,
		Child, ObjectApp->Msg02,
		Child, ObjectApp->Msg03,
		Child, ObjectApp->Msg04,
		Child, ObjectApp->Msg05,
		Child, ObjectApp->Msg06,
		Child, ObjectApp->Msg07,
	End;

	ObjectApp->JanelaMsg = WindowObject,
		MUIA_Window_Title, GetMBString(MSG_JanelaMsg),
		MUIA_Window_ID, MAKE_ID('1', 'W', 'I', 'N'),
		WindowContents, GrupoDeMensagens,
	End;

	FreedomImage = ImageObject,
		MUIA_Image_Spec, "5:Freedom:Images/ExplorerLogo.iff",
		MUIA_Frame, MUIV_Frame_ImageButton,
		MUIA_Image_FreeVert, TRUE,
		MUIA_Image_FreeHoriz, TRUE,
		MUIA_FixHeight, 117,
		MUIA_FixWidth, 186,
	End;

	ObjectApp->TextAbout = TextObject,
		MUIA_Background, MUII_SHADOWFILL,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, ObjectApp->STR_TextAbout,
		MUIA_Text_SetMin, TRUE,
	End;

	GrupoImagem = GroupObject,
		MUIA_Group_Horiz, TRUE,
		Child, FreedomImage,
		Child, ObjectApp->TextAbout,
	End;

	AboutBotaoOk = SimpleButton(GetMBString(MSG_AboutBotaoOk));

	GrupoBotoesAbout = GroupObject,
		MUIA_Group_Horiz, TRUE,
		Child, AboutBotaoOk,
	End;

	GrupoAbout = GroupObject,
		Child, GrupoImagem,
		Child, GrupoBotoesAbout,
	End;

	ObjectApp->JanelaAbout = WindowObject,
		MUIA_Window_Title, GetMBString(MSG_JanelaAbout),
		MUIA_Window_ID, MAKE_ID('2', 'W', 'I', 'N'),
		WindowContents, GrupoAbout,
	End;

	ObjectApp->App = ApplicationObject,
		MUIA_Application_Author, "Cristian Robert Gallas",
		MUIA_Application_Base, "Freedom Explore Call",
		MUIA_Application_Title, "ExploreCall",
		MUIA_Application_Version, "$VER: Module Explore Call 1.02 (05.11.2000)",
		MUIA_Application_Copyright, GetMBString(MSG_AppCopyright),
		MUIA_Application_Description, GetMBString(MSG_AppDescription),
		MUIA_Application_HelpFile, "Freedom.guide",
		SubWindow, ObjectApp->JanelaPrincipal,
		SubWindow, ObjectApp->JanelaMsg,
		SubWindow, ObjectApp->JanelaAbout,
	End;


	if (!ObjectApp->App)
	{
		FreeVec(ObjectApp);
		return(NULL);
	}

	DoMethod(ObjectApp->App,
		MUIM_Notify, MUIA_Application_Iconified, FALSE,
		ObjectApp->App,
		2,
		MUIM_CallHook, &CarregaUpdateHook
		);

	DoMethod(ObjectApp->App,
		MUIM_Notify, MUIA_Application_Iconified, TRUE,
		ObjectApp->App,
		2,
		MUIM_CallHook, &CarregaTempoHook
		);

	DoMethod(MNlabel1About,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		ObjectApp->App,
		2,
		MUIM_CallHook, &AcertaAboutHook
		);

	DoMethod(MNlabel1About,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		ObjectApp->JanelaAbout,
		3,
		MUIM_Set, MUIA_Window_Open, TRUE
		);

	DoMethod(MNlabel1AboutMUI,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		ObjectApp->App,
		2,
		MUIM_CallHook, &AboutMUIHook
		);

	DoMethod(MNlabel1Iconified,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		ObjectApp->App,
		2,
		MUIM_CallHook, &AplicacaoHideHook
		);

	DoMethod(MNlabel1Exit,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		ObjectApp->App,
		2,
		MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
		);

	DoMethod(MNlabel1LoadConfiguration,
		MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
		ObjectApp->App,
		2,
		MUIM_CallHook, &CarregaConfigHook
		);

	DoMethod(ObjectApp->JanelaPrincipal,
		MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
		ObjectApp->App,
		2,
		MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
		);

	DoMethod(ObjectApp->BotaoExit,
		MUIM_Notify, MUIA_Pressed, FALSE,
		ObjectApp->App,
		2,
		MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
		);

	DoMethod(ObjectApp->BotaoIconified,
		MUIM_Notify, MUIA_Pressed, FALSE,
		ObjectApp->App,
		2,
		MUIM_CallHook, &AplicacaoHideHook
		);

	DoMethod(ObjectApp->BotaoUpdate,
		MUIM_Notify, MUIA_Pressed, FALSE,
		ObjectApp->App,
		2,
		MUIM_CallHook, &CarregaUpdateHook
		);

	DoMethod(ObjectApp->JanelaPrincipal,
		MUIM_Window_SetCycleChain, ObjectApp->BotaoExit,
		ObjectApp->BotaoIconified,
		ObjectApp->BotaoUpdate,
		0
		);

	DoMethod(ObjectApp->JanelaMsg,
		MUIM_Window_SetCycleChain, 0
		);

	DoMethod(ObjectApp->JanelaAbout,
		MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
		ObjectApp->JanelaAbout,
		3,
		MUIM_Set, MUIA_Window_Open, FALSE
		);

	DoMethod(AboutBotaoOk,
		MUIM_Notify, MUIA_Pressed, FALSE,
		ObjectApp->JanelaAbout,
		3,
		MUIM_Set, MUIA_Window_Open, FALSE
		);

	DoMethod(ObjectApp->JanelaAbout,
		MUIM_Window_SetCycleChain, 0
		);

	set(ObjectApp->JanelaPrincipal,
		MUIA_Window_Open, TRUE
		);


	return(ObjectApp);
}

void DisposeApp(struct ObjApp * ObjectApp)
{
	MUI_DisposeObject(ObjectApp->App);
	FreeVec(ObjectApp);
}
