import 'package:flutter/material.dart';

import '../data/sort_options.dart';

class FilesSortActions extends StatelessWidget {
  final SortOption currentSort;
  final bool ascending;
  final ValueChanged<bool> onDirectionChanged;
  final ValueChanged<SortOption> onSortChanged;

  const FilesSortActions({
    required this.currentSort,
    required this.ascending,
    required this.onDirectionChanged,
    required this.onSortChanged,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        PopupMenuButton<bool>(
          tooltip:
              'Sortierrichtung: ${ascending ? 'aufsteigend' : 'absteigend'}',
          icon: Icon(
            ascending ? Icons.text_rotate_up : Icons.text_rotation_down,
          ),
          itemBuilder: (context) => [true, false]
              .map(
                (e) => PopupMenuItem<bool>(
                  value: e,
                  enabled: e != ascending,
                  child: Row(
                    children: [
                      Icon(
                        e ? Icons.text_rotate_up : Icons.text_rotation_down,
                        color: theme.colorScheme.onSurface,
                      ),
                      const SizedBox(width: 15),
                      Text(e ? 'Aufsteigend' : 'Absteigend'),
                    ],
                  ),
                ),
              )
              .toList(),
          onSelected: onDirectionChanged,
        ),
        PopupMenuButton<SortOption>(
          tooltip:
              'Sortieren nach: ${SortOptions.getOption(currentSort).displayName}',
          icon: const Icon(Icons.sort),
          itemBuilder: (context) => SortOptions.options.keys
              .map(
                (key) => PopupMenuItem<SortOption>(
                  value: key,
                  enabled: key != currentSort,
                  child: Row(
                    children: [
                      Icon(
                        SortOptions.getOption(key).icon,
                        color: theme.colorScheme.onSurface,
                      ),
                      const SizedBox(width: 15),
                      Text(SortOptions.getOption(key).displayName),
                    ],
                  ),
                ),
              )
              .toList(),
          onSelected: onSortChanged,
        ),
      ],
    );
  }
}
