import 'package:aves/model/entry/entry.dart';

/// Rappresenta un album costruito dalla tabella `folders` (FolderStats)
class AlbumRow {
  final String path;
  final String displayName;
  final int albumType;
  final int priority;
  final int totalSize;
  final int fileCount;
  final bool isRemote;
  final bool isVault;
  final int hybridType;
  final bool hidden;
  final AvesEntry? thumbEntry;

  AlbumRow({
    required this.path,
    required this.displayName,
    required this.albumType,
    required this.priority,
    required this.totalSize,
    required this.fileCount,
    required this.isRemote,
    required this.isVault,
    required this.hybridType,
    required this.hidden,
    required this.thumbEntry,
  });

  AlbumRow copyWith({
    String? path,
    String? displayName,
    int? albumType,
    int? priority,
    int? totalSize,
    int? fileCount,
    bool? isRemote,
    bool? isVault,
    int? hybridType,
    bool? hidden,
    AvesEntry? thumbEntry,
  }) {
    return AlbumRow(
      path: path ?? this.path,
      displayName: displayName ?? this.displayName,
      albumType: albumType ?? this.albumType,
      priority: priority ?? this.priority,
      totalSize: totalSize ?? this.totalSize,
      fileCount: fileCount ?? this.fileCount,
      isRemote: isRemote ?? this.isRemote,
      isVault: isVault ?? this.isVault,
      hybridType: hybridType ?? this.hybridType,
      hidden: hidden ?? this.hidden,
      thumbEntry: thumbEntry ?? this.thumbEntry,
    );
  }
}

/// Tipi di ordinamento disponibili
enum AlbumSortType {
  priorityThenName,
  nameAsc,
  nameDesc,
  sizeAsc,
  sizeDesc,
  countAsc,
  countDesc,
}

/// Ordinamento principale degli album
List<AlbumRow> sortAlbums(List<AlbumRow> albums, AlbumSortType sortType) {
  final list = [...albums];

  int byNameAsc(AlbumRow a, AlbumRow b) =>
      a.displayName.toLowerCase().compareTo(b.displayName.toLowerCase());

  int byNameDesc(AlbumRow a, AlbumRow b) => -byNameAsc(a, b);

  int bySizeAsc(AlbumRow a, AlbumRow b) => a.totalSize.compareTo(b.totalSize);

  int bySizeDesc(AlbumRow a, AlbumRow b) => -bySizeAsc(a, b);

  int byCountAsc(AlbumRow a, AlbumRow b) => a.fileCount.compareTo(b.fileCount);

  int byCountDesc(AlbumRow a, AlbumRow b) => -byCountAsc(a, b);

  switch (sortType) {
    case AlbumSortType.priorityThenName:
      list.sort((a, b) {
        // ⭐ 1. pinned prima (priority > 0)
        final aPinned = a.priority > 0;
        final bPinned = b.priority > 0;

        if (aPinned && !bPinned) return -1;
        if (!aPinned && bPinned) return 1;

        // ⭐ 2. se entrambi pinned → priority ASC
        if (aPinned && bPinned) {
          return a.priority.compareTo(b.priority);
        }

        // ⭐ 3. se entrambi normali → nome ASC
        return byNameAsc(a, b);
      });
      break;

    case AlbumSortType.nameAsc:
      list.sort(byNameAsc);
      break;

    case AlbumSortType.nameDesc:
      list.sort(byNameDesc);
      break;

    case AlbumSortType.sizeAsc:
      list.sort(bySizeAsc);
      break;

    case AlbumSortType.sizeDesc:
      list.sort(bySizeDesc);
      break;

    case AlbumSortType.countAsc:
      list.sort(byCountAsc);
      break;

    case AlbumSortType.countDesc:
      list.sort(byCountDesc);
      break;
  }

  return list;
}
