/******************************************************************************
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    :  vGetSignalList
    Input(s)         :  ETYPE_BUS eBus,int nChannel, DWORD dwMsgCode,std::list<std::string>& lstSigNames
    Output           :  void
    Functionality    :  Gets list of Signal names for the given Message Code.
    Member of        :  CExploreMsgSg
    Author(s)        :  Robin G.K.
    Date Created     :  27.10.2015
    Requirement ID   :  RS_FLX_08_09_10
***************************************************************************************/
void CExploreMsgSg::vGetSignalList(ETYPE_BUS eBus,int nChannel, DWORD dwMsgCode,std::list<std::string>& lstSigNames)
{
    ICluster* pCluster = nullptr;
    IBMNetWorkGetService* pouClusterConfig = m_pGlobalObj->m_ouClusterConfig;
    if(nullptr!=pouClusterConfig)
    {
        pouClusterConfig->GetDBService(eBus, nChannel, 0, &pCluster);
    }
    IFrame* ouframeStrct = nullptr;
    unsigned int unFrameId = dwMsgCode;
    switch (eBus)
    {
        case CAN:
        case LIN:
        case J1939:
            if (nullptr != pCluster)
            {
                pCluster->GetFrame(unFrameId, nullptr, &ouframeStrct);
            }
            break;
    }
	
	std::map< ISignal*, SignalInstanse> mapSignals;

    if(nullptr != pCluster && nullptr != ouframeStrct)
    {
        ouframeStrct->GetSignalList(mapSignals);
    }

    std::string omSignalName;
	for(auto itrSignal : mapSignals)
    {
        (itrSignal.first)->GetName(omSignalName);
        lstSigNames.push_back(omSignalName);
    }

}
/******************************************************************************
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;
}