#pragma once
#include <myapp/modbus/connection.h>
#include <modbus/modbus.h>
#include "context.h"

namespace myapp::modbus {

class connection_impl final 
	: public connection
	, public data_access
	, public std::enable_shared_from_this<connection_impl>
{
public:
    explicit connection_impl(std::shared_ptr<logging::logger>, context);

public:
	void flush();
    void connect() override;
    void close() override;

    std::shared_ptr<poll> get_poller() noexcept override;
    std::shared_ptr<data_access> get_data_access() noexcept override;

public:
    std::vector<uint8_t> read_coils(int slave_id, int starting_address, int quantity) override;
    std::vector<uint8_t> read_discrete_inputs(int slave_id, int starting_address, int quantity) override;
    std::vector<uint16_t> read_holding_registers(int slave_id, int starting_address, int quantity) override;
    std::vector<uint16_t> read_input_registers(int slave_id, int starting_address, int quantity) override;
    void write_single_coil(int slave_id, int address, uint8_t value) override;
    void write_single_register(int slave_id, int address, uint16_t value) override;
    void write_multiple_coils(int slave_id, int starting_address, std::vector<uint8_t> values) override;
    void write_multiple_registers(int slave_id, int starting_address, std::vector<uint16_t> values) override;
    std::vector<uint16_t> read_write_multiple_registers(int slave_id,
        int read_starting_address, int quantity_to_read,
        int write_starting_address, std::vector<uint16_t> values_to_write) override;

private:
    void set_slave(int addr);

private:
    context context_;
    std::shared_ptr<logging::logger> logger_;
    std::shared_ptr<poll> poll_;
};

} /* namespace myapp::modbus */
