/***************************************************************************
 *   Copyright (C) 2023 by David Register                                  *
 *   Copyright (C) 2023 Alec Leamas                                        *
 *                                                                         *
 *   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, see <https://www.gnu.org/licenses/>. *
 **************************************************************************/

/**
 * \file
 *
 * Android nmea0183 internal bluetooth driver.
 */

#ifndef _COMMDRIVERN0183ANDROIDBT_H
#define _COMMDRIVERN0183ANDROIDBT_H

#include <atomic>
#include <string>

#include <wx/event.h>

#include "model/comm_drv_n0183.h"
#include "model/comm_drv_stats.h"
#include "model/conn_params.h"

class CommDriverN0183AndroidBTEvent : public wxEvent {
public:
  CommDriverN0183AndroidBTEvent(wxEventType commandType, int id);
  ~CommDriverN0183AndroidBTEvent();

  // accessors
  void SetPayload(std::shared_ptr<std::vector<unsigned char>> data);
  std::shared_ptr<std::vector<unsigned char>> GetPayload();

  // required for sending with wxPostEvent()
  wxEvent* Clone() const;

private:
  std::shared_ptr<std::vector<unsigned char>> m_payload;
};

wxDECLARE_EVENT(wxEVT_COMMDRIVER_N0183_ANDROID_BT,
                CommDriverN0183AndroidBTEvent);

class CommDriverN0183AndroidBT : public CommDriverN0183,
                                 public wxEvtHandler,
                                 public DriverStatsProvider {
public:
  CommDriverN0183AndroidBT(const ConnectionParams* params, DriverListener& l);

  virtual ~CommDriverN0183AndroidBT();

  bool Open();
  void Close();

  const ConnectionParams& GetParams() const override { return m_params; }

  bool SendMessage(std::shared_ptr<const NavMsg> msg,
                   std::shared_ptr<const NavAddr> addr) override;

  DriverStats GetDriverStats() const override { return m_driver_stats; }

private:
  bool m_bok;
  std::string m_portstring;
  std::string m_BaudRate;
  int m_handshake;

  ConnectionParams m_params;
  DriverListener& m_listener;
  void handle_N0183_MSG(CommDriverN0183AndroidBTEvent& event);
  StatsTimer m_stats_timer;
  DriverStats m_driver_stats;
};

#endif  // guard
