import 'package:flutter/material.dart';

final _builderMap = <GlobalKey<ScaffoldState>, WidgetBuilder>{};

class VDrawer extends StatelessWidget {
  final Widget? child;
  final double? width;
  final String? title;
  final GlobalKey<ScaffoldState>? scaffoldKey;
  final VoidCallback? onConfirm;

  const VDrawer({
    super.key,
    this.child,
    this.width,
    this.title,
    this.scaffoldKey,
    this.onConfirm,
  });

  @override
  Widget build(BuildContext context) {
    return Drawer(
      shadowColor: Colors.black.withOpacity(.1),
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16),
      ),
      elevation: 0,
      width: width,
      child: Container(
        alignment: Alignment.topLeft,
        padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: _buildChildren(context),
        ),
      ),
    );
  }

  List<Widget> _buildChildren(BuildContext context) {
    final children = <Widget>[];
    final header = _buildHeader();
    if (header != null) {
      children.add(header);
    }
    if (child != null) {
      children.add(Expanded(child: child!));
    }
    return children;
  }

  Widget? _buildHeader() {
    final children = <Widget>[];
    const btnTextStyle = TextStyle(fontSize: 20);
    if (scaffoldKey != null) {
      children.add(
        TextButton(
          onPressed: () {
            scaffoldKey!.currentState?.closeEndDrawer();
          },
          child: const Text("取消", style: btnTextStyle),
        ),
      );
    }
    if (title != null) {
      children.add(Text(
        title!,
        style: TextStyle(color: Colors.grey.shade700, fontSize: 20),
      ));
    }
    if (onConfirm != null) {
      children.add(
        TextButton(
          onPressed: () {
            onConfirm?.call();
          },
          child: const Text("确定", style: btnTextStyle),
        ),
      );
    }

    if (children.isEmpty) return null;

    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: children,
    );
  }
}

class VDynamicDrawerWrapper extends StatefulWidget {
  final GlobalKey<ScaffoldState> scaffoldKey;

  const VDynamicDrawerWrapper({super.key, required this.scaffoldKey});

  @override
  State<StatefulWidget> createState() => _VDynamicDrawerWrapperState();

  static void show({
    required GlobalKey<ScaffoldState> scaffoldKey,
    required WidgetBuilder builder,
  }) {
    _builderMap[scaffoldKey] = builder;
    scaffoldKey.currentState?.openEndDrawer();
  }

  static void hide({required GlobalKey<ScaffoldState> scaffoldKey}) {
    scaffoldKey.currentState?.closeEndDrawer();
  }
}

class _VDynamicDrawerWrapperState extends State<VDynamicDrawerWrapper> {
  @override
  Widget build(BuildContext context) {
    final builder = _builderMap[widget.scaffoldKey];
    if (builder != null) {
      return builder(context);
    }
    return const SizedBox();
  }
}
