import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/alert_dialog.dart';

class VDialogCheck<T, TValue> extends StatefulWidget {
  const VDialogCheck({
    Key? key,
    this.title,
    required this.source,
    required this.initialValue,
    this.labelGetter,
    required this.valueGetter,
    // ignore: non_constant_identifier_names
    this.MutexValue,
  }) : super(key: key);
  final String? title;
  final List<T> source;
  final List<TValue> initialValue;
  // ignore: non_constant_identifier_names
  final TValue? MutexValue;

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

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

  @override
  // ignore: library_private_types_in_public_api
  _VDialogCheckState<T, TValue> createState() =>
      _VDialogCheckState<T, TValue>();

  Future<T?> show<T>() => VAlertDialog.showDialog<T>(this);
}

class _VDialogCheckState<T, TValue> extends State<VDialogCheck<T, TValue>> {
  List<TValue> initialValue = [];
  @override
  void initState() {
    super.initState();
    initialValue = widget.initialValue.toList();
  }

  @override
  Widget build(BuildContext context) {
    return VAlertDialog(
      width: 460,
      title: widget.title,
      content: Scrollbar(
        thumbVisibility: true,
        child: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: _buildOptions(context),
          ),
        ),
      ),
      onConfirm: () {
        Get.back(result: initialValue);
      },
    );
  }

  List<Widget> _buildOptions(BuildContext context) {
    final primaryColor = Theme.of(context).primaryColor;
    final children = <Widget>[];
    for (var i = 0; i < widget.source.length; i++) {
      final data = widget.source[i];
      final val = widget.valueGetter(data);
      final isSelected = initialValue.contains(val);
      Widget label;
      label = Text(
        widget.labelGetter!(data),
        style: TextStyle(
          color: isSelected ? primaryColor : Colors.black,
          fontSize: 20,
        ),
      );
      final widgetItem = Container(
        alignment: Alignment.centerLeft,
        height: 56,
        padding: const EdgeInsets.symmetric(horizontal: 8),
        color: isSelected ? Theme.of(context).secondaryHeaderColor : null,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                const SizedBox(
                  width: 20,
                ),
                label
              ],
            ),
            if (isSelected)
              Row(
                children: [
                  Icon(
                    Icons.check,
                    size: 24,
                    color: primaryColor,
                  ),
                  const SizedBox(
                    width: 20,
                  )
                ],
              )
          ],
        ),
      );
      children.add(
        Material(
          color: Colors.transparent,
          child: Ink(
              child: InkWell(
            onTap: () {
              final val = widget.valueGetter(data);
              if (!(widget.MutexValue != null &&
                      initialValue.contains(widget.MutexValue)) ||
                  val == widget.MutexValue) {
                setState(() {
                  if (val == widget.MutexValue &&
                      !initialValue.contains(widget.MutexValue)) {
                    initialValue.clear();
                  }
                  if (initialValue.contains(val)) {
                    initialValue.remove(val);
                  } else {
                    initialValue.add(val);
                  }
                });
              }
            },
            child: widgetItem,
          )),
        ),
      );
    }
    return children;
  }
}
