#pragma once

#include "SSR/Msg.h"

#include <chrono>


struct Player_Clock {
  using clock = std::chrono::system_clock;
  SSR::Play_Back_Time_Point start_time;  // 抽象时间起点
  std::chrono::time_point<std::chrono::system_clock> true_start_time; // 真正开始回放的墙钟起点
  double playback_speed_rate = 20.0;
  Player_Clock() {
    true_start_time = clock::now();
  }
  SSR::Play_Back_Time_Point get_cur_time_point() const noexcept {

    const auto now = clock::now();

    // 防御：系统时间回拨或还没初始化导致 now < true_start_time
    const double real_elapsed_sec =
        std::max(0.0, std::chrono::duration<double>(now - true_start_time).count());

    const double sim_elapsed_sec = real_elapsed_sec * playback_speed_rate;

    // Time_Point 只有秒精度：向下取整
    const auto add = static_cast<std::uint64_t>(sim_elapsed_sec);

    SSR::Play_Back_Time_Point tp = start_time;
    tp.add_sec(add);
    return tp;
  }
};
