import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/_custom_popup_menu.dart';

enum KwPopupMenuFit { expand, loose }

class KwPopupMenu extends StatefulWidget {
  final Widget Function(BuildContext, bool menuIsShowmn)? customButtonBuilder;
  final List<KwPopupMenuItem> menuItems;
  final double? horizontalMargin;
  final KwPopupMenuFit fit;
  final double horizontalPadding;
  final CustomPopupMenuController? controller;


  const KwPopupMenu({
    this.customButtonBuilder,
    required this.menuItems,
    this.fit = KwPopupMenuFit.loose,
    this.horizontalMargin,
    this.horizontalPadding = 0,
    this.controller,
    super.key,
  });

  @override
  State<KwPopupMenu> createState() => _KwPopupMenuState();
}

class _KwPopupMenuState extends State<KwPopupMenu> {
  late CustomPopupMenuController _controller;

  @override
  void initState() {
    _controller = widget.controller ?? CustomPopupMenuController();
    super.initState();
    _controller.addListener(() {
      if (mounted) setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return CustomPopupMenu(
      horizontalMargin: widget.horizontalMargin ?? 0,
      controller: _controller,
      verticalMargin: 4,
      position: PreferredPosition.bottom,
      showArrow: false,
      enablePassEvent: false,
      barrierColor: Colors.transparent,
      menuBuilder: widget.menuItems.isEmpty
          ? null
          : () {
              return Row(
                mainAxisSize: widget.fit == KwPopupMenuFit.expand
                    ? MainAxisSize.max
                    : MainAxisSize.min,
                children: [
                  widget.fit == KwPopupMenuFit.expand
                      ? Expanded(
                          child: _buildItem(),
                        )
                      : _buildItem()
                ],
              );
            },
      pressType: PressType.singleClick,
      child: widget.customButtonBuilder
              ?.call(context, _controller.menuIsShowing) ??
          AnimatedContainer(
            duration: const Duration(milliseconds: 200),
            height: 32,
            width: 32,
            decoration: BoxDecoration(
                color: _controller.menuIsShowing
                    ? AppColors.grayTintStroke
                    : AppColors.grayWhite,
                shape: BoxShape.circle,
                border: _controller.menuIsShowing
                    ? null
                    : Border.all(color: AppColors.grayTintStroke, width: 1)),
            child: Center(child: Assets.images.icons.more.svg()),
          ),
    );
  }

  Container _buildItem() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: widget.horizontalPadding),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        border: Border.all(color: AppColors.grayTintStroke, width: 1),
        color: AppColors.grayWhite,
      ),
      child: Container(
        constraints: const BoxConstraints(
          maxHeight: 210,
        ),
        child: SingleChildScrollView(
          child: IntrinsicWidth(
            child: Column(
              children: [
                for (var i = 0; i < widget.menuItems.length; i++)
                  Material(
                    type: MaterialType.transparency,
                    child: InkWell(
                      onTap: () {
                        widget.menuItems[i].onTap();
                        _controller.hideMenu();
                      },
                      child: Container(
                        height: 42,
                        padding: const EdgeInsets.symmetric(horizontal: 16),
                        decoration: i == 0
                            ? null
                            : const BoxDecoration(
                                border: Border(
                                  top: BorderSide(
                                    color: AppColors.grayTintStroke,
                                    width: 1,
                                  ),
                                ),
                              ),
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            if (widget.menuItems[i].icon != null) ...[
                              widget.menuItems[i].icon!,
                              const Gap(4),
                            ],
                            Expanded(
                                child: Text(
                              widget.menuItems[i].title,
                              style: widget.menuItems[i].textStyle ??
                                  AppTextStyles.bodyMediumReg,
                            )),
                          ],
                        ),
                      ),
                    ),
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class KwPopupMenuItem {
  final String title;
  final Widget? icon;

  final VoidCallback onTap;

  final TextStyle? textStyle;

  const KwPopupMenuItem({
    required this.title,
    required this.onTap,
    this.icon,
    this.textStyle,
  });
}
