 /***************************************************************************************
 *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					*
 * (at your option) any later version.													*
 *																						*
 * The GPL can be found at: http://www.gnu.org/copyleft/gpl.html						*
 *																						*
 *																						*	
 ****************************************************************************************
 * Authors:																			  *
 *          Marc Dukette                                                              *
 **************************************************************************************/

#include "skin.h"
#include "imgdecmp.h"

extern Skin PlayerSkin;

HANDLE SkinFile;
LPSTR SkinInfo;
extern HBITMAP  m_hBitmap;          // Handle to DIBSECTION
extern HBITMAP  m_hBitBack;          // Handle to DIBSECTION
extern HBITMAP  m_hBitButton;          // Handle to DIBSECTION
extern HWND hWndMain;


DWORD CALLBACK GetImageData(LPSTR szBuffer, DWORD dwBufferMax, LPARAM lParam )
{
	DWORD dwNumberOfBytesRead;

	if ( (HANDLE)lParam == INVALID_HANDLE_VALUE )
		return 0;

	ReadFile( (HANDLE)lParam, szBuffer, dwBufferMax, &dwNumberOfBytesRead, NULL );

	// Return number of bytes read
	return dwNumberOfBytesRead;
}


void CALLBACK ImageProgress(IImageRender *pRender, BOOL bComplete, LPARAM lParam )
{
	if( bComplete )
	{
		;// (Optional) add code here for completion processing
	}
}

static HBITMAP LoadBitmap(LPCTSTR pcszFileName)
{
	HDC hdc=GetDC(hWndMain);
	HDC g_hdc= CreateCompatibleDC(hdc);

	HRESULT hr;
	BYTE    szBuffer[1024] = {0};
	HANDLE hFile = INVALID_HANDLE_VALUE;

	DecompressImageInfo	dii;

	hFile = CreateFile(pcszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
	if (hFile == INVALID_HANDLE_VALUE)
		return FALSE;

	// Fill in the 'DecompressImageInfo' structure
	dii.dwSize = sizeof( DecompressImageInfo );		// Size of this structure
	dii.pbBuffer = szBuffer;						// Pointer to the buffer to use for data
	dii.dwBufferMax = 1024;							// Size of the buffer
	dii.dwBufferCurrent = 0;						// The amount of data which is current in the buffer
	dii.phBM = &m_hBitmap;							// Pointer to the bitmap returned (can be NULL)
	dii.ppImageRender = NULL;						// Pointer to an IImageRender object (can be NULL)
	dii.iBitDepth = 24; //GetDeviceCaps(hdc,BITSPIXEL);	// Bit depth of the output image
	dii.lParam = ( LPARAM ) hFile;					// User parameter for callback functions
	dii.hdc = g_hdc;								// HDC to use for retrieving palettes
	dii.iScale = 100;							// Scale factor (1 - 100)
	dii.iMaxWidth = 640;					// Maximum width of the output image
	dii.iMaxHeight = 480;					// Maxumum height of the output image
	dii.pfnGetData = GetImageData;					// Callback function to get image data
	dii.pfnImageProgress = ImageProgress;			// Callback function to notify caller of progress decoding the image
	dii.crTransparentOverride = ( UINT ) -1;		// If this color is not (UINT)-1, it will override the
													// transparent color in the image with this color. (GIF ONLY)

	// Process and decompress the image data
	hr = DecompressImageIndirect( &dii );
		
	// Clean up 
	CloseHandle( hFile );

	BITMAP	bmp;

	GetObject(m_hBitmap, sizeof(BITMAP), &bmp);
	ReleaseDC(hWndMain,hdc);
	DeleteDC(g_hdc);
	return m_hBitmap;

}


bool CheckRect(int x, int y, SkinItem* rc)
{
	if (x>rc->left&&(x<(rc->left+rc->width))&&(y>rc->top)&&(y<(rc->top+rc->height))) return true;
	else return false;

}



static void LoadSkinItem(LPSTR Name,SkinItem* Item)
{

	LPSTR temp=strstr(SkinInfo,Name);
	memset(Item,0,sizeof(SkinItem));
	if (temp)
	{
		LPSTR end=strstr(temp,"\r\n\r\n");
		LPSTR val=NULL;
		int len=end-temp;

		val=strstr(temp,"Left=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=5;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->left=atoi(val);
		}
		val=strstr(temp,"Top=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=4;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->top=atoi(val)+Y_ADJUST;
		}
		val=strstr(temp,"Width=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=6;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->width=atoi(val);
		}
		val=strstr(temp,"Height=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=7;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->height=atoi(val);
		}
		val=strstr(temp,"PushedLeft=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=11;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->pushedleft=atoi(val);
		}
		val=strstr(temp,"PushedTop=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=10;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->pushedtop=atoi(val);
		}
		val=strstr(temp,"BigStep=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=8;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->bigstep=atoi(val);
		}
		val=strstr(temp,"Font=");
		if (val&&(val-temp<len))
		{
			char t[100];
			val+=5;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,t,-1,Item->font,100);
		}
		val=strstr(temp,"PointSize=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=10;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->point=atoi(val);
		}
		val=strstr(temp,"FontRed=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=8;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->FontRed=atoi(val);
		}
		val=strstr(temp,"FontGreen=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=10;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->FontGreen=atoi(val);
		}
		val=strstr(temp,"FontBlue=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=9;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->FontBlue=atoi(val);
		}
		val=strstr(temp,"BackRed=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=8;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->BackRed=atoi(val);
		}
		val=strstr(temp,"BackGreen=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=10;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->BackGreen=atoi(val);
		}
		val=strstr(temp,"BackBlue=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=9;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->BackBlue=atoi(val);
		}
		val=strstr(temp,"ScrollText=");
		if (val&&(val-temp<len))
		{
			char t[4];
			val+=11;
			end=strstr(val,"\r\n");
			strncpy(t,val,end-val);
			t[end-val]=0;
			Item->scrolltext=atoi(val);
		}


		Item->rotated=0;
		if (Item->height>Item->width) Item->rotated=1;
		if (Item->bigstep>0)
			Item->step=1;
		else
			Item->step=-1;
	}
}

static void LoadSkinImages(LPTSTR Path)
{
	LPSTR temp=strstr(SkinInfo,"Files");
	LPSTR end=strstr(temp,"\r\n\r\n");
	LPSTR val=NULL;
	int len=end-temp;

	val=strstr(temp,"Background=");
	if (val&&(val-temp<len))
	{
		char t[255];
		TCHAR t1[255];
		val+=11;
		end=strstr(val,"\r\n");
		strncpy(t,val,end-val);
		t[end-val]=0;
		MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,t,-1,t1,255);
		wcscpy(PlayerSkin.BkgImg,Path);
		wcscat(PlayerSkin.BkgImg,t1);
	}
	val=strstr(temp,"Buttons=");
	if (val&&(val-temp<len))
	{
		char t[255];
		TCHAR t1[255];
		val+=8;
		end=strstr(val,"\r\n");
		strncpy(t,val,end-val);
		t[end-val]=0;
		MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,t,-1,t1,255);
		wcscpy(PlayerSkin.BtnImg,Path);
		wcscat(PlayerSkin.BtnImg,t1);
	}
}

void LoadSkinFile(LPTSTR FileName,HWND hWnd)
{
	unsigned long nPos;
	LPTSTR temp=FileName;
	LPTSTR next;
	LPSTR SkinData;
	TCHAR Path[255];
	SkinFile =CreateFile(FileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if (SkinFile==INVALID_HANDLE_VALUE)
	{
		TCHAR path[200];
		HANDLE h=GetModuleHandle(_T("PocketMVP.exe"));
		GetModuleFileName((HMODULE)h,path,200);
		LPTSTR t=wcsstr(path,_T("PocketMVP."));
		*t=0;
		wcscpy(FileName,path);
		wcscat(FileName,_T("Skins\\PocketMVP.pds"));
		SkinFile =CreateFile(FileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	}
	SkinData=(LPSTR) malloc(GetFileSize(SkinFile,NULL));
	ReadFile(SkinFile,SkinData,GetFileSize(SkinFile,NULL),&nPos,NULL);
	while ((next=wcsstr(temp,_T("\\")))!=NULL)
	{
		next++;
		temp=next;
	}
	wcsncpy(Path,FileName,temp-FileName);
	Path[temp-FileName]=0;
	CloseHandle(SkinFile);
#ifdef HPC
	SkinInfo=strstr(SkinData,"[Handheld]");
#else
	SkinInfo=strstr(SkinData,"[PocketPC]");
#endif

	LoadSkinItem("ViewPort",&PlayerSkin.ViewPort);
	LoadSkinItem("StopButton",&PlayerSkin.StopButton);
	LoadSkinItem("NextButton",&PlayerSkin.NextButton);
	LoadSkinItem("PrevButton",&PlayerSkin.PrevButton);
	LoadSkinItem("PlayButton",&PlayerSkin.PlayButton);
	LoadSkinItem("FFWDButton",&PlayerSkin.FFWDButton);
	LoadSkinItem("InfoView",&PlayerSkin.InfoView);
	LoadSkinItem("SeekArea",&PlayerSkin.SeekSlider);
	LoadSkinItem("SeekButton",&PlayerSkin.SeekButton);
	LoadSkinItem("VolumeArea",&PlayerSkin.VolSlider);
	LoadSkinItem("VolumeButton",&PlayerSkin.VolButton);
	LoadSkinItem("RotateButton",&PlayerSkin.RotateButton);
	LoadSkinItem("FullScreenButton",&PlayerSkin.FullScreenButton);
	LoadSkinItem("EQButton",&PlayerSkin.EQButton);
	LoadSkinItem("RepeatButton",&PlayerSkin.RepeatButton);
	LoadSkinItem("MuteButton",&PlayerSkin.MuteButton);
	LoadSkinItem("OpenButton",&PlayerSkin.OpenButton);
	LoadSkinItem("PlaylistButton",&PlayerSkin.EditorButton);
	LoadSkinItem("Time",&PlayerSkin.Time);
	LoadSkinItem("PlaylistView",&PlayerSkin.PlaylistView);
	LoadSkinItem("MediaView",&PlayerSkin.MediaView);

	LoadSkinImages(Path);
	free(SkinData);
	m_hBitBack=LoadBitmap(PlayerSkin.BkgImg);
	m_hBitButton=LoadBitmap(PlayerSkin.BtnImg);
}