/******************************************************************************
    Function Name    :  vSaveOWandGetDetails
    Input(s)         :
    Output           :
    Functionality    :  Save to OW buffer and provide the details requested
                        by receive child/ dll user class
    Member of        :  CMsgContainerCAN
    Friend of        :      -
    Author(s)        :  Anish kumar
    Date Created     :  01.04.2010
******************************************************************************/
void CMsgContainerCAN::vSaveOWandGetDetails( void* pMsg,
        __int64& dwMapIndex,
        __int64& dwTimeStamp,
        UINT& nMsgCode,
        int& nBufferIndex, EINTERPRET_MODE& interPretable)
{
    STCANDATA* pouCANData = (STCANDATA*)pMsg;
    dwTimeStamp = pouCANData->m_lTickCount.QuadPart;
    nMsgCode   = pouCANData->m_uDataInfo.m_sCANMsg.m_unMsgID;
    dwMapIndex =  nCreateMapIndexKey((LPVOID)pouCANData);


    interPretable = MODE_NONE;
    if (nullptr != mBmNetwork)
    {
        int nCount = 0;

        IFrame* pouFrame = nullptr;
        mBmNetwork->GetFrame(CAN, 0, pouCANData->m_uDataInfo.m_sCANMsg.m_unMsgID, nullptr, &pouFrame);
        if (nullptr != pouFrame)
        {
            if (pouFrame->GetSignalCount() > 0)
            {
                interPretable = INTERPRETABLE;
            }
        }
    }
    //Now write into the array
    m_ouOWCanBuf.WriteIntoBuffer(pouCANData, dwMapIndex, nBufferIndex);
}
/******************************************************************************
    Function Name    :  vSaveOWandGetDetails
    Input(s)         :
    Output           :
    Functionality    :  Save to OW buffer and provide the details requested
                        by receive child/ dll user class
    Member of        :  CMsgContainerJ1939
    Friend of        :      -
    Author(s)        :  Arun kumar K
    Date Created     :  20.01.2011
******************************************************************************/
void CMsgContainerJ1939::vSaveOWandGetDetails( void* pMsg,
        __int64& dwMapIndex,
        __int64& dwTimeStamp,
        UINT& nMsgCode,
        int& nBufferIndex, EINTERPRET_MODE& interPretable)
{
    STJ1939_MSG* pouJ1939Data = (STJ1939_MSG*)pMsg;
    dwTimeStamp = pouJ1939Data->m_sMsgProperties.m_un64TimeStamp;
    nMsgCode   = pouJ1939Data->m_sMsgProperties.m_uExtendedID.m_s29BitId.unGetPGN();
    dwMapIndex =  nCreateMapIndexKey((LPVOID)pouJ1939Data);

    interPretable = MODE_NONE;
    if (nullptr != mBmNetwork)
    {
        int nCount = 0;

        IFrame* pouFrame = nullptr;
        mBmNetwork->GetFrame(J1939, 0, nMsgCode, nullptr, &pouFrame);
        if (nullptr != pouFrame)
        {
            if (pouFrame->GetSignalCount() > 0)
            {
                interPretable = INTERPRETABLE;
            }
        }
    }

    //Now write into the array
    m_ouOWJ1939Buf.WriteIntoBuffer(pouJ1939Data, dwMapIndex, nBufferIndex);
}
/******************************************************************************
Function Name  :  vUpdateSignals
Input(s)       :  CVerify_MessageData& ouData
Output         :  INT
Functionality  :
Member of      :  CVerify_MessageEntity
Friend of      :  -
Author(s)      :  Venkatanarayana Makam, GT-Derka
Date Created   :  06/04/2011
Modifications  :
******************************************************************************/
INT CVerify_MessageEntity::vUpdateSignals(CVerify_MessageData& ouData)
{
    IFrame* sMsg = nullptr;
    INT nRetVal = m_ouDataBaseManager.nGetMessageInfo(ouData.m_omMessageName, &sMsg);
    if(nRetVal != S_OK)
    {
        return nRetVal;
    }
    UINT unSignalCount = sMsg->GetSignalCount();


    UINT unTSSignalCount = (UINT)ouData.m_odSignalConditionList.GetCount();

    CVerify_MessageData ouTempSignalData;
    ouTempSignalData.m_dwMessageID = ouData.m_dwMessageID;
    ouTempSignalData.m_eSignalUnitType = ouData.m_eSignalUnitType;
    ouTempSignalData.m_omMessageName = ouData.m_omMessageName;
    ouTempSignalData.m_byChannelNumber = ouData.m_byChannelNumber;

    std::map<ISignal*, SignalInstanse> signalList;
    sMsg->GetSignalList( signalList );
    std::string signalName;
for (auto signal : signalList )
    {
        CSignalCondition ouSignalData;
        signal.first->GetName( signalName );
        ouSignalData.m_omSigName = signalName.c_str();
        ouSignalData.m_omCondition = "";
        ouTempSignalData.m_odSignalConditionList.AddTail(ouSignalData);
    }
    for(UINT i = 0; i < unTSSignalCount; i++)
    {
        POSITION pos = ouData.m_odSignalConditionList.FindIndex(i);
        CSignalCondition& ouTSSignalData =  ouData.m_odSignalConditionList.GetAt(pos);
        for(UINT j = 0; j <unSignalCount; j++)
        {
            POSITION pos = ouTempSignalData.m_odSignalConditionList.FindIndex(j);
            CSignalCondition& ouSignalData =  ouTempSignalData.m_odSignalConditionList.GetAt(pos);
            if(ouSignalData.m_omSigName == ouTSSignalData.m_omSigName)
            {
                ouSignalData.m_omCondition = ouTSSignalData.m_omCondition;
                break;
            }
        }
    }
    ouData = ouTempSignalData;
    return 0;
}
/******************************************************************************
Function Name  :  GetData
Input(s)       :  MSXML2::IXMLDOMNodePtr& pIDomNode
Output         :  HRESULT
Functionality  :  Fill the datastructure from xml node pIDomNode
Member of      :  CVerify_MessageEntity
Friend of      :  -
Author(s)      :  Venkatanarayana Makam, GT-Derka
Date Created   :  06/04/2011
Modifications  :
******************************************************************************/
HRESULT CVerify_MessageEntity::GetData(MSXML2::IXMLDOMNodePtr& pIDomNode)
{
    IXMLDOMNode* pIDOMChildNode;
    LONG lCount;
    CComBSTR bstrNodeName;
    CComVariant NodeValue;
    CString omstrTemp;
    CSignalCondition ouSignalCondition;
    IXMLDOMNamedNodeMapPtr pIDOMAttributes;
    pIDOMAttributes = pIDomNode->Getattributes();// get_attributes((IXMLDOMNamedNodeMap**)&pIDOMAttributes);

    //Retrieving Message ID
    bstrNodeName = def_STR_TCATTRIB_ID;
    pIDOMAttributes->getNamedItem(bstrNodeName, &pIDOMChildNode);
    pIDOMChildNode->get_nodeTypedValue(&NodeValue);
    omstrTemp = strCopyBSTRToCString(NodeValue);
    m_ouData.m_dwMessageID = atoi((LPCSTR)omstrTemp);
    m_ouData.m_omMessageName = omstrTemp;
    if(m_ouDataBaseManager.bIsValidMessageID(m_ouData.m_dwMessageID)== FALSE)
    {
        //TODO::INVALID MSG POSSIBLE ONLY WHEN THE FILE IS EDITED WITH NOTEPAD.
        return -1;
    }
    pIDOMChildNode->Release();

    //Retrieving Message UNIT
    bstrNodeName = _(def_STR_TCATTRIB_UNIT);
    pIDOMAttributes->getNamedItem(bstrNodeName, &pIDOMChildNode);
    pIDOMChildNode->get_nodeTypedValue(&NodeValue);
    omstrTemp = strCopyBSTRToCString(NodeValue);
    if(omstrTemp == "RAW")
    {
        m_ouData.m_eSignalUnitType = RAW;
    }
    else                                    // else if(omstrTemp == "ENG")
    {
        m_ouData.m_eSignalUnitType = ENG;
    }
    pIDOMChildNode->Release();

    //Retrieving Message Channel
    bstrNodeName = def_STR_TCATTRIB_CHANNEL;
    pIDOMAttributes->getNamedItem(bstrNodeName, &pIDOMChildNode);
    m_ouData.m_byChannelNumber = 0;                 // set default-value for the case, the number is incorrect or the whole argument is missing
    if (NULL != pIDOMChildNode)                     // avoid crash in case XML-file -without channel-information- is loaded
    {
        pIDOMChildNode->get_nodeTypedValue(&NodeValue);
        omstrTemp = strCopyBSTRToCString(NodeValue);
        m_ouData.m_byChannelNumber = atoi((LPCSTR)omstrTemp);
        pIDOMChildNode->Release();
    }
    if(m_ouData.m_byChannelNumber == 0)             // if casting fails (failure in xml)
    {
        m_ouData.m_byChannelNumber = 1; // set default channel */
        m_lDefaultChannelUsed = 1;
    }

    //Retrieving Signals and their Data

    IFrame* sMsg;
    IXMLDOMNodeListPtr pIDOMSignalList;
    IXMLDOMNode* pIDOMSChildSignal;

    bstrNodeName = _(def_STR_SIGNAL_NODE);
    pIDOMSignalList = pIDomNode->selectNodes((_bstr_t)bstrNodeName);
    pIDOMSignalList->get_length(&lCount);


    INT nRetVal;
    nRetVal = m_ouDataBaseManager.nGetMessageName(m_ouData.m_dwMessageID, m_ouData.m_omMessageName);
    if(nRetVal != S_OK)
    {
        return nRetVal;
    }

    nRetVal =  m_ouDataBaseManager.nGetMessageInfo(m_ouData.m_omMessageName, &sMsg);
    if ( sMsg == nullptr )
    {
        return nRetVal;
    }

    UINT unSignalCount = sMsg->GetSignalCount();
    //W4 Removal - Local variable not required
    //UINT unTSSignalCount = lCount;
    std::map<ISignal*, SignalInstanse> signalList;
    sMsg->GetSignalList( signalList);
    std::string signalName;
for (auto signal : signalList )
        //for(UINT i = 0; i < unSignalCount; i++)
    {
        CSignalCondition ouSignalData, ouTSSignalData;
        signal.first->GetName( signalName );
        ouSignalData.m_omSigName = signalName.c_str();
        ouSignalData.m_omCondition = "";
        for(int i = 0; i < lCount; i++)
        {
            pIDOMSignalList->get_item(i, &pIDOMSChildSignal);
            vRetrieveConditionSignalValue(pIDOMSChildSignal, ouTSSignalData);
            if(ouSignalData.m_omSigName == ouTSSignalData.m_omSigName)
            {
                ouSignalData.m_omCondition = ouTSSignalData.m_omCondition;
                break;
            }
        }
        m_ouData.m_odSignalConditionList.AddTail(ouSignalData);
    }
    return S_OK;
}