import '../../core/contracts/library.dart';


const int kTicksPerSecond = 10000000;
const int kTicksPerMinute = 600000000;

int ticksFromDuration(Duration duration) => duration.inMicroseconds * 10;

Duration durationFromTicks(int ticks) => Duration(microseconds: ticks ~/ 10);


double playbackProgress(EmbyRawItem item) {
  final position = item.UserData?.PlaybackPositionTicks;
  final total = item.RunTimeTicks;
  if (position == null || total == null || total <= 0) return 0;
  final value = position / total;
  if (value.isNaN || value.isInfinite) return 0;
  return value.clamp(0.0, 1.0);
}


String? formatRemainingLabel(EmbyRawItem item) {
  final position = item.UserData?.PlaybackPositionTicks;
  final total = item.RunTimeTicks;
  if (position == null || total == null || total <= 0) return null;
  final minutes = ((total - position) / kTicksPerMinute).ceil();
  return minutes > 0 ? '剩余 $minutes 分钟' : null;
}
