/**
 * server/src/hardware/interface/cbus/cbusinterface.hpp
 *
 * This file is part of the traintastic source code.
 *
 * Copyright (C) 2019-2025 Reinder Feenstra
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef TRAINTASTIC_SERVER_HARDWARE_INTERFACE_CBUSINTERFACE_HPP
#define TRAINTASTIC_SERVER_HARDWARE_INTERFACE_CBUSINTERFACE_HPP

#include "interface.hpp"
#include "../input/inputcontroller.hpp"
#include "../output/outputcontroller.hpp"
#include "../../core/serialdeviceproperty.hpp"
#include "../../core/objectproperty.hpp"
#include "../../enum/cbusinterfacetype.hpp"
#include "../../enum/serialflowcontrol.hpp"
#include <span>

namespace CBUS {
class Kernel;
class Settings;
}

/**
 * @brief CBUS hardware interface
 */
class CBUSInterface final
  : public Interface
  , public InputController
  , public OutputController
{
  CLASS_ID("interface.cbus")
  DEFAULT_ID("cbus")
  CREATE_DEF(CBUSInterface)

  private:
    std::unique_ptr<CBUS::Kernel> m_kernel;
    boost::signals2::connection m_cbusPropertyChanged;

    void addToWorld() final;
    void loaded() final;
    void destroying() final;
    void updateVisible();

  public:
    Property<CBUSInterfaceType> type;
    SerialDeviceProperty device;
    Property<uint32_t> baudrate;
    Property<SerialFlowControl> flowControl;
    Property<std::string> hostname;
    Property<uint16_t> port;
    ObjectProperty<CBUS::Settings> cbus;

    CBUSInterface(World& world, std::string_view _id);
    ~CBUSInterface() final;

    bool setOnline(bool& value, bool simulation) final;

    std::span<const InputChannel> inputChannels() const final;
    std::pair<uint32_t, uint32_t> inputAddressMinMax(InputChannel) const final;

    std::span<const OutputChannel> outputChannels() const final;
    std::pair<uint32_t, uint32_t> outputAddressMinMax(OutputChannel) const final;
    [[nodiscard]] bool setOutputValue(OutputChannel channel, uint32_t address, OutputValue value) final;

};

#endif
