//
// Copyright (C) 1999-2002 Toshikaz Hirabayashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// TOSHIKAZ HIRABAYASHI BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Except as contained in this notice, the name of Toshikaz Hirabayashi shall
// not be used in advertising or otherwise to promote the sale, use or other
// dealings in this Software without prior written authorization from
// Toshikaz Hirabayashi.

#include <win/WSDwinPrivateTimer.h>
#include <win/WSDwinAppDev.h>

WSMFclassInit(WSDwinPrivateTimer,WSDprivateTimer);

WSDprivateTimer* _winPrivatetimer_create(){
dbprintf("_winPrivatetimer_create.. start/done %s:%d\n",__FILE__,__LINE__);
  return new WSDwinPrivateTimer;
}

class _winPrivatetimer_init {
  public: _winPrivatetimer_init(){
_dbprintf_init();
dbprintf("_winPrivatetimer_init global contructor.. start/done %s:%d \n",__FILE__,__LINE__);
    WSDprivateTimer::setCreateInstanceHandler((void*)_winPrivatetimer_create);
  };
};

static _winPrivatetimer_init  _winPrivatetimer_init_execute;


WSDwinPrivateTimer::WSDwinPrivateTimer(){
WSMFtrace("WSDwinPrivateTimer 0x%x constructed.. %s:%d\n",this,__FILE__,__LINE__);
}
WSDwinPrivateTimer::~WSDwinPrivateTimer(){
  stopTimer();
}

void WSDwinPrivateTimer::startTimer(){
WSMFtrace("WSDwinPrivateTimer 0x%x started.. %s:%d\n",this,__FILE__,__LINE__);
  _timer_on = True;
  _timer_set();
}

void WSDwinPrivateTimer::stopTimer(){
  _timer_on = False;
  HWND wnd = WSGIwinAppDev()->getHWND();
  KillTimer(wnd,(UINT)this);
}

void WSDwinPrivateTimer::_timer_set(){
  HWND wnd = WSGIwinAppDev()->getHWND();
dbprintf("WSDwinPrivate _timer_set appdev=0x%x wnd=0x%x\n",WSGIwinAppDev(),WSGIwinAppDev()->getHWND());
WSMFtrace("WSDwinPrivate _timer_set appdev=0x%x wnd=0x%x\n",WSGIwinAppDev(),WSGIwinAppDev()->getHWND());
dbprintf("WSDwinPrivate _timer_set this-=0x%x ton=%d addr=0x%x\n",this,_timer_on,&_timer_on);
WSMFtrace("WSDwinPrivate _timer_set this-=0x%x ton=%d addr=0x%x\n",this,_timer_on,&_timer_on);
  UINT ret = SetTimer(wnd,(UINT)this,_timer_rate,NULL);
  if ( ret ==  0 ) {
dbprintf("Error. WSDwinPrivateTimer is no effective...\n");
WSMFtrace("Error. WSDwinPrivateTimer is no effective...\n");
  }
}
void WSDwinPrivateTimer::_timer_callback(UINT ptr){
dbprintf("WSDwinPrivate appdev=0x%x wnd=0x%x\n",WSGIwinAppDev(),WSGIwinAppDev()->getHWND());
WSMFtrace("WSDwinPrivate appdev=0x%x wnd=0x%x\n",WSGIwinAppDev(),WSGIwinAppDev()->getHWND());
WSMFtrace("WSDwinPrivateTimer::_timer_callback 0x%x\n",ptr);
    WSDwinPrivateTimer* obj = (WSDwinPrivateTimer*)ptr;
    if (obj->_work_proc != NULL){
      obj->_work_proc(obj->_data);
    }
    if (obj->_cont != False && obj->_timer_on == True){
//      obj->_timer_set();
    }else{
      obj->stopTimer();
//      obj->_timer_on = False;
//      HWND wnd = WSGIwinAppDev()->getHWND();
//      KillTimer(wnd,(UINT)obj);
    }
    return;
}
