#pragma once
#include <stdint.h>
#include "protocol_spec.hpp"
#include <strings.h>

class GobotRPCNumberMap {
private:
    int32_t rpc_number_map[16];
public:
    GobotRPCNumberMap();
    ~GobotRPCNumberMap();

    void set(RpcNum rpc_num, uint32_t id);
    uint32_t get(RpcNum rpc_num);
};


enum RPCPackageType {
    REQUEST,
    RESPONSE,
    ERROR
};

struct SMResult {
    bool done;
    uint8_t data[7*4];
    RpcNum rpc_num;
    RPCPackageType type;
};

class GobotRPCStateMashine {
private:
    static const size_t SLOTS = 6;
    static const size_t REQUEST_DATA_BUFFER_SIZE = 7*4;
    static const size_t RESPONSE_DATA_BUFFER_SIZE = 7*4;


    GobotRPCNumberMap rpc_number_map;
    size_t slotCounter;

    uint8_t requestDataBuffer[REQUEST_DATA_BUFFER_SIZE][SLOTS];
    bool requestSegmentArrivedFlags [SLOTS];
    
    uint8_t responseDataBuffer[RESPONSE_DATA_BUFFER_SIZE][SLOTS];
    uint8_t responseSegmentArrivedFlags [SLOTS];

    uint8_t errorDataBuffer[RESPONSE_DATA_BUFFER_SIZE][SLOTS];
    uint8_t errorSegmentArrivedFlags [SLOTS];

public:
    GobotRPCStateMashine();
    ~GobotRPCStateMashine();

    void registerRPC(RpcNum rpc_num);

    void submitFrame(RPCFrame * frame, SMResult * result);

};
