import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/pages/lab_export/entitys/image_type.dart';
import 'package:get/get.dart';

// ignore: must_be_immutable
class ImageSettingDialog<T> extends StatelessWidget {
  final String title;

  final String confirmText;
  final String cancelText;
  final bool showCloseIcon;
  final bool showCancelButton;
  final ExportImageFormat imageType;

  /// 确定回调，不为空则显示确定按钮
  final VoidCallback? onConfirm;
  final VoidCallback? onCancel;
  ImageSettingDialog({
    Key? key,
    required this.title,
    this.confirmText = "确定",
    this.cancelText = "取消",
    this.showCloseIcon = true,
    this.showCancelButton = true,
    this.onConfirm,
    this.onCancel,
    required this.imageType,
  }) : super(key: key);

  // 添加状态变量来跟踪用户选择
  var imageFormat = 0.obs; // 0 for jpg, 1 for png
  var videoFormat = 0.obs; // 0 for mp4, 1 for avi

  static ButtonStyle buttonStyle = ElevatedButton.styleFrom(
    elevation: 0.0,
    backgroundColor: LabColors.buttonColor,
    padding: EdgeInsets.symmetric(
      horizontal: 12,
      vertical: 5,
    ),
  );

  static const TextStyle buttonTextStyle = TextStyle(
    fontSize: 15,
    height: 1,
    color: LabColors.text200,
  );

  static Future<T?> showDialog<T>(Widget child,
      [bool barrierDismissible = false]) async {
    return await Get.dialog(
      child,
      barrierDismissible: barrierDismissible,
      barrierColor: LabColors.base100.withOpacity(0.1),
    );
  }

  @override
  Widget build(BuildContext context) {
    imageFormat.value = imageType.imageType;
    videoFormat.value = imageType.videoType;
    return Dialog(
      backgroundColor: Colors.transparent,
      insetPadding: EdgeInsets.all(0),
      child: Container(
        width: 600,
        clipBehavior: Clip.antiAlias,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5),
          border: Border.all(
            color: LabColors.mainPageHeaderColor,
            width: 2,
            strokeAlign: 0.0,
          ),
          color: LabColors.base200,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              color: LabColors.mainPageHeaderColor,
              padding: EdgeInsets.symmetric(vertical: 5),
              child: SizedBox(
                height: 40,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(
                      width: 15,
                    ),
                    Text(
                      title,
                      style: TextStyle(
                        fontSize: 18,
                        color: LabColors.text200,
                      ),
                    ),
                    Expanded(child: Container()),
                    if (showCloseIcon)
                      IconButton(
                        icon: Icon(
                          Icons.close_rounded,
                          color: LabColors.text200,
                          size: 20,
                        ),
                        onPressed: () {
                          Get.back(result: null);
                        },
                      ),
                  ],
                ),
              ),
            ),

            ///内容
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 120, vertical: 20),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Row(
                    children: [
                      Text(
                        "图片格式：",
                        style: TextStyle(
                          fontSize: 18,
                          color: LabColors.text200,
                        ),
                      ),
                      SizedBox(
                        width: 30,
                      ),
                      SizedBox(
                        width: 38,
                        child: Row(
                          children: [
                            Text(
                              "jpg",
                              style: TextStyle(
                                fontSize: 18,
                                color: LabColors.text200,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Obx(
                        () => Radio(
                          value: 0,
                          groupValue: imageFormat.value,
                          onChanged: (value) {
                            imageFormat.value = value as int;
                            imageType.imageType = imageFormat.value;
                          },
                        ),
                      ),
                      SizedBox(
                        width: 30,
                      ),
                      SizedBox(
                        width: 35,
                        child: Row(
                          children: [
                            Text(
                              "png",
                              style: TextStyle(
                                fontSize: 18,
                                color: LabColors.text200,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Obx(
                        () => Radio(
                          value: 1,
                          groupValue: imageFormat.value,
                          onChanged: (value) {
                            imageFormat.value = value as int;
                            imageType.imageType = imageFormat.value;
                          },
                        ),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Row(
                    children: [
                      Text(
                        "视频格式：",
                        style: TextStyle(
                          fontSize: 18,
                          color: LabColors.text200,
                        ),
                      ),
                      SizedBox(
                        width: 30,
                      ),
                      SizedBox(
                        width: 38,
                        child: Row(
                          children: [
                            Text(
                              "mp4",
                              style: TextStyle(
                                fontSize: 18,
                                color: LabColors.text200,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Obx(
                        () => Radio(
                          value: 0,
                          groupValue: videoFormat.value,
                          onChanged: (value) {
                            videoFormat.value = value as int;
                            imageType.videoType = videoFormat.value;
                          },
                        ),
                      ),
                      SizedBox(
                        width: 30,
                      ),
                      SizedBox(
                        width: 35,
                        child: Row(
                          children: [
                            Text(
                              "avi",
                              style: TextStyle(
                                fontSize: 18,
                                color: LabColors.text200,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Obx(
                        () => Radio(
                          value: 1,
                          groupValue: videoFormat.value,
                          onChanged: (value) {
                            videoFormat.value = value as int;
                            imageType.videoType = videoFormat.value;
                          },
                        ),
                      ),
                    ],
                  )
                ],
              ),
            ),
            if (onConfirm != null)
              Container(
                height: 40,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Container(
                      width: 100,
                      child: ElevatedButton(
                        style: buttonStyle,
                        onPressed: () {
                          Get.back(result: imageType);
                        },
                        child: Text(
                          confirmText,
                          style: buttonTextStyle,
                        ),
                      ),
                    ),
                    if (showCancelButton)
                      SizedBox(
                        width: 20,
                      ),
                    if (showCancelButton)
                      Container(
                        width: 100,
                        child: ElevatedButton(
                          style: buttonStyle,
                          child: Text(
                            cancelText,
                            style: buttonTextStyle,
                          ),
                          onPressed: () {
                            if (onCancel != null)
                              onCancel?.call();
                            else
                              Get.back(result: null);
                          },
                        ),
                      )
                  ],
                ),
              ),
            if (onConfirm != null)
              SizedBox(
                height: 15,
              ),
          ],
        ),
      ),
    );
  }
}
