#pragma once

#include <cstdint>
#include <memory>
#include <vector>

#include "core/audio_backend.h"
#include "core/diagnostic_log.h"

namespace aptt {

struct DirectSoundPcmPolicy {
  bool requires_explicit_device = true;
  bool keep_warm_after_signal = true;
  bool release_after_drain = false;
  bool uses_reset_on_normal_stop = false;
  bool changes_cooperative_level_during_session = false;
  bool restores_lost_buffers = true;
  std::uint32_t secondary_buffer_duration_ms = 250;
  std::uint32_t write_chunk_ms = 20;
  std::uint32_t cursor_poll_interval_ms = 5;
  std::uint32_t release_cooldown_ms = 1000;
};

DirectSoundPcmPolicy MakeDirectSoundPcmPolicy(const PlaybackConfig& config);
bool IsDirectSoundPcmPlaybackFormat(const FormatDescriptor& format);

class DirectSoundPcmBackend final : public IAudioBackend {
 public:
  DirectSoundPcmBackend();
  ~DirectSoundPcmBackend() override;

  BackendApi api() const override;
  DeviceList enumerateDevices() override;
  FormatProbeResult probeFormats(const DeviceInfo& device) override;
  OperationResult initialize(const PlaybackConfig& config) override;
  OperationResult start() override;
  OperationResult stop() override;
  OperationResult reset() override;
  RealtimeStats getRealtimeStats() const override;
  BackendError getLastError() const override;
  OperationResult shutdown() override;

  DeviceLifecycleState lifecycle_state() const;
  std::vector<DiagnosticEvent> diagnostic_events() const;

 private:
  struct Impl;
  std::unique_ptr<Impl> impl_;
};

}  // namespace aptt
