#if !defined SGMANAGER_H
#define SGMANAGER_H

#include "ISGManager.h"
#include "BufferedSocket.h"
#include "SGLogger.h"

#define MAXSOCKBUF  4096
#define SG_END_TERMINATOR   '\n'

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// SGManager.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CSGManager document

/*************************************************************************************/
class ISGManagerObserver;
class CSGManager : public ISGManager, IBufferSocketCallBack
{
public:
    CSGManager();
    virtual ~CSGManager();

    void    EnableEventForAllMode();
    void    DisableEventForAllMode(); // Note: Event Enable in PRODUCTION Mode
    CString GetControlState();
    CString GetLinkState();
    bool    IsOnlineRemote();
    bool    IsEnableCommunicating();
    bool    IsOnlineLocal();
    bool    IsSpoolActive();
    bool    IsConnectWithSecsGem(); // check for connection between handler and Ti secs gem
    bool m_bDataComplete;
    char m_cReceiveData[MAXSOCKBUF];
    char m_RcvBuff[MAXSOCKBUF];
    long m_nBufferPtr;
    void OnReceive(const char *buf, int size);
    void OnDisconnect(int errorCode);
    void ResetReceiveBuffer();
/**********************************************************************/
//  LPTSTR psMsg;
protected:
/******************************* To Hanlder info ***********************/
    CString m_strSecsGemConnection;
    CString m_sSGMainMode;
    CString m_sSGLoginUser;
    CString m_sMachineState;
    bool    m_bIsSecsGemEnabled;

    PROCESS_INFORMATION g_pi;
/**********************************************************************/
/**********************************************************************/
    int m_NumberOfDay;
/**********************************************************************/
public:
/************** Handler Calling Functions *****************************/
    bool ConnectSocketSecsGem();    // To make connection with Independent Secs/Gem
    bool DisconnectSocketSecsGem(); // To disconnect Secs/Gem application

// Secsgem Application Function
    void SetEnableLogFile(bool bEnable = true);
    void SetLogLevel(CString level);
    void SetNumberOfDaysKeepInMemoryForLogFile(int nDays = 30);
    bool SetSecsGemIpAddressAndPort(ISGManagerObserver * pObserver, int iPort, const char *pStrIpAddress, bool bConnect = false);
    bool StartAndConnectSecsGemApplication(ISGManagerObserver *pObserver); // To Start Secs/Gem application and connect
    bool CloseSecsGemApplication();                                        // To close secsgem application
    bool StartSecsGemApplication();                                        // To start Secsgem application

// SecsGem Status Function
    bool GoOnLine();                                                       // To switch to SecsGem Online mode
    bool GoOffline();                                                      // To switch to SecsGem Offline mode
    bool GoRemote();                                                       // To switch to Online/Remote control state
    bool GoLocal();                                                        // To switch to Online/Local control state
    bool SecsGemDisableLink();                                             // To disable SecsGem Link
    bool SecsGemEnableLink();                                              // To enable Secsgem Link
    bool SetMainMode(CString sMode);                                       // To set Main Mode
    bool SetUserLevel(CString sUserLevel);                                 // To set User level
    bool SetProcessState(CString sProcessState);                           // To set process state
    bool UpdateCurrentParameters();                                        // To send current mainmode, user and process state to secsgem appln

// Send Event Function
    bool SendEvent(unsigned int iEventID);                                 // To send Event,iEventReply:0= No need to Watit Event Reply

// Update Variable Function
    bool SetVariableValue(UINT iVariableID, UINT iFormat, int iValue);     // To set  Integer Variable value
    bool SetVariableValue(UINT iVariableID, UINT iFormat, long iValue);    // To set Long variable value
    bool SetVariableValue(UINT iVariableID, UINT iFormat, CString iValue); // To set string variable value
    bool SetVariableValue(UINT iVariableID, UINT iFormat, double iValue);  // To set floating point variable value

// Alarm Function
    bool SendAlarmOn(UINT iAlarmID, CString sAlarmText);                   // To send Alarm On
    bool SendAlarmOff(UINT iAlarmID, CString sAlarmText);                  // To send Alarm Off
    bool ClearAllAlarms();                                                 // To clear all alarms

// Data Set Function
    bool UploadData(CString strName);                                      // To Upload Data
    bool DownloadData(CString strName);                                    // To download data
    bool ResetData();                                                      // To reset data

// Wafer map Function
    bool DownloadWaferMap(CString sWMapID, int nType = 2, int nFormat = 1, int nFNLoc = 0, int nRotAngle = 0, int nOrgLoc = 0, CString strBinCodeEq = "", CString strNullBinCodeEq = "");
    bool UploadWaferMap(CString strName);                                  // To Upload Wafer Map
    bool DownloadStripMap(CString strName);                                // To download strip Map

    bool DisplayTerminalMsgDialog();                                       // To display terminal message
    bool AcknowledgeRemoteCommand(CString sRmtCmd, int iReturnVal);        // To acknowledge remote command

// Process Program Management(Recipe)
    bool DownloadRecipe(CString strName);                                  // To download recipe
    bool UploadRecipe(CString strName);                                    // To Upload recipe
    bool UpdateRecipeList();                                               // To update recipe list

// ***************************************************************
// Verify recipe after download recipe, then send S7F27 for PASS or FAIL
// strResultData = "PASS": successful load recipe
// strResultData = "FAIL": Fail to load recipe
// Note:Do not use this function unless customer request
// ***************************************************************
    bool SendRecipeVerification(CString strResultData); // reply to host

// ***************************************************************
// To Add Recipe
// bRefreshRecipe = true  :Not Send GemPPChangeEvent to host
// bRefreshRecipe = false :Send GemPPChangeEvent with PPCHANGESTATUS(Created) to host
// ***************************************************************
    bool AddRecipe(CString strName, bool bRefreshRecipe = false);

// ***************************************************************
//  After delete Recipe file at C:\\PPDER and D:\\PPDER,
//  then send GemPPChangeEvent with PPCHANGESTATUS(Deleted) to host.
// ****************************************************************
    bool DeleteRecipe(CString strName); // To delete Recipe

// ***************************************************************
// bRefreshRecipe = true  :Delete all recipe at C:\\PPDIR only
// bRefreshRecipe = false :Delete all recipe at C:\\PPDIR and D:\\PPDIR
// ***************************************************************
    bool DeleteAllRecipe(bool bRefreshRecipe = false);

// ***************************************************************
//  Send GemPPChangeEvent with PPCHANGESTATUS(Changed) to host.
//  After edit recipe, need to call this function to inform to host.
// ****************************************************************
    bool EditRecipe(CString strName);

// ***************************************************************
//  Send EPT Module Data to host.
// ****************************************************************
    bool SendEPTData(int nModuleID, CString strTaskName, int nTaskType, int nEPTState, int nBlockedReson, CString strBlockedReasonText);
public:
    void OnReceivedControlState(CString m_sControlState);                           // This function is called when Control State change is received
    void OnReceivedLinkState(CString m_sLinkState);                                 // This function is called when Link State change is received
    void OnReceivedSpoolState(CString m_sSpoolState);                               // This function is called when Spool State change is received
    void OnWaferMapDownloaded(CString m_sWaferMapDownloaded);                       // This function is called when Wafer Map downloaded
    void OnStripMapDownloaded(CString m_sStripMapDownloaded);                       // This function is called when Strip Map downloaded
    void OnWaferMapUploaded(CString m_sWaferMapUploaded);                           // This function is called when Wafer Map Uploaded
    void OnRecipeDownloaded(CString m_sRecipeDownloaded);                           // This function is called when Recipe is downloaded from Host
    void OnRecipeUploaded(CString m_sRecipeUploaded);                               // This function is called when Recipe is uploaded.
    void OnRecipeDeleted(CString m_sRecipeDeleted);                                 // This function is called when Recipe is deleted.
    void OnRecipeAdded(CString m_sAddedRecipe);                                     // This function is called when Recipe is added
    void OnDatasetUploaded(CString m_sDatasetUploaded);                             // This function is called when Data Set uploaded
    void OnDatasetDownloaded(CString m_sDatasetDownloaded);                         // This function is called when Dataset downloaded from Host
    void OnRemoteCommandReceived(CString m_sRemoteCmdReceived, int nReply = 0);     // This function is called when a Remote command is received
    void OnRecipeUpdateNumberOfRecipe(CString m_sNumberOfRecipe);                   // This function is called when Number recipe received
    void OnRecipeUpdateNthRecipe(unsigned int iRecipeNumber, CString m_sNthRecipe); // This function is called to updated Nth Recipe
    void OnRecipeUpdatedList();                                                     // This function is called when updated Recipe List.
    void OnReceivedPendingMessage(CString m_sPendingReadMsg);                       // This function is called whenever message received
    void OnReceivedE10State(CString m_sE10State);                                   // This function is called when E10 State change is received

    void SetSecsGemDataFileFolder(CString m_sSGDataFileFolder);                     // Write secs gem data to this file
    void OnAllRecipeDeleted(CString m_sRecipeDeleted);
    void OnRecipeEdited(CString m_sRecipeEdited);
    void OnECChanged(CString sECID, CString sECData);                               // Thi function is call when host change ECID value.
protected:
    virtual void NotifyHandler(WPARAM wParam, LPARAM lParam);
private:
/************************** private fields **************************/
    int                 m_nSGPort;
    char                m_szSGIpAddress[128];
    ISGManagerObserver *m_pObserver;
/******************************* To Hanlder Info ***********************/
    int     m_iEnableForAllMode;
    CString m_sControlState;
    CString m_sLinkState;
    CString m_sSpoolState;
    CString m_sWaferMapDownloaded;
    CString m_sStripMapDownloaded;
    CString m_sWaferMapUploaded;
    CString m_sRecipeDownloaded;
    CString m_sRecipeUploaded;
    CString m_sRecipeDeleted;
    CString m_sAddedRecipe;
    CString m_sDatasetUploaded;
    CString m_sDatasetDownloaded;
    CString m_sRemoteCmdReceived;
    CString m_sNumberOfRecipe;
    CString m_sNthRecipe;
    CString m_sE10State;
    CString m_sGeneralCmdReceived;

    unsigned int iRecipeNumber;

    CString   m_sPendingReadMsg;
    CString   m_sSecsGemDataFile;
    CSGLogger logger;
private:
/************************** private methods *************************/
    bool        SendMsgToSecsGem(CString pStr);
    bool static SkipToNextData(char **ppStr);
    CString     GetNextData(char **ppStr);
    CBufferedSocket* m_pSocketSecsGem;
};
#endif // !defined(AFX_SGMANAGER_H__3247996A_39A5_48E2_B94A_27CC7247F08C__INCLUDED_)
