import 'dart:ui';

extension HexColor on String {
  Color get color => toColor();

  Color toColor() {
    String hex = replaceAll('#', '');
    if (hex.length == 6) {
      hex = 'FF$hex';
    }
    return Color(int.parse(hex, radix: 16));
  }
}

extension BoolExtension on bool? {
  bool get isTrue => this == true;

  bool get isFalse => this == false;
}

extension StringExtension on String? {
  String get orEmpty => this ?? '';
}

extension DurationExtension on double? {
  String toFormattedDuration() {
    if (this == null) return '';
    int totalSeconds = (this!).round();
    if (totalSeconds < 60) {
      return '${totalSeconds}s';
    } else if (totalSeconds < 3600) {
      int minutes = totalSeconds ~/ 60;
      int seconds = totalSeconds % 60;
      return '${minutes}m${seconds}s';
    } else {
      int hours = totalSeconds ~/ 3600;
      int minutes = (totalSeconds % 3600) ~/ 60;
      int seconds = totalSeconds % 60;
      return '${hours}h${minutes}m${seconds}s';
    }
  }
}

extension StringExtensions on String {
  String replacePlaceholders(List<dynamic> replacements) {
    var result = this;
    for (var replacement in replacements) {
      if (replacement is String) {
        result = result.replaceFirst('%s', replacement);
      } else if (replacement is int) {
        result = result.replaceFirst('%d', replacement.toString());
      }
    }
    return result;
  }
}
