#include "sysheaders.h"
#include "cdpanel.h"
#include "CompactPlayer.h"

/********************************************************
 * CD Index editor
 */

struct Window *ListWin;
Object *ListWinObj;
struct Gadget *ListLayout;

static struct List *TitleList;

static UBYTE artist[40], title[40], track[40], work1[40], work2[40];

STRPTR IndexPath = "Disks";

static UBYTE cd[20];

BOOL
GetIndex( ULONG t, ULONG a1, ULONG a2 )
{
	UBYTE buf[256];
	BPTR f;
	
	/* Read an index file from the disk */
	
	strcpy( buf, IndexPath );
	Sprintf( cd, "ID%02.2ld%06.6lx%06.6lx", t, a1, a2 );
	AddPart( buf, cd, sizeof(buf)-1 );
	
	TOCP[0] = ~0;

	if (f = Open(buf, MODE_OLDFILE))
	{
		ULONG i;
		
		FGets(f, buf, sizeof(buf)-1);
		stccpy(TITLE[0] = &TitleBuffer[0], buf, 40);
		FGets(f, buf, sizeof(buf)-1);
		stccpy(TITLE[1] = &TitleBuffer[40], buf, 40);
		
		for (i = 0 ; i < t ; i++)
		{
			FGets(f, buf, sizeof(buf)-1);
			stccpy(TOCS[i] = &TitleBuffer[(i+2)*40], buf, 40);
		}
		TOCS[i] = NULL;
		
		for (i = 0 ; i < (t+2)*40 ; i++)
		{
			if (TitleBuffer[i] == '\n')
				TitleBuffer[i] = 0;
		}
		
		Close(f);
		
		return TRUE;
	}
	return FALSE;
}

void
SaveIndex(void)
{
	UBYTE name[256];
	BPTR f;
	
	/* Save an index file to disk */
		
	strcpy( name, IndexPath );
	AddPart( name, cd, 256 );

	if (f = Open(name, MODE_NEWFILE))
	{
		ULONG i;
		
		FPuts(f, TITLE[0]);
		FPutC(f, '\n');
		FPuts(f, TITLE[1]);
		FPutC(f, '\n');
		
		for (i = 0 ; TOCS[i] ; i++)
		{
			FPuts(f, TOCS[i]);
			FPutC(f, '\n');
		}

		Close(f);
	}
}

struct Window *
OpenListWindow(ULONG X, ULONG Y)
{
	if (ListWin)
		return ListWin;
		
	/* A very simple layout here. I won't bother even explaining it */

	ListLayout = VGroupObject, LAYOUT_SpaceOuter, TRUE,
		GA_DrawInfo, Dri,
		StartMember, GList[G_Artist] = StringObject,
			STRINGA_WorkBuffer, work1,
			STRINGA_UndoBuffer, work2,
			STRINGA_Buffer, artist,
			STRINGA_MaxChars, 39,
			STRINGA_TextVal, "",
			GA_RelVerify, TRUE,
			GA_ID, G_Artist,
			End,
			CHILD_Label, LabelObject, LABEL_Text, "_Artist", End,
		StartMember, GList[G_Title] = StringObject,
			STRINGA_WorkBuffer, work1,
			STRINGA_UndoBuffer, work2,
			STRINGA_Buffer, title,
			STRINGA_MaxChars, 39,
			STRINGA_TextVal, "",
			GA_RelVerify, TRUE,
			GA_ID, G_Title,
			End,
			CHILD_Label, LabelObject, LABEL_Text, "_Title", End,
		StartMember, GList[G_TitleList] = ListBrowserObject,
			LISTBROWSER_Labels, &dummyList,
			LISTBROWSER_ShowSelected, TRUE,
			GA_RelVerify, TRUE,
			GA_ID, G_TitleList,
			End,
			CHILD_MinHeight, ScreenFont->tf_YSize * 6,
			CHILD_MinWidth, ScreenFont->tf_XSize * 20,
		StartMember, GList[G_Track] = StringObject,
			STRINGA_WorkBuffer, work1,
			STRINGA_UndoBuffer, work2,
			STRINGA_Buffer, track,
			STRINGA_MaxChars, 39,
			STRINGA_TextVal, "",
			GA_Disabled, TRUE,
			GA_RelVerify, TRUE,
			GA_ID, G_Track,
			End,
		StartMember, GList[G_SaveTitles] = ButtonObject,
			GA_Text, "_Save titles",
			GA_RelVerify, TRUE,
			GA_Disabled, TRUE,
			GA_ID, G_SaveTitles,
			End,
			CHILD_NominalSize, TRUE,
			CHILD_WeightedWidth, TRUE,
		End;
	
	if (ListLayout)
	{
		if (ListWinObj = WindowObject,
				WA_DepthGadget,		TRUE,
				WA_CloseGadget,		TRUE,
				WA_DragBar,			TRUE,
				WA_Activate,		TRUE,
				WA_Top,				Y,
				WA_Left,			X,
				WA_PubScreen,		Scr,
				WA_Title,			"Title Editor",
				WA_ScreenTitle,		"CompactPlayer Copyright 1995 Osma Ahvenlampi",
				WA_AutoAdjust, 		TRUE,
				WINDOW_Layout, 		ListLayout,
				TAG_END))
		{
			if (ListWin = CA_OpenWindow(ListWinObj))
				ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Artist]);
			
			return ListWin;
		}
		DisposeObject(ListLayout);
		ListLayout = NULL;
	}
	ErrorMsg("Failed to create GUI");
}

void
CloseListWindow(void)
{
	if (ListLayout)
	{
		SafeSetGadgetAttrs(GList[G_TitleList], Win, NULL, LISTBROWSER_Labels, ~0, TAG_END);
		FreeBrowserNodes(TitleList);
		TitleList = NULL;
	}
	if (ListWinObj)
	{
		DisposeObject(ListWinObj);
		ListWin = NULL;
		ListWinObj = NULL;
		ListLayout = NULL;
		memset(&GList[G_Artist], 0, &GList[G_MAX]-&GList[G_Artist]);
	}
}

void
ListWindowIDCMP(void)
{
	BOOL close = FALSE;
	ULONG result;
	
	while ((result = CA_HandleInput(ListWinObj,NULL)) != WMHI_LASTMSG)
	{
		/* CA_HandleInput (aka. WM_HANDLEINPUT) will translate keyboard events
		 * into GADGETUPs on its own */
		switch (result & WMHI_CLASSMASK)
		{
		case WMHI_CLOSEWINDOW:
			close = TRUE;
			break;
			
		case WMHI_GADGETUP:
			/* changing a gadget this window is reflected in the other window */
			switch(result & WMHI_GADGETMASK)
			{
			  case G_Artist:
				SafeSetGadgetAttrs(GList[G_Panel], Win, NULL,
							   CDPANEL_Artist, "",
							   TAG_END);
				strcpy(TITLE[0], artist);
				SafeSetGadgetAttrs(GList[G_Panel], Win, NULL,
							   CDPANEL_Artist, TITLE[0],
							   TAG_END);
				ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Title]);
			  	break;
			  	
			  case G_Title:
				{
					struct Node *n = TitleList->lh_Head;
					
					SafeSetGadgetAttrs(GList[G_Panel], Win, NULL,
								   CDPANEL_Title, "",
								   TAG_END);
					strcpy(TITLE[1], title);
					SafeSetGadgetAttrs(GList[G_Panel], Win, NULL,
								   CDPANEL_Title, TITLE[1],
								   TAG_END);
					if (SafeSetGadgetAttrs(GList[G_TitleList], ListWin, NULL, 
											LISTBROWSER_Selected, 0,
											LISTBROWSER_MakeVisible, 0,
											TAG_END))
						RefreshGList(GList[G_TitleList], ListWin, NULL, 1);

		  			if (SafeSetGadgetAttrs(GList[G_Track], ListWin, NULL,
		  						STRINGA_TextVal, n->ln_Name,
		  						GA_Disabled, FALSE,
		  						TAG_END));
		  				RefreshGList(GList[G_Track], ListWin, NULL, 1);
		  		
					ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Track]);
				  	break;
				}
			  	
			  case G_TitleList:
			  	{
			  		struct Node *n;
			  		SafeGetAttr(LISTBROWSER_SelectedNode, GList[G_TitleList], (ULONG *)&n);
			  		if (n)
			  		{
			  			if (SafeSetGadgetAttrs(GList[G_Track], ListWin, NULL,
			  						STRINGA_TextVal, n->ln_Name,
			  						GA_Disabled, FALSE,
			  						TAG_END));
			  				RefreshGList(GList[G_Track], ListWin, NULL, 1);
			  		
			  			ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Track]);
			  		}
			  		else
			  		{
			  			if (SafeSetGadgetAttrs(GList[G_Track], ListWin, NULL,
			  						STRINGA_TextVal, "",
			  						GA_Disabled, TRUE,
			  						TAG_END));
			  				RefreshGList(GList[G_Track], ListWin, NULL, 1);
			  		}
			  	}
			  	break;
			  
			  case G_Track:
			  	{
			  		struct Node *n;
			  		ULONG i;
			  		SafeGetAttr(LISTBROWSER_SelectedNode, GList[G_TitleList], (ULONG *)&n);
			  		SafeGetAttr(LISTBROWSER_Selected, GList[G_TitleList], &i);
			  		
			  		if (i == ~0 || !n)
			  			break;
	
					SafeSetGadgetAttrs(GList[G_List], Win, NULL, 
										LISTBROWSER_Labels, ~0, 
										TAG_END);
										
					SafeSetGadgetAttrs(GList[G_TitleList], Win, NULL, 
										LISTBROWSER_Labels, ~0, 
										TAG_END);
					
					strcpy(TOCS[i], track);
	
			  		if (TrackList)
			  		{
						FreeBrowserNodes(TrackList);
						
						TrackList = BrowserNodesA(TOCS);
	
						if (SafeSetGadgetAttrs(GList[G_List], Win, NULL, 
											LISTBROWSER_Labels, TrackList, 
											TAG_END))
							RefreshGList(GList[G_List], Win, NULL, 1);
			  		}
	
		  			SetListBrowserNodeAttrs(n, LBNCA_Text, TOCS[i], TAG_END);
		  		
					if (SafeSetGadgetAttrs(GList[G_TitleList], ListWin, NULL, 
											LISTBROWSER_Labels, TitleList, 
											TAG_END))
						RefreshGList(GList[G_TitleList], ListWin, NULL, 1);
			  		
			  		n = n->ln_Succ;
			  		if (n->ln_Succ)
			  		{
						if (SafeSetGadgetAttrs(GList[G_TitleList], ListWin, NULL, 
												LISTBROWSER_Selected, i+1,
												LISTBROWSER_MakeVisible, i+1,
												TAG_END))
							RefreshGList(GList[G_TitleList], ListWin, NULL, 1);
			  		
			  			if (SafeSetGadgetAttrs(GList[G_Track], ListWin, NULL,
			  						STRINGA_TextVal, n->ln_Name,
			  						TAG_END));
			  				RefreshGList(GList[G_Track], ListWin, NULL, 1);
	
			  			ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Track]);
			  		}
			  		else
			  		{
						if (SafeSetGadgetAttrs(GList[G_TitleList], ListWin, NULL, 
												LISTBROWSER_Selected, ~0,
												TAG_END))
							RefreshGList(GList[G_TitleList], ListWin, NULL, 1);
			  			
			  			if (SafeSetGadgetAttrs(GList[G_Track], ListWin, NULL,
			  						STRINGA_TextVal, "",
			  						GA_Disabled, TRUE,
			  						TAG_END));
			  				RefreshGList(GList[G_Track], ListWin, NULL, 1);
			  		}
			  	}
			  	break;
			  	
			  case G_SaveTitles:
			  	SaveIndex();
			  	break;
			}
			break;
		}
	}
	if (close)
		CloseListWindow();
}

void
UpdateTitleEditor(void)	
{
	/* Changing a CD will trigger this function to update the contents of the
	 * editor. */
	if (SafeSetGadgetAttrs(GList[G_Artist], ListWin, NULL,
					STRINGA_TextVal, TITLE[0],
					TAG_END));
		RefreshGList(GList[G_Artist], ListWin, NULL, 1);
	if (SafeSetGadgetAttrs(GList[G_Title], ListWin, NULL,
					STRINGA_TextVal, TITLE[1],
					TAG_END));
		RefreshGList(GList[G_Title], ListWin, NULL, 1);
	SafeSetGadgetAttrs(GList[G_TitleList], Win, NULL, LISTBROWSER_Labels, ~0, TAG_END);
	FreeBrowserNodes(TitleList);
	TitleList = BrowserNodesA(TOCS);
	if (SafeSetGadgetAttrs(GList[G_TitleList], ListWin, NULL, 
					LISTBROWSER_Labels, TitleList, 
					LISTBROWSER_Selected, ~0, 
					TAG_END))
		RefreshGList(GList[G_TitleList], ListWin, NULL, 1);
	if (SafeSetGadgetAttrs(GList[G_Track], ListWin, NULL,
					GA_Disabled, TRUE,
					TAG_END));
		RefreshGList(GList[G_Track], ListWin, NULL, 1);
	if (SafeSetGadgetAttrs(GList[G_SaveTitles], ListWin, NULL, 
					GA_Disabled, FALSE, 
					TAG_END ))
		RefreshGList(GList[G_SaveTitles], ListWin, NULL, 1);
}
