import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:phylum/libphylum/db/db.dart';
import 'package:phylum/libphylum/phylum_account.dart';

import 'menu_option.dart';

typedef MenuOptionGroups = Iterable<Iterable<MenuOption>>;

MenuOptionGroups createOptions(BuildContext context) => [
      [
        MenuOption.details,
      ],
      [
        MenuOption.download,
        if (!kIsWeb && Platform.isAndroid) MenuOption.share,
        MenuOption.openWith,
      ],
      [
        MenuOption.rename,
        MenuOption.move,
        MenuOption.copy,
        MenuOption.delete,
      ],
      [
        MenuOption.bookmarkAdd,
        MenuOption.bookmarkRemove,
        MenuOption.permissions,
        MenuOption.publinks,
      ],
    ];

const trashedResourceOptions = [
  [
    MenuOption.details,
  ],
  [
    MenuOption.restore,
    MenuOption.deleteForever,
  ],
];

final parentFolderOptions = [
  [
    MenuOption.details,
  ],
  [
    MenuOption.newFolder,
    MenuOption.uploadFiles,
    MenuOption.uploadFolders,
  ],
  [
    MenuOption.bookmarkAdd,
    MenuOption.bookmarkRemove,
    MenuOption.permissions,
    MenuOption.publinks,
  ],
];

Future<Iterable<PopupMenuEntry<MenuOption>>> buildPopupMenuItems(
  PhylumAccount account,
  MenuOptionGroups options,
  Iterable<Resource> resources, [
  bool hideDisabled = true,
]) async {
  final availableOptions = await Future.wait(
    options.map(
      (group) async => Future.wait(
        group.map((o) async {
          final enabled = await o.filter(account, resources);
          return hideDisabled && !enabled
              ? null
              : PopupMenuItem(
                  value: o,
                  enabled: enabled,
                  child: ListTile(
                    visualDensity: VisualDensity.adaptivePlatformDensity,
                    leading: Icon(o.icon),
                    title: Text(o.text),
                  ),
                );
        }),
      ).then((items) => items.whereType<PopupMenuEntry<MenuOption>>()),
    ),
  );

  final m = availableOptions.fold(
    <PopupMenuEntry<MenuOption>>[],
    (acc, e) {
      if (e.isEmpty) {
        return acc;
      }
      if (acc.isEmpty) {
        return e.toList(growable: false);
      }
      return [...acc, PopupMenuDivider(), ...e];
    },
  );
  return m;
}
