#include "wxinc.h"
#include "wx/print.h"
#include "wx/printdlg.h"

#include "xvt.h"
#include "oslinux.h"

#include "xvt_menu.h"
#include "xvt_help.h"
#include "xvintern.h"
#include <wx/fontenum.h>
#include <wx/string.h>
#include <wx/snglinst.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <mntent.h>
#include <sys/vfs.h>

wxString OsLinux_File2App(const char* filename)
{
  wxString app;

  SORRY_BOX();
  return app;
}

int OsLinux_EnumerateFamilies(char** families, int max_count)
{
  wxFontEnumerator ef;

  ef.EnumerateFacenames();
  wxArrayString * fonts = ef.GetFacenames();

  size_t items = fonts->GetCount();
  size_t i;

  for (i = 0; i < items; i++)
    strcpy(families[i], (*fonts)[i].c_str());
	
	return items;
}

int OsLinux_EnumerateSizes(const char* name, long* sizes, short* scalable, int max_count)
{
  int i = 0;
  TFontId f;
  wxPrinter printer;
  wxPostScriptDC dc(printer.GetPrintDialogData().GetPrintData());

  f.SetFaceName(name);
  for (int size = 7; size < 80; size++)
  {
    f.SetPointSize(size);
    if (f.Font((wxDC *) &dc).Ok())
      sizes[i++] = size;
  }

	return i;
}

void OsLinux_PlaceProcessInWindow(unsigned int instance, const char* name, unsigned int parent)
{
  SORRY_BOX();
}

void OsLinux_UpdateWindow(unsigned int handle)
{
  SORRY_BOX();
}

///////////////////////////////////////////////////////////
// Hardlock Support
///////////////////////////////////////////////////////////

#include "hlapi_c.h"

bool OsLinux_HL_Login(unsigned short address, const unsigned char* label, const unsigned char* password)
{
  int err = HL_LOGIN(address, LOCAL_DEVICE, (unsigned char*)label, (unsigned char*)password);
  return err == STATUS_OK;
}

bool OsLinux_HL_Logout()
{
  HL_LOGOUT();
  return TRUE;
}

bool OsLinux_HL_Read(unsigned short reg, unsigned short* data)
{
  int err = HL_READ(reg, data);
  return err == STATUS_OK;
}

bool OsLinux_HL_ReadBlock(unsigned char* data)
{
  int err = HL_READBL(data);
  return err == STATUS_OK;
}

bool OsLinux_HL_Write(unsigned short reg, unsigned short data)
{
  int err = HL_WRITE(reg, data);
  return err == STATUS_OK;
}

bool OsLinux_HL_Crypt(unsigned short* data) // Array di 4 words (8 bytes)
{
  int err = HL_CODE(data, 1);
  return err == STATUS_OK;
}

///////////////////////////////////////////////////////////
// Eutron Smartlink Support
///////////////////////////////////////////////////////////

#include "skeylink.h"

static KEY_NET _eutron_key;

static short smartlink(KEY_NET * key)
{
  SKEY_DATA skey;
  int i;

  skey.lpt = key->lpt;
	skey.command = key->command;
	for (i = 0; i < LABEL_LENGTH; i++)
  	skey.label[i] = key->label[i];
	for (i = 0; i < PASSWORD_LENGTH; i++)
  	skey.password[i] = key->password[i];
	for (i = 0; i < DATA_LENGTH; i++)
	  skey.data[i] = key->data[i];
	skey.fail_counter = key->fail_counter;
	skey.status = key->status;
	for (i = 0; i < EXTENDED_DATA_LENGTH; i++)
	  skey.ext_data[i] = key->ext_data[i];
	
	short retval = clink(&skey);

	key->lpt = skey.lpt;
	key->command = skey.command;
	for (i = 0; i < LABEL_LENGTH; i++)
  	key->label[i] = skey.label[i];
	for (i = 0; i < PASSWORD_LENGTH; i++)
  	key->password[i] = skey.password[i];
	for (i = 0; i < DATA_LENGTH; i++)
	  key->data[i] = skey.data[i];
	key->fail_counter = skey.fail_counter;
	key->status = skey.status;
	for (i = 0; i < EXTENDED_DATA_LENGTH; i++)
	  key->ext_data[i] = skey.ext_data[i];
	return retval;
}

bool OsLinux_SL_Crypt(unsigned short* data)
{
  _eutron_key.net_command = NET_KEY_ACCESS;
  _eutron_key.command = SCRAMBLING_MODE;
  memcpy(_eutron_key.data, data, 8);
  smartlink(&_eutron_key);
  if (_eutron_key.status == ST_OK)
    memcpy(data, _eutron_key.data, 8);
  return _eutron_key.status == ST_OK;
}

bool OsLinux_SL_Login(const unsigned char* label, const unsigned char* password)
{
  memset(&_eutron_key, 0, sizeof(KEY_NET));
  _eutron_key.net_command = NET_KEY_OPEN;
	_eutron_key.status = ST_HW_FAILURE; // Don't leave ST_OK = 0 here!
  memcpy(_eutron_key.label, label, strlen((const char*)label));
  memcpy(_eutron_key.password, password, strlen((const char*)password));

  smartlink(&_eutron_key);
  return _eutron_key.status == ST_OK;
}

bool OsLinux_SL_Logout()
{
  _eutron_key.net_command = NET_KEY_CLOSE;
  _eutron_key.command = 0;
  smartlink(&_eutron_key);
  return true;
}

bool OsLinux_SL_ReadBlock(unsigned short reg, unsigned short size, unsigned short* data)
{
  _eutron_key.net_command = NET_KEY_ACCESS;
  _eutron_key.command = BLOCK_READING_MODE;
  unsigned short* pointer = (unsigned short*)(&_eutron_key.data[0]);
  unsigned short* number  = (unsigned short*)(&_eutron_key.data[2]);
  *pointer = reg;
  *number = size;
  smartlink(&_eutron_key);
  bool ok = _eutron_key.status == ST_OK;
  if (ok)
    memcpy(data, &_eutron_key.data[4], size*sizeof(unsigned short));
  return ok;
}

bool OsLinux_SL_WriteBlock(unsigned short reg, unsigned short size, const unsigned short* data)
{
  _eutron_key.net_command = NET_KEY_ACCESS;
  _eutron_key.command = BLOCK_WRITING_MODE;
  unsigned short* pointer = (unsigned short*)(&_eutron_key.data[0]);
  unsigned short* number  = (unsigned short*)(&_eutron_key.data[2]);
  *pointer = reg;
  *number = size;
  memcpy(&_eutron_key.data[4], data, size*sizeof(unsigned short));
  smartlink(&_eutron_key);
  return _eutron_key.status == ST_OK;
}

void  OsLinux_GetFileSys(const char* path, char * dev, char * dir, char * type)
{
  struct mntent *m;
  FILE *f = setmntent("/etc/mnttab", "r");

  while ((m = getmntent(f)) && strncmp(path, m->mnt_dir, strlen(m->mnt_dir)) != 0);
  if (m)
  {
    if (dev) strcpy(dev, m->mnt_fsname);
    if (dir) strcpy(dir, m->mnt_dir);
    if (type) strcpy(type, m->mnt_type);
  }
  else
  {
    if (dev) *dev = '\0';
    if (dir) *dir = '\0';
    if (type) *type = '\0';
  }
  endmntent(f);
}


bool OsLinux_IsNetworkDrive(const char * path)
{
  struct statfs buf;

  if (statfs(path, &buf) == -1)
    return FALSE;
  return (buf.f_type == 0x6969 /*NFS_SUPER_MAGIC */) ||
         (buf.f_type == 0x517B /*SMB_SUPER_MAGIC)*/);
}

int64_t OsLinux_GetDiskFreeSpace(const char * path)
{
  struct statfs buf;
  int64_t nBytes = 0L;

  if (statfs(path, &buf) != -1)
  {
    nBytes = buf.f_bsize;
    nBytes *= buf.f_bavail;
    nBytes *= 1024;
  }

  return nBytes;
}