/********************************************************************************
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
*
* This program and the accompanying materials are made available under the 
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0 
********************************************************************************/

#include "../include/can_com_test_helper.h"

void ReceiveData()
{
    sdv::app::CAppControl appControl;
    appControl.Startup("");

    sdv::SObjectInfo sObjectInfo{};
    sObjectInfo.ssConfig = R"(
    DebugInfo = true
    SyncMode = true
    SilKitParticipantName = "can_reader"
    CanSilKitNetwork = "PrivateCAN"
    RegistryURI = "silkit://localhost:8500"
    SilKitConfig = """{
                        "Logging": {
                            "Sinks": [ { "Type": "Stdout", "Level": "Info" } ]
                   },
       }"""                      
    )"; 

    CTestCanSilKit canComObj;
    canComObj.Initialize(sObjectInfo);
    canComObj.SetOperationMode(sdv::EOperationMode::configuring);

    CanReceiver mockRcv; 
    canComObj.RegisterReceiver(&mockRcv);
    canComObj.SetOperationMode(sdv::EOperationMode::running);
    
    while(true)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        auto receivedMessages = mockRcv.GetReceivedMessages();
        if(receivedMessages.size() != 0)
        {
            for(auto rcvdMsg : receivedMessages)
            {          
                std::stringstream buffer;
                buffer << "\n[RX] CAN message being received [CANID=";
                buffer << rcvdMsg.uiID; buffer << ", Length=" ;
                buffer << unsigned(rcvdMsg.seqData.size()); buffer << "]" ;

                buffer << " Data=";
                for(uint16_t i = 0; i < rcvdMsg.seqData.size(); i++)
                {
                    buffer << "[" ; buffer << std::hex << unsigned(rcvdMsg.seqData[i]); buffer << "] ";
                }
                std::cout << buffer.str().c_str();
                mockRcv.ClearReceivedMessages();
            }
        }
        else
        {
            std::cout << "[RX] Received Message size is " << receivedMessages.size() << std::endl;
        }
    }

    canComObj.UnregisterReceiver(&mockRcv);
    std::cout << "CAN has read all messages\n";

    canComObj.Shutdown();
}

int main()
{
    ReceiveData();
    return 0;
}