/**************************************

  deskDo -- todolist on your desktop

  (c) 2005 Pixel-Apes.

 **************************************/


function deskDoStorage( desk, server ) 
{
  this.desk   = desk;
  this.server = server;
  this.wrapper = desk.wrapper;
  desk.storage = this;

  this.flash = new FlashStorage( this, this.desk.id );
  this.flash.fake = false;

}

deskDoStorage.prototype = 
{
    tag : "",
    version  : 0,
    _version : 0,
    maxVersion : 0,

    _content : "",

    reset : function()
    {
      this.flash.reset();
    },
    store : function( what )
    {
      this._content = what;
      this._version++;

      this.flash.store( what, this.tag );
      if (this.wrapper.auth)
        this.server.doStore( this.wrapper.user.login, this.tag, what );
    },
    load : function()
    {
      if (this.wrapper.auth)
        this.server.doLoad( this.tag );
    },
    restore : function()
    {
      this.flash.restore( this.tag );
    },

    // multidesking
    switchTo : function( newTag )
    {
      if (newTag == 0) newTag = "";
      if (newTag !== this.tag)
      {
        this.tag = newTag;
        this.restore();
        this.load();
      }
    },
    cleanDesk : function( tag )
    {
      this.flash.store( "", tag );
    },

    //undo
    loadUndo : function( undoVersion )
    {
      if (!undoVersion) undoVersion = this.version-1;
      if (undoVersion < 1) undoVersion = 1;
      if (this.wrapper.auth)
        this.server.doUndo( this.tag, undoVersion );
    },
    updateVersion : function( version, maxVersion )
    {
      this.version  = version;
      this._version = version; // ??? suspicious
      this.maxVersion = maxVersion;
      this.wrapper.updatePatchNo( version );
    },


    // event handlers
    onPatchComplete : function( success, version, edited_by, edited_dt )
    {
      if (success)
      {
        // alert("patch accepted");
        this.updateVersion( version, version );
      }
      else // patch is not accepted
      {
        if (confirm( "Patch is not accepted! \n\n"+
                     "current version {"+version+"}\n\nBy: "+edited_by+"\nOn: "+edited_dt +
                     "\n\nClick OK to try again"))
          this.store( this._content );
      }

      // todo: pretty notification
    },
    onLoadComplete : function( serverContent, version, edited_by, edited_dt, maxVersion )
    {
      // custom desk on clean surface
      if (this._content == "")
      {
        if (serverContent === 0) serverContent = "";
        this.wrapper.desk.buildFromScratch( serverContent );
        this.wrapper.desk.doInitView();
        this.wrapper.desk.doViewAll();
        this.wrapper.desk.save();
      }

      this.updateVersion( version, maxVersion );

      if ((version == maxVersion) && ((this._content == serverContent) || (serverContent === ""))) ; // do nothing
      else this.wrapper.onConflict( this._content, serverContent );
    },
    onFlashRestoreComplete : function( content )
    {
      content = String(content);

      this._content = content;
      this.desk.buildFromScratch( content, "non_empty" );
      this.desk.doInitView();
      this.desk.doViewAll();
    },

    // simple function to end the class definition.
    undef : function( param ) { return param; }
}



