#include <applicat.h>
#include <config.h>
#include <execp.h> 
#include <golem.h> 
#include <mask.h> 
#include <prefix.h>
#include <relation.h>
#include <urldefid.h>
#include <utility.h>

#include <bagn006.h>

long TGolem::_count = 0;
TFilename* TGolem::_path = NULL;
TConfig* TGolem::_config = NULL;
TRelation* TGolem::_golem = NULL;

TGolem::TGolem(const char* cls, long id)
: _class(cls), _id(id)
{                   
  CHECK(_class.not_empty() && id >= 0, "Invalid Golem creation");
  _class.upper();

  if (_count == 0)
  { 
    CHECK(_path == NULL, "Golem construction count error");
    _path = new TFilename;
    _config = new TConfig(CONFIG_GOLEM, _class);
    _golem = new TRelation(LF_GOLEM);
  }
  _count++;
}

TGolem::~TGolem()
{
  _count--;
  if (_count == 0)
  {
    CHECK(_path != NULL, "Golem destruction count error");
    delete _golem; _golem = NULL;
    delete _config; _config = NULL;
    delete _path; _path = NULL;
  }
}

TConfig& TGolem::config() const
{
  _config->set_paragraph(_class);
  return *_config;
}


bool TGolem::ok() const
{
  return _id > 0 && fexist(path());
} 

const char* TGolem::class_name() const
{
  *_path = "GOLEM_";
  *_path << _class << "_CLASS";
  return *_path;
}

word TGolem::class_id() const
{
  return CLASS_GOLEM;
}

short TGolem::icon() const
{
  const short id = config().get_int("Icon", NULL, -1, DLG_F9);
  return id;
}


const char* TGolem::ext() const
{
  return config().get("Extension", NULL, -1, _class.left(3));
}


const TFilename& TGolem::path(bool test) const
{
  const char* e = ext();
  *_path = firm2dir(-1);           // C:\PRASSI\DATI
  _path->add("golem");             // C:\PRASSI\DATI\GOLEM
  if (test && !fexist(*_path))
    make_dir(*_path);
  
  _path->add(_class);              // C:\PRASSI\DATI\GOLEM\BITMAP
  if (test && !fexist(*_path))
    make_dir(*_path);
  
  if (_id > 0)
  {
    _path->add(format("%ld", _id));// C:\PRASSI\DATI\GOLEM\BMP\883
    _path->ext(e);                 // C:\PRASSI\DATI\GOLEM\BMP\883.BMP
  }  
  return *_path;
}

int TGolem::compare(const TSortable& s) const
{
  if (s.class_id() != class_id())
    return UNDEFINED;
  const TGolem& g = (const TGolem&)s;  
  return int(_id - g._id);
}

bool TGolem::edit()
{
  const TFilename& p = path();
  bool ok = fexist(p);
  if (ok)
  {
    TFilename e(config().get("Editor"));
    e << ' ' << p;
    
    TExternal_app app(e);
    ok = app.run(FALSE, FALSE) == 0;
  }
  
  return ok;  
}


long TGolem::new_id() const
{   
  long id = 0;
  
  TLocalisamfile& gol = _golem->lfile();
  gol.zero();
  gol.put("CLASSE", _class);
  gol.put("CHIAVE", "99999999");
  
  const int err = gol.read(_isgteq);
  switch (err) 
  {
  case NOERR:                       
    id = 0; break;
  case _isemptyfile:
    id = 1; break;
  default:  
    if (gol.get("CLASSE") != _class)
      gol.prev();                     
    if (gol.get("CLASSE") == _class)
      id = gol.get_long("CHIAVE")+1;
    else
      id = 1;
    break;    
  }
  return id;
}

bool TGolem::import()
{
  FILE_SPEC fs;
  const char* const e = ext();                             
  xvt_fsys_convert_str_to_dir(".", &fs.dir);
  strcpy(fs.type, e);
  sprintf(fs.name, "*.%s", e);
  strcpy(fs.creator, "GOLEM");
  
  xvt_fsys_save_dir();
  FL_STATUS ok = xvt_dm_post_file_open(&fs, "Selezionare il file ...");
  xvt_fsys_restore_dir();
  if (ok == FL_OK)
  {            
    TFilename from;
    xvt_fsys_convert_dir_to_str(&fs.dir, (char*)(const char*)from, 80);
    from.add(fs.name); from.ext(e);

    TMask msk("bagn006");
    msk.set(F_CLASSE, _class);
    msk.set(F_CODICE, new_id());
    
    bool correct = msk.run() == K_ENTER;
    if (correct)
    {
      _id = msk.get_long(F_CODICE);
      correct = fcopy(from, path(TRUE));
      if (correct)
      {
        msk.autosave(_golem);
        int err =  _golem->write();
        if (err == _isreinsert) 
          err = _golem->rewrite();
        if (err != NOERR)
        {
          error_box("Errore nell'aggiornamento del file degli oggetti");
          ok = FL_BAD;  
        }
      }
      else 
      {            
        error_box("Spazio su disco insufficiente per l'oggetto");
        ok = FL_BAD;  
      }  
    }
    else ok = FL_CANCEL;  
    
  }
  
  return ok == FL_OK;
}


#if XVT_OS == XVT_OS_WIN

#define STRICT
#include <windows.h>
#include <dde.h>

///////////////////////////////////////////////////////////
// DDE
///////////////////////////////////////////////////////////

static TDDE* DDEWIN = NULL;

HIDDEN BOOLEAN hook(HWND hwnd,
                    UINT msg,
                    UINT wparam,
                    ULONG lparam,
                    long* ret)
{
  switch (msg)
  {
  case WM_DDE_ACK:
    DDEWIN->set_server(wparam);
    GlobalDeleteAtom(LOWORD(lparam));
    GlobalDeleteAtom(HIWORD(lparam));
    *ret = TRUE;
    break;
  default:
    break;  
  }
  return TRUE;
}

TDDE::TDDE()
: _old_hook(NULL), _server(0)
{               
  CHECK(DDEWIN == NULL, "Non puoi lanciare due DDE, bestia!");                    
  create(0, 0, 0, 0, "DDE", WSF_INVISIBLE, W_PLAIN, NULL_WIN);
  _hwnd = (word)xvt_vobj_get_attr(win(), ATTR_NATIVE_WINDOW);
  DDEWIN = this;
}

TDDE::~TDDE()
{
  terminate();
  DDEWIN = NULL;
}

bool TDDE::initiate(const char* app, const char* topic)
{ 
  CHECK(_old_hook == NULL, "Non puoi iniziare due connessioni DDE");                    
  _old_hook = xvt_vobj_get_attr(NULL_WIN, ATTR_EVENT_HOOK);  
  xvt_vobj_set_attr(NULL_WIN, ATTR_EVENT_HOOK, (long)hook); 

  _server = 0;
  ATOM a_app = GlobalAddAtom(app);
  ATOM a_topic = GlobalAddAtom(topic);
  SendMessage(HWND_BROADCAST, WM_DDE_INITIATE, (WORD)_hwnd, MAKELPARAM(a_app, a_topic));
  GlobalDeleteAtom(a_app);
  GlobalDeleteAtom(a_topic);
  return _server != 0;
}

bool TDDE::execute(const char* cmd) const
{                                    
  HGLOBAL hg = GlobalAlloc(GMEM_DDESHARE, strlen(cmd)+1);
  char* c = (char*)GlobalLock(hg);
  strcpy(c, cmd);
  GlobalUnlock(hg);
  return PostMessage((HWND)_server, WM_DDE_EXECUTE, (WORD)_hwnd, MAKELPARAM(0, hg));
}


void TDDE::terminate()
{
  if (_old_hook)
  {
    xvt_vobj_set_attr(NULL_WIN, ATTR_EVENT_HOOK, _old_hook);   
    _old_hook = NULL;
    PostMessage((HWND)_server, (WORD)_hwnd, WM_DDE_TERMINATE, 0L);
  }
}

#endif
