import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import '../my_text_view.dart';
import '../utils/dark_theme_util.dart';
import 'package:cs_resources/constants/color_constants.dart';

class OptionPickerUtil {
  static void showCupertinoOptionPicker({
    required List<String> items,
    required Function(List<String>, int) onPickerChanged,
    int? initialSelectIndex = 0,
    String? title,
    String? confirmTextStr,
    String? cancelTextStr,
    Function()? onReset,
  }) {
    SmartDialog.show(
      usePenetrate: false,
      tag: 'data_picker',
      maskColor: Colors.black.withOpacity(0.4),
      alignment: Alignment.bottomCenter,
      builder: (BuildContext context) {
        return Container(
          height: 280,
          decoration: BoxDecoration(
            color: DarkThemeUtil.multiColors(Colors.white, darkColor: ColorConstants.darkBlackItem),
            borderRadius: const BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20)),
          ),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  CupertinoButton(
                    onPressed: onReset ??
                        () {
                          SmartDialog.dismiss(tag: 'data_picker');
                        },
                    child: Text(
                      cancelTextStr ?? 'Cancel'.tr,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                        color: ColorConstants.appBlue,
                      ),
                    ),
                  ),
                  Text(
                    title ?? "",
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.w500,
                      color: DarkThemeUtil.multiColors(Colors.black, darkColor: Colors.white),
                    ),
                  ),
                  CupertinoButton(
                    child: Text(
                      confirmTextStr ?? 'Confirm'.tr,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                        color: ColorConstants.appBlue,
                      ),
                    ),
                    onPressed: () {
                      SmartDialog.dismiss(tag: 'data_picker');
                      // 将选中的值通过回调函数传递出去
                      onPickerChanged(items, initialSelectIndex!);
                    },
                  ),
                ],
              ),
              const Divider(color: const Color(0x4dd9d9d9), height: 0.5),
              Expanded(
                  child: Container(
                alignment: Alignment.center,
                child: CupertinoPicker(
                  scrollController: FixedExtentScrollController(initialItem: initialSelectIndex ?? 0),
                  itemExtent: 45,
                  diameterRatio: 1,
                  selectionOverlay: const CupertinoPickerDefaultSelectionOverlay(
                    capStartEdge: false,
                    capEndEdge: false,
                  ),
                  onSelectedItemChanged: (int value) {
                    initialSelectIndex = value;
                  },
                  children: items
                      .map((item) => Container(
                            alignment: Alignment.center,
                            child: MyTextView(
                              item.toString(),
                              textAlign: TextAlign.center,
                              textColor: DarkThemeUtil.multiColors(
                                const Color(0XFF404A5B),
                                darkColor: Colors.white,
                              ),
                              fontSize: 16,
                              isFontBold: true,
                            ),
                          ))
                      .toList(),
                ),
              )),
            ],
          ),
        );
      },
    );
  }
}
