import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
import 'package:sk_base_mobile/util/snack_bar.util.dart';
import 'package:sk_base_mobile/widgets/loading_indicator.dart';

class SkMutiPickerDialog<T> extends StatelessWidget {
  final SkMutiPickerDialogController controller;
  Function(List<PickerItem> data)? onSelected;
  SkMutiPickerDialog(
      {super.key,
      required Future<List<PickerItem<T>>> Function() getData,
      this.onSelected})
      : controller = Get.put(SkMutiPickerDialogController<T>(getData: getData));

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenAdaper.height(400),
      decoration: const BoxDecoration(color: AppTheme.nearlyWhite),
      child: Column(children: [
        Container(
          padding: EdgeInsets.symmetric(
              vertical: ScreenAdaper.height(20),
              horizontal: ScreenAdaper.width(20)),
          child: Row(
            children: [
              Text(
                '取消',
                style: TextStyle(fontSize: ScreenAdaper.height(30)),
              ),
              const Spacer(),
              GestureDetector(
                onTap: () {
                  if (onSelected != null) {
                    final selectedData = controller.pickData
                        .where((element) => element.checked)
                        .toList();
                    if (selectedData.isEmpty) {
                      SnackBarUtil().warning('请最少选择一个“员工”角色作为基础角色');
                      return;
                    }
                    onSelected!(selectedData);
                  }
                },
                child: Text(
                  '确定',
                  style: TextStyle(
                      fontSize: ScreenAdaper.height(30),
                      color: AppTheme.primaryColor),
                ),
              )
            ],
          ),
        ),
        Expanded(
            child: Obx(
          () => controller.loading.value
              ? const LoadingIndicator(
                  common: true,
                )
              : SingleChildScrollView(
                  child: ListBody(
                  children: controller.pickData.mapIndexed((
                    int index,
                    PickerItem item,
                  ) {
                    return CheckboxListTile(
                      fillColor: MaterialStateProperty.resolveWith<Color?>(
                          (Set<MaterialState> states) {
                        if (states.contains(MaterialState.selected)) {
                          return AppTheme.primaryColor;
                        }
                        return null;
                      }),
                      dense: true,
                      visualDensity: VisualDensity.compact,
                      contentPadding: EdgeInsets.symmetric(
                          vertical: ScreenAdaper.height(0),
                          horizontal: ScreenAdaper.width(20)),
                      title: Text(item.label,
                          style: TextStyle(fontSize: ScreenAdaper.height(25))),
                      subtitle: item.subLabel == null
                          ? null
                          : Text(
                              item.subLabel!,
                              style: TextStyle(
                                  fontSize: ScreenAdaper.height(20),
                                  color: AppTheme.grey),
                            ),
                      value: controller.pickData[index].checked,
                      onChanged: (bool? value) {
                        PickerItem item = controller.pickData[index];
                        item.checked = value ?? false;
                        controller.pickData[index] = item;
                      },
                    );
                  }).toList(),
                )),
        ))
      ]),
    );
  }
}

class SkMutiPickerDialogController<T> extends GetxController {
  RxList<PickerItem<T>> pickData = RxList([]);
  Future<List<PickerItem<T>>> Function() getData;
  SkMutiPickerDialogController({required this.getData});
  RxBool loading = false.obs;
  @override
  void onReady() {
    init();
    super.onReady();
  }

  Future<void> init() async {
    loading.value = true;
    pickData.assignAll(await getData());
    loading.value = false;
  }
}

class PickerItem<T> {
  final String label;
  final String? subLabel;
  final T value;
  bool checked;
  PickerItem(
      {required this.label,
      required this.value,
      required this.checked,
      this.subLabel});
}
