#pragma once

#include "console_buffer.hpp"
#include "session.hpp"

#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <cstddef>

namespace sol
{

/** @struct Outbound
 *
 *  Operation/Status in an outbound SOL payload format(BMC to Remote Console).
 */
struct Outbound
{
#if BYTE_ORDER == LITTLE_ENDIAN
    uint8_t testMode : 2;        //!< Not supported.
    uint8_t breakDetected : 1;   //!< Not supported.
    uint8_t transmitOverrun : 1; //!< Not supported.
    uint8_t SOLDeactivating : 1; //!< 0 : SOL is active, 1 : SOL deactivated.
    uint8_t charUnavailable : 1; //!< 0 : Available, 1 : Unavailable.
    uint8_t ack : 1;             //!< 0 : ACK, 1 : NACK.
    uint8_t reserved : 1;        //!< Reserved.
#endif

#if BYTE_ORDER == BIG_ENDIAN
    uint8_t reserved : 1;        //!< Reserved.
    uint8_t ack : 1;             //!< 0 : ACK, 1 : NACK.
    uint8_t charUnavailable : 1; //!< 0 : Available, 1 : Unavailable.
    uint8_t SOLDeactivating : 1; //!< 0 : SOL is active, 1 : SOL deactivated.
    uint8_t transmitOverrun : 1; //!< Not supported.
    uint8_t breakDetected : 1;   //!< Not supported.
    uint8_t testMode : 2;        //!< Not supported.
#endif
} __attribute__((packed));

/** @struct Inbound
 *
 *  Operation/Status in an Inbound SOL Payload format(Remote Console to BMC).
 */
struct Inbound
{
#if BYTE_ORDER == LITTLE_ENDIAN
    uint8_t flushOut : 1;      //!< Not supported.
    uint8_t flushIn : 1;       //!< Not supported.
    uint8_t dcd : 1;           //!< Not supported.
    uint8_t cts : 1;           //!< Not supported.
    uint8_t generateBreak : 1; //!< Not supported.
    uint8_t ring : 1;          //!< Not supported.
    uint8_t ack : 1;           //!< 0 : ACK, 1 : NACK.
    uint8_t reserved : 1;      //!< Reserved.
#endif

#if BYTE_ORDER == BIG_ENDIAN
    uint8_t reserved : 1;      //!< Reserved.
    uint8_t ack : 1;           //!< 0 : ACK, 1 : NACK.
    uint8_t ring : 1;          //!< Not supported.
    uint8_t generateBreak : 1; //!< Not supported.
    uint8_t cts : 1;           //!< Not supported.
    uint8_t dcd : 1;           //!< Not supported.
    uint8_t flushIn : 1;       //!< Not supported.
    uint8_t flushOut : 1;      //!< Not supported.
#endif
} __attribute__((packed));

/** @struct Payload
 *
 *  SOL Payload Data Format.The following fields make up the SOL payload in an
 *  RMCP+ packet, followed by the console character data.
 */
struct Payload
{
    uint8_t packetSeqNum;      //!< Packet sequence number
    uint8_t packetAckSeqNum;   //!< Packet ACK/NACK sequence number
    uint8_t acceptedCharCount; //!< Accepted character count
    union
    {
        uint8_t operation;            //!< Operation/Status
        struct Outbound outOperation; //!< BMC to Remote Console
        struct Inbound inOperation;   //!< Remote Console to BMC
    };
} __attribute__((packed));

namespace internal
{

/** @struct SequenceNumbers
 *
 *  SOL sequence numbers. At the session level, SOL Payloads share the session
 *  sequence numbers for authenticated and unauthenticated packets with other
 *  packets under the IPMI session. At the payload level, SOL packets include
 *  their own message sequence numbers that are used for tracking missing and
 *  retried SOL messages. The sequence numbers must be non-zero. Retried
 *  packets use the same sequence number as the first packet.
 */
struct SequenceNumbers
{
    static constexpr uint8_t MAX_SOL_SEQUENCE_NUMBER = 0x10;

    /** @brief Get the SOL sequence number.
     *
     *  @param[in] inbound - true for inbound sequence number and false for
     *                       outbound sequence number
     *
     *  @return sequence number
     */
    auto get(bool inbound = true) const
    {
        return inbound ? in : out;
    }

    /** @brief Increment the inbound SOL sequence number. */
    void incInboundSeqNum()
    {
        if ((++in) == MAX_SOL_SEQUENCE_NUMBER)
        {
            in = 1;
        }
    }

    /** @brief Increment the outbound SOL sequence number.
     *
     *  @return outbound sequence number to populate the SOL payload.
     */
    auto incOutboundSeqNum()
    {
        if ((++out) == MAX_SOL_SEQUENCE_NUMBER)
        {
            out = 1;
        }

        return out;
    }

  private:
    uint8_t in = 1;  //!< Inbound sequence number.
    uint8_t out = 0; //!< Outbound sequence number, since the first
                     //!< operation is increment, it is initialised to 0
};

} // namespace internal

/** @class Context
 *
 *  Context keeps the state of the SOL session. The information needed to
 *  maintain the state of the SOL is part of this class. This class provides
 *  interfaces to handle incoming SOL payload, send response and send outbound
 *  SOL payload.
 */
class Context : public std::enable_shared_from_this<Context>
{
  public:
    Context() = delete;
    ~Context() = default;
    Context(const Context&) = delete;
    Context& operator=(const Context&) = delete;
    Context(Context&&) = delete;
    Context& operator=(Context&&) = delete;

    /** @brief Context Factory
     *
     *  This is called by the SOL Manager when a SOL payload instance is
     *  started for the activate payload command. Its purpose is to be able
     *  to perform post-creation tasks on the object without changing the
     *  code flow
     *
     *  @param[in] io  - boost::asio io context for event scheduling.
     *  @param[in] maxRetryCount  - Retry count max value.
     *  @param[in] sendThreshold - Character send threshold.
     *  @param[in] instance - SOL payload instance.
     *  @param[in] sessionID - BMC session ID.
     */
    static std::shared_ptr<Context>
        makeContext(std::shared_ptr<boost::asio::io_context> io,
                    uint8_t maxRetryCount, uint8_t sendThreshold,
                    uint8_t instance, session::SessionID sessionID);

    /** @brief Context Constructor.
     *
     *  This should only be used by the Context factory makeContext
     *  or the accumulate timer will not be initialized properly
     *
     *  @param[in] io  - boost::asio io context for event scheduling.
     *  @param[in] maxRetryCount  - Retry count max value.
     *  @param[in] sendThreshold - Character send threshold.
     *  @param[in] instance - SOL payload instance.
     *  @param[in] sessionID - BMC session ID.
     */
    Context(std::shared_ptr<boost::asio::io_context> io, uint8_t maxRetryCount,
            uint8_t sendThreshold, uint8_t instance,
            session::SessionID sessionID);

    static constexpr auto clear = true;
    static constexpr auto noClear = false;

    /** @brief accumulate timer */
    boost::asio::steady_timer accumulateTimer;

    /** @brief retry timer */
    boost::asio::steady_timer retryTimer;

    /** @brief Retry count max value. */
    const uint8_t maxRetryCount = 0;

    /** @brief Retry counter. */
    uint8_t retryCounter = 0;

    /** @brief Character send threshold. */
    const uint8_t sendThreshold = 0;

    /** @brief SOL payload instance. */
    const uint8_t payloadInstance = 0;

    /** @brief Session ID. */
    const session::SessionID sessionID = 0;

    /** @brief session pointer
     */
    std::shared_ptr<session::Session> session;

    /** @brief enable/disable accumulate timer
     *
     *  The timeout interval is managed by the SOL Manager;
     *  this function only enables or disable the timer
     *
     *  @param[in] enable - enable(true) or disable(false) accumulation timer
     */
    void enableAccumulateTimer(bool enable);

    /** @brief enable/disable retry timer
     *
     *  The timeout interval is managed by the SOL Manager;
     *  this function only enables or disable the timer
     *
     *  @param[in] enable - enable(true) or disable(false) retry timer
     */
    void enableRetryTimer(bool enable);

    /** @brief Process the Inbound SOL payload.
     *
     *  The SOL payload from the remote console is processed and the
     *  acknowledgment handling is done.
     *
     *  @param[in] seqNum - Packet sequence number.
     *  @param[in] ackSeqNum - Packet ACK/NACK sequence number.
     *  @param[in] count - Accepted character count.
     *  @param[in] operation - ACK is false, NACK is true
     *  @param[in] input - Incoming SOL character data.
     */
    void processInboundPayload(uint8_t seqNum, uint8_t ackSeqNum, uint8_t count,
                               bool status, const std::vector<uint8_t>& input);

    /** @brief Send the outbound SOL payload.
     *
     *  @return zero on success and negative value if condition for sending
     *          the payload fails.
     */
    int sendOutboundPayload();

    /** @brief Resend the SOL payload.
     *
     *  @param[in] clear - if true then send the payload and clear the
     *                     cached payload, if false only send the payload.
     */
    void resendPayload(bool clear);

    /** @brief accumlate timer handler called by timer */
    void charAccTimerHandler();

    /** @brief retry timer handler called by timer */
    void retryTimerHandler();

  private:
    /** @brief Expected character count.
     *
     *  Expected Sequence number and expected character count is set before
     *  sending the SOL payload. The check is done against these values when
     *  an incoming SOL payload is received.
     */
    size_t expectedCharCount = 0;

    /** @brief Inbound and Outbound sequence numbers. */
    internal::SequenceNumbers seqNums;

    /** @brief Copy of the last sent SOL payload.
     *
     *  A copy of the SOL payload is kept here, so that when a retry needs
     *  to be attempted the payload is sent again.
     */
    std::vector<uint8_t> payloadCache;

    /**
     * @brief Send Response for Incoming SOL payload.
     *
     * @param[in] ackSeqNum - Packet ACK/NACK Sequence Number.
     * @param[in] count - Accepted Character Count.
     * @param[in] ack - Set ACK/NACK in the Operation.
     */
    void prepareResponse(uint8_t ackSeqNum, uint8_t count, bool ack);

    /** @brief Send the outgoing SOL payload.
     *
     *  @param[in] out - buffer containing the SOL payload.
     */
    void sendPayload(const std::vector<uint8_t>& out) const;
};

} // namespace sol