/************************************************************************
    MeOS - Orienteering Software
    Copyright (C) 2009-2011 Melin Software HB
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Melin Software HB - software@melin.nu - www.melin.nu
    Stigbergsvägen 7, SE-75242 UPPSALA, Sweden

************************************************************************/

#include "stdafx.h"

#include "oEvent.h"
#include "TabAuto.h"
#include "meos_util.h"

MySQLReconnect::MySQLReconnect() : AutoMachine("MySQL-daemon") 
{
  callCount=0;
  hThread=0;
}

MySQLReconnect::~MySQLReconnect()
{
  CloseHandle(hThread);
  hThread=0;
}

bool MySQLReconnect::stop()
{
  if(interval==0)
    return true;

  return MessageBox(0, "If this daemon is stopped, then MeOS will not reconnect to the network. Continue?",
    "Warning", MB_YESNO|MB_ICONWARNING)==IDYES;
}


static volatile DWORD mysqlConnecting=0;
static volatile DWORD mysqlStatus=0;

bool isThreadReconnecting()
{
  return mysqlConnecting!=0;
}

int ReconnectThread(void *v)
{
  if(mysqlConnecting) 
    return 0;
  
  OPENDB_FCN msReconnect=static_cast<OPENDB_FCN>(v);

  if(!msReconnect)
    return 0;

  mysqlConnecting=1;
  mysqlStatus=0;
  
  if( msReconnect() ) 
    mysqlStatus=1;     
  else
    mysqlStatus=-1;

  mysqlConnecting=0;

  return 0;
}

void MySQLReconnect::process(gdioutput &gdi, oEvent *oe, AutoSyncType ast)
{
  if(mysqlConnecting)
    return;
  else if (mysqlStatus==1) {
    if(hThread){
        CloseHandle(hThread);
        hThread=0;
    }
    mysqlStatus=0;
    char bf[256];
    if (!oe->reConnect(bf)) {
      gdi.addInfoBox("", string("VARNING. Problem med databasen: ") + bf + " Anslutningen kommer automatiskt att återställas. Fortsätt arbeta som vanligt.", 9000);    
    }
    else {
      gdi.addInfoBox("", "Återansluten mot databasen. Tävlingen synkroniserad.", 5000);
      interval=0;
    }
  }
  else if (mysqlStatus==-1) {
    if(hThread){
        CloseHandle(hThread);
        hThread=0;
    }
    mysqlStatus=0;
    callCount=10; //Wait ten seconds for next attempt

    char bf[256];
    if (oe->msGetErrorState) {
      oe->msGetErrorState(bf);
      gdi.addInfoBox("", string("VARNING. Problem med databasen: ") + bf + " Anslutningen kommer automatiskt att återställas. Fortsätt arbeta som vanligt.", 9000);    
    }
    return;
  }
  else if(callCount<=0) {    
    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ReconnectThread,
          oe->msReConnect, 0, NULL);
    Sleep(100);
  }
  else callCount--;
}

void MySQLReconnect::status(gdioutput &gdi)
{
  gdi.dropLine(0.5);
	gdi.addString("", 1, name);
  gdi.fillRight();
	gdi.pushX();
	if(interval>0){		
		gdi.addString("", 0, "Nästa försök:");
		gdi.addTimer(gdi.getCY(),  gdi.getCX()+10, timerCanBeNegative, (GetTickCount()-timeout)/1000);
	}
  gdi.popX();
  gdi.fillDown();
  gdi.dropLine();
}
