// RunComm.h: interface for the CRunComm class.
//
//////////////////////////////////////////////////////////////////////

#pragma once

#include <vector>

#include "RUN.H"

#include "structure.h"

#include "DllDefines.h"

//
using stNetAddress = struct _stNetAddress
{
    std::string strIpAddress{ "127.0.0.1" };
    int nPort{ 400 };
};

//
using stComSetting = struct _stComSetting
{
    std::string strCommPort{ "COM1" };
    long lBaudRate{ 9600 };
    std::string strParity{ "None" };
    int nDataBits{ 8 };
    double dStopBits{ 1.0 };
};

class MCCTRDLLCLASS CRunComm : public CRun
{
public:
    CRunComm(int nID, const std::string& strName, const std::vector<stModule>& modules, const std::vector<int>& iSequenceModuleNo);
    virtual ~CRunComm();

    int GetPortNo(bool bMaint = false, int nWhichOne = 0);
    std::string GetIpAddress(bool bMaint = false, int nWhichOne = 0);

    enum
    {
        MAINT_OPEN_COM        = 0x1,
        MAINT_CLOSE_COM       = 0x2,
        MAINT_SEND_COM        = 0x4,
        MAINT_CONNECT_PORT    = 0x8,
        MAINT_DISCONNECT_PORT = 0x10,
        MAINT_SEND_PACKET     = 0x20,
    };

protected:
    bool IsMaintenaceCloseCommPort();
    bool IsMaintenanceSendComm();
    bool IsMaintenaceOpenCommPort();
    std::string GetMaintMsg();
    bool IsMaintenanceDisconnectPort();
    bool IsMaintenanceConnectPort();
    bool IsMaintenanceSendPacket();
    double GetStopBits(bool bMaint = false, int nWhichOne = 0);
    int GetDataBits(bool bMaint = false, int nWhichOne = 0);
    long GetBaudRate(bool bMaint = false, int nWhichOne = 0);
    std::string GetParity(bool bMaint = false, int nWhichOne = 0);
    std::string GetCommPort(bool bMaint = false, int nWhichOne = 0);
    bool GetCommPortSetting(std::string csIniFile, std::string csSectionName, int nWhichOne = 0);
    bool GetNetworkPortSetting(std::string csIniFile, std::string csSectionName, int nWhichOne = 0);
    //  send com data to modules in maintenance mode
    virtual void OnMaintenanceSendCom(LPCTSTR lpcComPort, int iBaudRate, int iDataBits,
        LPCTSTR lpcParity, float fStopBits, LPCTSTR lpcFlowCtrl, LPCTSTR lpcSendMsg);

    //  send open com to modules in maintenance mode
    virtual void OnMaintenanceOpenCom(LPCTSTR lpcComPort, int iBaudRate, int iDataBits,
        LPCTSTR lpcParity, float fStopBits, LPCTSTR lpcFlowCtrl);

    //  send close com to modules in maintenance mode
    virtual void OnMaintenanceCloseCom(LPCTSTR lpcComPort, int iBaudRate, int iDataBits,
        LPCTSTR lpcParity, float fStopBits, LPCTSTR lpcFlowCtrl);

    //  send packet to modules in maintenance mode
    virtual void OnMaintenanceSendPacket(LPCTSTR IpAddress, int iPortNo, LPCTSTR lpcSendMsg);

    //  send connect port to modules in maintenance mode
    virtual void OnMaintenanceConnectPort(LPCTSTR IpAddress, int iPortNo);

    //  send connect port to modules in maintenance mode
    virtual void OnMaintenanceDisconnectPort(LPCTSTR IpAddress, int iPortNo);

    //  write acknowledge message to text file
    void WriteAckToTextFile(std::string strAckMsg);

    virtual void SendPacket(std::string strAckMsg = "");
    virtual void DisconnectPort(std::string strAckMsg = "");
    virtual void ConnectPort(std::string strAckMsg = "");
    virtual void SendComm(std::string strAckMsg = "");
    virtual void CloseCommPort(std::string strAckMsg = "");
    virtual void OpenCommPort(std::string strAckMsg = "");
    void InitCommSettings(LPCTSTR lpcComPort, long lBaudRate, int iDataBits, LPCTSTR lpcParity, float fStopBits, LPCTSTR lpcFlowCtrl);

private:
    // Maintenance use
    char m_cComCommand;
    std::string m_strMaintSendMsg;

    // TCP/IP Maintenance variables
    stNetAddress m_maintNetAddress;
    // Serial Comm Settings for maintenance use

    stComSetting m_maintComSetting;

    // TCP/IP variables for auto run
    std::vector<stNetAddress> m_vecNetAddress;
    // Serial Comm Settings for auto run use
    std::vector<stComSetting> m_vecComSetting;
};
