import 'package:cs_resources/generated/assets.dart';
import 'package:domain/entity/response/index_option_entity.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:cs_resources/constants/color_constants.dart';

import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/no_shadow_scroll_behavior.dart';
import 'package:widgets/widget_export.dart';

/*
 * 切换项目的弹窗
 */
class SwitchProjectDialog extends StatefulWidget {
  final List<IndexOptionEntity> options;
  final void Function(IndexOptionEntity) confirmAction;
  final VoidCallback? cancelAction;

  SwitchProjectDialog({
    required this.options,
    required this.confirmAction,
    this.cancelAction,
  });

  @override
  _SwitchProjectDialogState createState() => _SwitchProjectDialogState();
}

class _SwitchProjectDialogState extends State<SwitchProjectDialog> {
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          width: double.infinity,
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(15)),
          ),
          child: Column(
            children: [
              MyTextView(
                "Switch Projects".tr,
                fontSize: 19,
                isFontMedium: true,
                textColor: ColorConstants.black,
                marginTop: 15,
                marginBottom: 15,
              ),
              Scrollbar(
                child: ScrollConfiguration(
                  behavior: NoShadowScrollBehavior(),
                  child: SingleChildScrollView(
                    child: Column(
                      children: widget.options.map<Widget>((IndexOptionEntity option) {
                        return InkWell(
                          onTap: () {
                            // 清除其他选项的 checked 状态
                            for (var opt in widget.options) {
                              opt.checked = null; // 清除所有选项的 checked 状态
                            }
                            option.checked = 'checked'; // 选中当前选项
                            setState(() {}); // 刷新页面
                          },
                          child: Row(
                            mainAxisSize: MainAxisSize.max,
                            children: [
                              MyTextView(
                                option.txt ?? "",
                                fontSize: 15,
                                isFontRegular: true,
                                textColor: ColorConstants.black33,
                              ).expanded(),
                              option.checked == "checked"
                                  ? const MyAssetImage(Assets.cptAuthCheckedIcon, width: 20.5, height: 20.5)
                                  : const MyAssetImage(Assets.cptAuthUncheckedIcon, width: 20.5, height: 20.5)
                            ],
                          ).paddingOnly(top: 12, bottom: 12, left: 20, right: 20),
                        );
                      }).toList(),
                    ),
                  ),
                ),
              ).constrained(maxHeight: 210),
              Container(
                margin: const EdgeInsets.only(top: 25),
                color: const Color(0XFFCECECE),
                height: 0.5,
              ),
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () {
                        onCancel();
                        widget.cancelAction?.call();
                      },
                      child: MyTextView(
                        "Cancel".tr,
                        fontSize: 17.5,
                        isFontMedium: true,
                        textAlign: TextAlign.center,
                        textColor: const Color(0XFF0085C4),
                        cornerRadius: 3,
                        borderWidth: 1,
                      ),
                    ),
                  ),
                  Container(
                    color: const Color(0xff09141F).withOpacity(0.13),
                    width: 0.5,
                  ),
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () async {
                        onCancel();
                        // 找到被选中的选项并触发确认操作
                        final selectedOption = widget.options.firstWhereOrNull((opt) => opt.checked == 'checked');
                        if (selectedOption != null) {
                          widget.confirmAction(selectedOption);
                        }
                      },
                      child: MyTextView(
                        "Confirm".tr,
                        marginLeft: 10,
                        fontSize: 17.5,
                        isFontMedium: true,
                        textAlign: TextAlign.center,
                        textColor: const Color(0XFF0085C4),
                        cornerRadius: 3,
                      ),
                    ),
                  ),
                ],
              ).constrained(height: 46),
            ],
          ),
        ),
      ],
    ).constrained(width: 295);
  }

  // 取消弹框
  void onCancel() async {
    SmartDialog.dismiss();
  }
}
