import 'package:flutter/material.dart';
import 'package:vitalapp/consts/styles.dart';
import '../dynamic_drawer.dart';

import 'group.dart';

class VDrawerCheckBoxCellGroup<T, TValue> extends StatefulWidget {
  final GlobalKey<ScaffoldState> scaffoldKey;
  final String? title;
  final List<CheckBoxCellGroupItem<T>> source;

  /// 选项文本提取函数
  final String Function(T data) labelGetter;

  /// 选项值提取函数
  final TValue Function(T data) valueGetter;

  final VCheckBoxCellGroupCheckChanged<T, TValue>? onChanged;

  final ValueChanged<List<TValue>>? onConfirm;

  const VDrawerCheckBoxCellGroup({
    super.key,
    this.title,
    required this.source,
    required this.labelGetter,
    required this.valueGetter,
    this.onChanged,
    required this.scaffoldKey,
    this.onConfirm,
  });

  @override
  State<StatefulWidget> createState() => _DrawerGroupState<T, TValue>();
}

class _DrawerGroupState<T, TValue>
    extends State<VDrawerCheckBoxCellGroup<T, TValue>> {
  final ScrollController _scrollController = ScrollController();
  @override
  Widget build(BuildContext context) {
    List<TValue> values = widget.source
        .where((e) => e.isChecked)
        .map((e) => widget.valueGetter.call(e.data))
        .toList();
    return Drawer(
      shadowColor: Colors.black.withOpacity(.1),
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: GlobalStyles.borderRadius,
      ),
      elevation: 0,
      width: 520,
      child: Container(
        alignment: Alignment.topLeft,
        padding: const EdgeInsets.symmetric(vertical: 12),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 4),
              child: Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  widget.title != null
                      ? Text(
                          widget.title!,
                          style: const TextStyle(
                            color: Colors.black,
                            fontSize: 24,
                          ),
                        )
                      : const SizedBox(width: 1),
                  IconButton(
                    onPressed: () {
                      VDynamicDrawerWrapper.hide(
                          scaffoldKey: widget.scaffoldKey);
                    },
                    icon: Icon(
                      Icons.close,
                      color: Colors.grey.shade600,
                      size: 36,
                    ),
                    // style: ButtonStyle(
                    //   shape: MaterialStatePropertyAll(
                    //     RoundedRectangleBorder(
                    //       side: BorderSide(color: Colors.grey.shade600),
                    //       borderRadius: BorderRadius.circular(48),
                    //     ),
                    //   ),
                    // ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Scrollbar(
                thumbVisibility: true,
                controller: _scrollController,
                child: VCheckBoxCellGroup<T, TValue>(
                  labelGetter: widget.labelGetter,
                  controller: _scrollController,
                  valueGetter: widget.valueGetter,
                  source: widget.source,
                  onChanged: (value, isChecked, data, checkedValues) {
                    values = checkedValues;
                    widget.onChanged
                        ?.call(value, isChecked, data, checkedValues);
                  },
                  isScrollable: true,
                ),
              ),
            ),
            Container(
              padding:
                  const EdgeInsets.symmetric(horizontal: 18).copyWith(top: 8),
              alignment: Alignment.center,
              child: TextButton(
                onPressed: () {
                  widget.onConfirm?.call(values);
                  VDynamicDrawerWrapper.hide(scaffoldKey: widget.scaffoldKey);
                },
                child: Container(
                  width: double.infinity,
                  height: 38,
                  alignment: Alignment.center,
                  child: const Text(
                    "确定",
                    style: TextStyle(fontSize: 20),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
