// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter/material.dart';
import 'package:hoshan/core/gen/assets.gen.dart';
import 'package:hoshan/ui/theme/text.dart';

class MorePopupMenuHandler {
  final BuildContext context;

  MorePopupMenuHandler({required this.context});

  static Widget morePopUpItem(
      {final SvgGenImage? icon,
      final Icon? customeIcon,
      required final String title,
      Color? color}) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 10.0),
        child: Row(
          children: [
            if (icon != null) icon.svg(color: color, width: 18, height: 18),
            if (customeIcon != null) customeIcon,
            const SizedBox(
              width: 8,
            ),
            Text(
              title,
              style: AppTextStyles.body4.copyWith(color: color),
            ),
          ],
        ),
      ),
    );
  }

  Widget morePopUpMenu(
      {required final Widget child,
      final Color? color,
      final List<PopUpMenuItemModel> items = const []}) {
    return PopupMenuButton<int>(
        tooltip: '',
        offset: const Offset(0, 38),
        onSelected: (value) async {
          items[value].click?.call();
        },
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        itemBuilder: (BuildContext context) {
          return <PopupMenuEntry<int>>[
            ...List.generate(
                items.length, (index) => items[index].popupMenuItem)
          ];
        },
        child: child);
  }

  void showMorePopupMenu(
      {required final GlobalKey containerKey,
      final Color? color,
      final bool right = false,
      final List<PopUpMenuItemModel> items = const []}) async {
    final RenderBox overlay =
        Overlay.of(context).context.findRenderObject() as RenderBox;
    final RenderBox containerRenderBox =
        containerKey.currentContext!.findRenderObject() as RenderBox;

    final Offset containerPosition =
        containerRenderBox.localToGlobal(Offset.zero);
    final Size containerSize = containerRenderBox.size;

    await showMenu<int>(
      context: context,
      shape: ContinuousRectangleBorder(borderRadius: BorderRadius.circular(24)),
      color: color,
      position: RelativeRect.fromLTRB(
          containerPosition.dx - (right ? 32 : 0),
          (containerPosition.dy + containerSize.height + 8),
          (overlay.size.width - containerPosition.dx) - (right ? 0 : 120),
          overlay.size.height - containerPosition.dy - containerSize.height),
      items: [
        ...items.map(
          (e) => e.popupMenuItem,
        )
      ],
    ).then((value) async {
      if (value != null) {
        items
            .firstWhere(
              (element) => element.popupMenuItem.value == value,
            )
            .click
            ?.call();
      }
    });
  }
}

class PopUpMenuItemModel {
  final PopupMenuItem<int> popupMenuItem;
  final Function()? click;

  PopUpMenuItemModel({required this.popupMenuItem, this.click});
}
