#pragma once

#include <cstdint>
#include <map>
#include <string>

namespace aptt {

struct AudioThreadMmcssConfig {
  bool enabled = true;
  std::string task_name = "Pro Audio";
  std::int32_t priority = 0;
};

struct AudioThreadMmcssRegistrationSnapshot {
  bool enabled = false;
  bool attempted = false;
  bool active = false;
  bool priority_applied = false;
  bool revert_attempted = false;
  bool revert_succeeded = false;
  std::string task_name;
  std::int32_t priority = 0;
  std::uint32_t task_index = 0;
  std::uint32_t thread_id = 0;
  std::uint32_t error_code = 0;
  std::string error_stage;
};

std::string AudioThreadMmcssPriorityLabel(std::int32_t priority);
void AddAudioThreadMmcssBackendFields(
    std::map<std::string, std::string>& fields,
    const AudioThreadMmcssRegistrationSnapshot& snapshot);

class ScopedAudioThreadMmcssRegistration {
 public:
  explicit ScopedAudioThreadMmcssRegistration(AudioThreadMmcssConfig config);
  ScopedAudioThreadMmcssRegistration(
      const ScopedAudioThreadMmcssRegistration&) = delete;
  ScopedAudioThreadMmcssRegistration& operator=(
      const ScopedAudioThreadMmcssRegistration&) = delete;
  ~ScopedAudioThreadMmcssRegistration();

  AudioThreadMmcssRegistrationSnapshot snapshot() const;
  AudioThreadMmcssRegistrationSnapshot Revert();

 private:
  AudioThreadMmcssRegistrationSnapshot snapshot_;
  void* handle_ = nullptr;
};

}  // namespace aptt
