import 'dart:async';

import 'package:flutter/Material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import '../resource/assets.gen.dart';
import '../resource/colors.gen.dart';
import '../utils/styles.dart';

class PermissionDialog {
  static Future<bool> showRequestDialog(
      String titleTxt,
      Widget desc,
      String sureTxt,
      String permissionDesc, {
        Future<bool> Function()? sureClick,

      }) async {
    const tag = 'PermissionDialog';
    final completer = Completer<bool>();

    SmartDialog.show(
      tag: tag,
      alignment: Alignment.center,
      builder: (_) {
        return Container(
          width: 300.w,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(12),
          ),
          child: Stack(
            children: [
              Container(
                padding: EdgeInsets.symmetric(horizontal: 19.w),
                child: IntrinsicHeight(
                  child: Column(
                    children: [
                      SizedBox(height: 20.h),
                      Text(
                        titleTxt,
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 18.sp,
                          color: Colors.black,
                        ),
                      ),
                      SizedBox(height: 14.h),
                      desc,
                      SizedBox(height: 30.h),
                      GestureDetector(
                        onTap: () async {
                          if (sureClick != null) {
                            Get.snackbar(
                              '权限请求',
                              permissionDesc,
                              snackPosition: SnackPosition.TOP,
                              backgroundColor: Colors.black.withOpacity(0.8),
                              colorText: Colors.white,
                              duration: null,
                              animationDuration: Duration(milliseconds: 0),
                            );

                            bool shouldDismiss = await sureClick();

                            Get.closeAllSnackbars();

                            if (shouldDismiss) {
                              SmartDialog.dismiss(tag: tag);
                              if (!completer.isCompleted) {
                                completer.complete(true);
                              }
                            }
                          }
                        },
                        child: Container(
                          decoration:
                          Styles.getActivateButtonDecoration(50.r),
                          width: 130.w,
                          height: 40.w,
                          child: Center(
                            child: Text(
                              sureTxt,
                              style: TextStyle(
                                fontSize: 16.sp,
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ),
                      ),
                      SizedBox(height: 24.h),
                    ],
                  ),
                ),
              ),
              Positioned(
                top: 17.w,
                right: 18.w,
                child: GestureDetector(
                  onTap: () {
                    SmartDialog.dismiss(tag: tag);
                    if (!completer.isCompleted) {
                      completer.complete(false);
                    }
                  },
                  child: Assets.images.iconCustomDialogClose.image(
                    width: 24.w,
                    height: 24.w,
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );

    return completer.future;
  }


  static Widget buildStorageView() {
    return RichText(
      textAlign: TextAlign.center,
      text: TextSpan(
        style: TextStyle(fontSize: 15.sp, color: ColorName.black80),
        children: const <TextSpan>[
          TextSpan(text: '使用该功能App需要访问您设备权限'),
          TextSpan(
            text: '“照片和媒体”',
            style: TextStyle(
              fontWeight: FontWeight.bold,
              color: ColorName.black90,
            ),
          ),
          TextSpan(text: '，开启权限后，您即可上传图片并体验相关服务。'),
        ],
      ),
    );
  }
}
