import 'package:flutter/material.dart';

import '../../../../api/marianumcloud/webdav/queries/list_files/cacheable_file.dart';

enum SortOption { name, date, size }

class BetterSortOption {
  final String displayName;
  final int Function(CacheableFile, CacheableFile) compare;
  final IconData icon;

  BetterSortOption({
    required this.displayName,
    required this.icon,
    required this.compare,
  });
}

class SortOptions {
  static final Map<SortOption, BetterSortOption> options = {
    SortOption.name: BetterSortOption(
      displayName: 'Name',
      icon: Icons.sort_by_alpha_outlined,
      compare: (a, b) => a.name.toLowerCase().compareTo(b.name.toLowerCase()),
    ),
    SortOption.date: BetterSortOption(
      displayName: 'Datum',
      icon: Icons.history_outlined,
      compare: (a, b) {
        final aMs = a.modifiedAt?.millisecondsSinceEpoch ?? 0;
        final bMs = b.modifiedAt?.millisecondsSinceEpoch ?? 0;
        return aMs.compareTo(bMs);
      },
    ),
    SortOption.size: BetterSortOption(
      displayName: 'Größe',
      icon: Icons.sd_card_outlined,
      compare: (a, b) {
        if (a.isDirectory || b.isDirectory) return a.isDirectory ? 1 : 0;
        if (a.size == null) return 0;
        if (b.size == null) return 1;
        return a.size!.compareTo(b.size!);
      },
    ),
  };

  static BetterSortOption getOption(SortOption option) => options[option]!;
}
