#include "stdafx.h"
#include "MeteoToolsDefs.h"
#include <direct.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

typedef BMSDLL_DATA& 		(WINAPI *DLL_GetBSM_Para)();
typedef PictureFileList& 	(WINAPI *DLL_GetPicFiles)(int);
typedef IsoLines& 		(WINAPI *DLL_GetPoliLines)();
typedef Coordinates& 		(WINAPI *DLL_GetCoordinates)();
typedef IsoPlane& 		(WINAPI *DLL_GetIsoPlanes)();
typedef ColorMatrix& 		(WINAPI *DLL_GetColorMatrix)();
typedef BMSDSynopList& 		(WINAPI *DLL_GetSynopDataSet)();
typedef MSDFile& 		(WINAPI *DLL_GetMSDFileList)();
typedef MSDFILE& 		(WINAPI *DLL_GetCurrentMSDFile)();
typedef MSDFILE& 		(WINAPI *DLL_GetFirstSynopMSDFile)();
typedef DRAW_PARA& 		(WINAPI *DLL_GetDrawPara)();

#define myPicFiles(folder) 	GetPicFiles(folder)
#define myBMS_Para 		GetBSM_Para()
#define myPoliLines 		GetPoliLines()
#define myCoordinates 		GetCoordinates()
#define myIsoPlanes 		GetIsoPlanes()
#define mySynopList 		GetSynopDataSet()
#define NowDate 		GetCurrentMSDFile()
#define ExistDates 		GetMSDFileList()
#define FirstSynopDate 		GetFirstSynopMSDFile()
#define myDrawPara 		GetDrawPara()

typedef void (WINAPI *DLL_ConnectMeteoCom)(HWND,int);
typedef void (WINAPI *DLL_DisconnectMeteoCom)();

typedef BOOL (WINAPI *DLL_GetDataOfPoint)(int,int,BMSDATA_SET*);
typedef BOOL (WINAPI *DLL_GetSynopDataOfPoint)(int,int,BMSD_SYN*,char *);

typedef int  (WINAPI *DLL_SetSynopField)(time_t);
typedef BOOL (WINAPI *DLL_LoadScreenField)(time_t,int,int,ScreenGridData &);
typedef BOOL (WINAPI *DLL_LoadDataField)(time_t,int,int,BMSDataList &);
typedef void (WINAPI *DLL_LoadPictureList)();

typedef void (WINAPI *DLL_DrawThumbFolders)(HDC,int,CRect &,int,int,int,int);

typedef void (WINAPI *DLL_DrawSeaData)(CDC*,LPRECT,time_t,int,int,float,float,float,float);
typedef void (WINAPI *DLL_DrawSfcData)(CDC*,LPRECT,time_t,int,int,float,float,float,float);
typedef void (WINAPI *DLL_DrawSynData)(CDC*,LPRECT,time_t,int,int,float,float,float,float);

typedef void (WINAPI *DLL_MovePicFile)(int,int,int);
typedef void (WINAPI *DLL_DeletePicFile)(int,int);
typedef void (WINAPI *DLL_RenamePicFileName)(int,int,char*);

typedef void (WINAPI *DLL_DecodeMPfromFile)(char*);
typedef void (WINAPI *DLL_DecodeMPfromClipBoard)(HWND);
typedef time_t (WINAPI *DLL_LoadAllMSDFiles)();
typedef void (WINAPI *DLL_SetCurrentMSDFile)(int);
typedef void (WINAPI *DLL_SetGridDimension)(int,int);


HINSTANCE m_DllHandle=NULL;

DLL_ConnectMeteoCom 	ConnectMeteoCom=NULL;
DLL_DisconnectMeteoCom 	DisconnectMeteoCom=NULL;
DLL_GetBSM_Para 	GetBSM_Para=NULL;

DLL_LoadPictureList 	LoadPictureList=NULL;
DLL_GetPicFiles 	GetPicFiles=NULL;
DLL_DrawThumbFolders 	DrawThumbFolders=NULL;


DLL_GetColorMatrix 	GetColorMatrix=NULL;
DLL_LoadScreenField 	LoadScreenField=NULL;
DLL_LoadDataField 	LoadDataField=NULL;
DLL_SetSynopField 	SetSynopField=NULL;
DLL_GetSynopDataSet 	GetSynopDataSet=NULL;

DLL_GetDataOfPoint 	GetDataOfPoint=NULL;
DLL_GetSynopDataOfPoint GetSynopDataOfPoint=NULL;
DLL_GetPoliLines 	GetPoliLines=NULL;
DLL_GetCoordinates 	GetCoordinates=NULL;
DLL_GetIsoPlanes 	GetIsoPlanes=NULL;

DLL_MovePicFile 	MovePicFile=NULL;
DLL_DeletePicFile 	DeletePicFile=NULL;
DLL_RenamePicFileName 	RenamePicFileName=NULL;

DLL_DrawSeaData 	DrawSeaData=NULL;
DLL_DrawSfcData 	DrawSfcData=NULL;
DLL_DrawSynData 	DrawSynData=NULL;
DLL_DecodeMPfromFile 	  DecodeMPfromFile=NULL;
DLL_DecodeMPfromClipBoard DecodeMPfromClipBoard=NULL;
DLL_GetMSDFileList 	  GetMSDFileList=NULL;
DLL_GetCurrentMSDFile 	  GetCurrentMSDFile=NULL;
DLL_GetFirstSynopMSDFile  GetFirstSynopMSDFile=NULL;
DLL_LoadAllMSDFiles 	  LoadAllMSDFiles=NULL;
DLL_SetCurrentMSDFile 	  SetCurrentMSDFile=NULL;
DLL_SetGridDimension 	  SetGridDimension=NULL;
DLL_GetDrawPara 	  GetDrawPara=NULL;

void WINAPI DeInstallMeteoServerTool()
{
    DisconnectMeteoCom();
    FreeLibrary(m_DllHandle);
}

BOOL WINAPI GetMcString(char* DatBuf)
{
    HKEY hKey;
    long retCode;
    DWORD dat_sodb=255,val_type=REG_SZ;

    DatBuf[0]=0;

    retCode=RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Bonito\\MeteoCom",0,KEY_READ,&hKey);
    if(!retCode)
        {
        retCode=RegQueryValueEx(
                   hKey, 		// handle to key to query
                   "AccessDir", 	// address of name of value to query
                   NULL, 		// reserved
                   &val_type, 		// address of buffer for value type
                   (BYTE*)DatBuf, 	// address of data buffer
                   &dat_sodb 		// address of data buffer size
                   );
        RegCloseKey (hKey); 
        }
    return(retCode);
}

extern char myDIR[256];
BOOL WINAPI InstallMeteoServerTool()
{
     //getcwd(buf,255);      //get the main directory
     //strcpy(myDIR,buf);
    if(m_DllHandle) return TRUE;
    // Get the DLL 

    char buf[512],dlldir[512];
    if(GetMcString(dlldir)) return FALSE;
    sprintf(buf,"%s\\MeteoTools.dll",dlldir);
    m_DllHandle= LoadLibrary(buf);
    if(!m_DllHandle)
        {
        sprintf(buf,"%s\\MeteoTools.dll",myDIR);
        m_DllHandle= LoadLibrary(buf);
        if(!m_DllHandle) return FALSE;
        }


    // Get the routine pointers to the MeteoServiceTool DLL

   ConnectMeteoCom=(DLL_ConnectMeteoCom)GetProcAddress(m_DllHandle,"ConnectMeteoCom");
   DisconnectMeteoCom=(DLL_DisconnectMeteoCom)GetProcAddress(m_DllHandle,"DisconnectMeteoCom");
   GetBSM_Para=(DLL_GetBSM_Para)GetProcAddress(m_DllHandle,"GetBSM_Para");

   LoadPictureList = (DLL_LoadPictureList)GetProcAddress(m_DllHandle,"LoadPictureList");
   GetPicFiles=(DLL_GetPicFiles)GetProcAddress(m_DllHandle,"GetPicFiles");
   DrawThumbFolders=(DLL_DrawThumbFolders)GetProcAddress(m_DllHandle,"DrawThumbFolders");

   GetColorMatrix = (DLL_GetColorMatrix)GetProcAddress(m_DllHandle,"GetColorMatrix");
   LoadScreenField=(DLL_LoadScreenField)GetProcAddress(m_DllHandle,"LoadScreenField");
   LoadDataField=(DLL_LoadDataField)GetProcAddress(m_DllHandle,"LoadDataField");
   SetSynopField=(DLL_SetSynopField)GetProcAddress(m_DllHandle,"SetSynopField");
   GetDataOfPoint = (DLL_GetDataOfPoint)GetProcAddress(m_DllHandle,"GetDataOfPoint");
   GetSynopDataOfPoint = (DLL_GetSynopDataOfPoint)GetProcAddress(m_DllHandle,"GetSynopDataOfPoint");
   GetSynopDataSet = (DLL_GetSynopDataSet)GetProcAddress(m_DllHandle,"GetSynopDataSet");
   GetCoordinates=(DLL_GetCoordinates)GetProcAddress(m_DllHandle,"GetCoordinates");
   GetPoliLines=(DLL_GetPoliLines)GetProcAddress(m_DllHandle,"GetPoliLines");
   GetIsoPlanes=(DLL_GetIsoPlanes)GetProcAddress(m_DllHandle,"GetIsoPlanes");

   MovePicFile=(DLL_MovePicFile)GetProcAddress(m_DllHandle,"MovePicFile");
   DeletePicFile=(DLL_DeletePicFile)GetProcAddress(m_DllHandle,"DeletePicFile");
   RenamePicFileName=(DLL_RenamePicFileName)GetProcAddress(m_DllHandle,"RenamePicFileName");

   DrawSeaData=(DLL_DrawSeaData)GetProcAddress(m_DllHandle,"DrawSeaData");
   DrawSfcData=(DLL_DrawSfcData)GetProcAddress(m_DllHandle,"DrawSfcData");
   DrawSynData=(DLL_DrawSfcData)GetProcAddress(m_DllHandle,"DrawSynData");

   DecodeMPfromFile=(DLL_DecodeMPfromFile)GetProcAddress(m_DllHandle,"DecodeMPfromFile");
   DecodeMPfromClipBoard=(DLL_DecodeMPfromClipBoard)GetProcAddress(m_DllHandle,"DecodeMPfromClipBoard");
   GetMSDFileList=(DLL_GetMSDFileList)GetProcAddress(m_DllHandle,"GetMSDFileList");
   GetCurrentMSDFile=(DLL_GetCurrentMSDFile)GetProcAddress(m_DllHandle,"GetCurrentMSDFile");
   GetFirstSynopMSDFile=(DLL_GetFirstSynopMSDFile)GetProcAddress(m_DllHandle,"GetFirstSynopMSDFile");
   SetCurrentMSDFile=(DLL_SetCurrentMSDFile)GetProcAddress(m_DllHandle,"SetCurrentMSDFile");
   LoadAllMSDFiles=(DLL_LoadAllMSDFiles)GetProcAddress(m_DllHandle,"LoadAllMSDFiles");
   SetGridDimension=(DLL_SetGridDimension)GetProcAddress(m_DllHandle,"SetGridDimension");
   GetDrawPara=(DLL_GetDrawPara)GetProcAddress(m_DllHandle,"GetDrawPara");
    return TRUE;
}