import 'package:cached_network_image/cached_network_image.dart';
import 'package:clean/data/bean/wallpapers_bean.dart';
import 'package:clean/resource/assets.gen.dart';
import 'package:clean/resource/colors.gen.dart';
import 'package:clean/utils/toast_util.dart';
import 'package:flutter/Material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';

const tag = 'wallpaperDetailDialog';

void wallpaperDetailDialog(
    {WallpapersBean? wallpapersBean}) {
  SmartDialog.show(
      tag: tag,
      backType: SmartBackType.normal,
      clickMaskDismiss: false,
      maskColor: ColorName.black,
      builder: (_) {
        return Container(
            height: double.infinity,
            width: double.infinity,
            child: Container(
              child: Stack(
                children: [
                  Container(
                      child: SafeArea(
                          child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      _titleCard(),
                      Container(
                          width: 322.w,
                          height: 572.h,
                          decoration: ShapeDecoration(
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(20.r),
                            ),
                          ),
                          child: AspectRatio(
                            aspectRatio: 9 / 16, // 设置9:16的宽高比

                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(16.r),
                              child: CachedNetworkImage(
                                imageUrl: wallpapersBean?.thumbUrl ?? "",
                                fit: BoxFit.cover,
                                placeholder: (context, url) =>
                                    CircularProgressIndicator(),
                                // 加载时显示一个指示器
                                errorWidget: (context, url, error) =>
                                    Icon(Icons.error),
                              ),
                            ),
                          )),
                      Spacer(
                        flex: 1,
                      ),
                      GestureDetector(
                          onTap: () {
                            downloadWallpaper(wallpapersBean?.imageUrl);
                          },
                          child: Container(
                            width: 328.w,
                            height: 48.h,
                            decoration: ShapeDecoration(
                              color: Color(0xFF0279FB),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10.r),
                              ),
                            ),
                            child: Center(
                              child: Text(
                                'Download',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 16,
                                  fontFamily: 'Source Han Sans CN',
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          )),
                      Spacer(
                        flex: 1,
                      ),
                    ],
                  ))),
                  IgnorePointer(
                    child: Assets.images.bgHome.image(
                      width: 360.w,

                    ),
                  ),
                ],
              ),
            ));
      });
}

void downloadWallpaper(String? imageUrl) async {
  try {
    ToastUtil.show("Downloading...");
    final saveFile = await DefaultCacheManager().getSingleFile(imageUrl ?? "");

    await ImageGallerySaver.saveFile(saveFile.path);
    ToastUtil.show("Download success");
  } catch (e) {
    ToastUtil.show("Download failed");
  }
}

Widget _titleCard() {
  return Container(
    alignment: Alignment.centerLeft,
    padding: EdgeInsets.only(left: 16.w, top: 14.h, right: 16.w, bottom: 20.h),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        GestureDetector(
          onTap: () => SmartDialog.dismiss(tag: tag),
          child: Assets.images.iconBackArrow.image(
            width: 28.w,
            height: 28.h,
          ),
        ),
      ],
    ),
  );
}
