#include <RegAccess.h>

#include <config.h>
#include <configfile.h>

//#include <iostream>

using namespace std;

int RegAccess::ReadAviPlayer(IAviPlayer::Property name, string& res)
{
    if (name >= (int)(sizeof(apdefs) / sizeof(apdefs[0])))
        return -1;

    switch (apdefs[name].flags)
    {
    case RA_STRING:
	res = Registry::ReadString(aviplayerstr, apdefs[name].text,
				   (const char*)(apdefs[name].defvalue));
	//cout << "READ " << res << "   " << name << endl;
	return 0;
    default:
	return -1;
    }
}

int RegAccess::ReadAviPlayer(IAviPlayer::Property name, void* res)
{
    if (name >= (int)(sizeof(apdefs) / sizeof(apdefs[0])))
        return -1;

    int i;
    switch (apdefs[name].flags)
    {
    case RA_BOOL:
    case RA_INT:
	i = Registry::ReadInt(aviplayerstr,
			      apdefs[name].text,
			      *(int*)apdefs[name].defvalue);
	if (!res)
	    return i;

	if (apdefs[name].flags == RA_INT)
	    (int*)res = i;
	else
	    (bool*)res = (bool)i;
	return 0;
    default:
	return -1;
    }
}

int RegAccess::WriteAviPlayer(IAviPlayer::Property name, int val)
{
    if (name >= (int)(sizeof(apdefs) / sizeof(apdefs[0])))
        return -1;
    if (apdefs[name].min != apdefs[name].max
	&& (val < apdefs[name].min || apdefs[name].max < val))
	return -1;

    return Registry::WriteInt(aviplayerstr, apdefs[name].text, val);
}

int RegAccess::WriteAviPlayer(IAviPlayer::Property name, string val)
{
    return Registry::WriteString(aviplayerstr, apdefs[name].text, val);
}

/*
 #ifdef USE_QT
 QFont ReadFont()
 {
 string name=Registry::ReadString("AviPlayer\\Font", "Family", "helvetica");
 int size=Registry::ReadInt("AviPlayer\\Font", "Size", 12);
 int bold=Registry::ReadInt("AviPlayer\\Font", "Bold", QFont::Normal);
 int italic=Registry::ReadInt("AviPlayer\\Font", "Italic", 0);
 int charset=Registry::ReadInt("AviPlayer\\Font", "Charset", QFont::ISO_8859_1);
 return QFont(QString(name.c_str()), size, bold, (bool)italic, (QFont::CharSet)charset);
 }
 void WriteFont(const QFont& font=QFont("Helvetica", 10))
 {
 Registry::WriteString("AviPlayer\\Font", "Family", font.family().ascii());
 Registry::WriteInt("AviPlayer\\Font", "Size", font.pointSize());
 Registry::WriteInt("AviPlayer\\Font", "Bold", font.weight());
 Registry::WriteInt("AviPlayer\\Font", "Italic", font.italic());
 Registry::WriteInt("AviPlayer\\Font", "Charset", font.charSet());
 }
 #endif
 */
/*	int result, status, newkey;
 result=RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\AviPlayer\\Font", 0, 0, 0, 0, 0,
 &newkey, &status);
 if(result!=0)
 return QFont("Helvetica", 10);
 if(status==REG_CREATED_NEW_KEY)
 {
 RegCloseKey(newkey);
 WriteFont();
 return QFont("Helvetica", 10);
 }
 char name[50];
 int size;
 int bold;
 int italic;
 int charset;

 int count=50;
 result=RegQueryValueExA(newkey, "Family", 0, 0, (int*)&name, &count)
;
 if(result!=0)strcpy(name, "Helvetica");

 count=4;
 result=RegQueryValueExA(newkey, "Size", 0, 0, (int*)&size, &count)
;
 if(result!=0)size=10;

 count=4;
 result=RegQueryValueExA(newkey, "Bold", 0, 0, (int*)&bold, &count)
;
 if(result!=0)bold=QFont::Normal;

 count=4;
 result=RegQueryValueExA(newkey, "Italic", 0, 0, (int*)&italic, &count)
;
 if(result!=0)italic=0;

 count=4;
 result=RegQueryValueExA(newkey, "Charset", 0, 0, (int*)&charset, &count)
;
 if(result!=0)charset=QFont::ISO_8859_1;

 return QFont(QString(name), size, bold, (bool)italic, (QFont::CharSet)charset);*/
/*
 int result, status, newkey;
 result=RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\AviPlayer\\Font", 0, 0, 0, 0, 0,
 &newkey, &status);
 if(result!=0)
 return;

 result=RegSetValueExA(newkey, "Family", 0, REG_SZ,
 (int*)font.family().ascii(), font.family().length()+1);
 int val=font.pointSize();
 result=RegSetValueExA(newkey, "Size", 0, REG_DWORD, &val, 4);
 val=font.weight();
 result=RegSetValueExA(newkey, "Bold", 0, REG_DWORD, &val, 4);
 val=font.italic();
 result=RegSetValueExA(newkey, "Italic", 0, REG_DWORD, &val, 4);
 val=font.charSet();
 result=RegSetValueExA(newkey, "Charset", 0, REG_DWORD, &val, 4);
 */

/*
{
    int result, status, newkey;
    result=RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\AviPlayer", 0, 0, 0, 0, 0,
			   &newkey, &status);
    if(result!=0)
	return g_fAsync;
    if(status==REG_CREATED_NEW_KEY)
    {
	RegCloseKey(newkey);
	WriteAsync();
	return g_fAsync;
    }
    float answer=g_fAsync;
    int count=4;
    result=RegQueryValueExA(newkey, "Async", 0, 0, (int*)&answer, &count)
;
    RegCloseKey(newkey);
    return answer;

    int result, status, newkey;
    result=RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\AviPlayer", 0, 0, 0, 0, 0,
			   &newkey, &status);
    if(result!=0)
	return;
    result=RegSetValueExA(newkey, "Async", 0, REG_DWORD, (int*)&async, 4);
    RegCloseKey(newkey);

    RegCloseKey(newkey);
    WriteAsync();
    return 0;

    float answer=0;
    int count=4;
    result=RegQueryValueExA(newkey, "SubAsync", 0, 0, (int*)&answer, &count)
;
    RegCloseKey(newkey);
    return answer;

    int result, status, newkey;
    result=RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\AviPlayer", 0, 0, 0, 0, 0,
			   &newkey, &status);
    if(result!=0)
	return;
    result=RegSetValueExA(newkey, "SubAsync", 0, REG_DWORD, (int*)&async, 4);
    RegCloseKey(newkey);
}
*/
