import 'dart:io';

import 'package:clean/base/base_page.dart';
import 'package:clean/module/image_picker/image_picker_util.dart';
import 'package:clean/module/photo_info/photo_info_controller.dart';
import 'package:clean/utils/expand.dart';
import 'package:clean/utils/file_utils.dart';
import 'package:flutter/Material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import '../../resource/assets.gen.dart';
import '../../utils/image_util.dart';
import 'dart:typed_data';

class PhotoInfoPage extends BasePage<PhotoInfoController> {
  const PhotoInfoPage({super.key});

  @override
  bool statusBarDarkFont() => false;

  @override
  bool immersive() => true;

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        SafeArea(
          child: Stack(
            children: [
              Column(
                // mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(left: 16.w, top: 14.h),
                    child: GestureDetector(
                      onTap: () {
                        Get.back();
                      },
                      child: Assets.images.iconCommonBack
                          .image(width: 28.w, height: 28.w),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 16.w, top: 12.h),
                    child: Obx(() {
                      if (controller.imageList.isEmpty) {
                        return SizedBox.shrink();
                      }
                      return Text(
                        controller.imageList[controller.currentImageIndex.value]
                            .dateTitle ??
                            "",
                        style: TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.w500,
                          fontSize: 24.sp,
                        ),
                      );
                    }),
                  ),
                  SizedBox(
                    height: 20.h,
                  ),
                  _buildImageCarousel(),
                ],
              ),
              Visibility(
                visible: controller.type.value == FileType.analysis,
                child: Positioned(
                  left: 0,
                  right: 0,
                  bottom: 0,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        margin: EdgeInsets.only(left: 18.w),
                        child: Obx(() {
                          return Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                "Analysis Results",
                                style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.w900,
                                  fontSize: 16.sp,
                                ),
                              ),
                              Visibility(
                                visible: controller.createTime.value.isNotEmpty,
                                child: Column(
                                  children: [
                                    SizedBox(
                                      height: 20.h,
                                    ),
                                    Text(
                                      controller.createTime.value,
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.w500,
                                        fontSize: 13.sp,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Visibility(
                                visible: controller.fileName.value.isNotEmpty,
                                child: Column(
                                  children: [
                                    SizedBox(
                                      height: 12.h,
                                    ),
                                    Text(
                                      controller.fileName.value,
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.w500,
                                        fontSize: 13.sp,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Visibility(
                                visible: controller.model.value.isNotEmpty,
                                child: Column(
                                  children: [
                                    SizedBox(
                                      height: 12.h,
                                    ),
                                    Text(
                                      controller.model.value,
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.w500,
                                        fontSize: 13.sp,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Visibility(
                                visible: controller.focalLength.value.isNotEmpty,
                                child: Column(
                                  children: [
                                    SizedBox(
                                      height: 12.h,
                                    ),
                                    Text(
                                      controller.focalLength.value,
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.w500,
                                        fontSize: 13.sp,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Visibility(
                                visible: controller.size.value.isNotEmpty,
                                child: Column(
                                  children: [
                                    SizedBox(
                                      height: 12.h,
                                    ),
                                    Text(
                                      controller.size.value,
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.w500,
                                        fontSize: 13.sp,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Visibility(
                                visible: controller.iso.value.isNotEmpty,
                                child: Column(
                                  children: [
                                    SizedBox(
                                      height: 12.h,
                                    ),
                                    Text(
                                      controller.iso.value,
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.w500,
                                        fontSize: 13.sp,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Visibility(
                                visible: controller.address.value.isNotEmpty,
                                child: Column(
                                  children: [
                                    SizedBox(
                                      height: 12.h,
                                    ),
                                    Text(
                                      controller.address.value,
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.w500,
                                        fontSize: 13.sp,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              SizedBox(height: 32.h,),
                            ],
                          );
                        }),
                      ),
                      Center(
                        child: GestureDetector(
                          onTap: () {
                            controller.deleteBtnClick(
                                controller.imageList[
                                controller.currentImageIndex.value],
                                controller.currentImageIndex.value);
                          },
                          child: Container(
                            width: 328.w,
                            height: 48.h,
                            decoration: BoxDecoration(
                              color: "#0279FB".color,
                              borderRadius: BorderRadius.all(
                                Radius.circular(10.r),
                              ),
                            ),
                            child: Center(
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Assets.images.iconPrivacyPhotoDelete
                                      .image(width: 18.w, height: 18.h),
                                  SizedBox(
                                    width: 5.w,
                                  ),
                                  Obx(() {
                                    if (controller.imageList.isEmpty) {
                                      return SizedBox.shrink();
                                    }
                                    return Text(
                                      controller.formatFileSize(controller
                                          .imageList[controller
                                          .currentImageIndex.value]
                                          .size ??
                                          0),
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 16.sp,
                                        fontWeight: FontWeight.w500,
                                      ),
                                    );
                                  }),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
        IgnorePointer(
          child: Assets.images.bgHome.image(
            width: 360.w,
          ),
        ),
      ],
    );
  }

  // 轮播图组件
  Widget _buildImageCarousel() {
    return Obx(() {
      if (controller.imageList.isEmpty) {
        return SizedBox.shrink();
      }

      return SizedBox(
        height: 492.h,
        width: ScreenUtil().screenWidth,
        child: PageView.builder(
          controller: PageController(
            viewportFraction: 1, // 显示部分下一张图片
            initialPage: controller.currentImageIndex.value,
          ),
          onPageChanged: (index) {
            controller.currentImageIndex.value = index;
            controller.loadPhotoInfo();
            PhotoManager.clearFileCache();
          },
          itemCount: controller.imageList.length,
          itemBuilder: (context, index) {
            final asset = controller.imageList[index];
            return AnimatedPadding(
              duration: Duration(milliseconds: 300),
              padding: EdgeInsets.symmetric(
                horizontal: 0.w,
                vertical:
                controller.currentImageIndex.value == index ? 0 : 20.h,
              ),
              child: GestureDetector(
                // onTap: () => _showImageDetail(asset),
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.r),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withOpacity(0.2),
                        blurRadius: 8,
                        offset: Offset(0, 4),
                      ),
                    ],
                  ),
                  child: ClipRRect(
                    child: FutureBuilder<File?>(
                      key: ValueKey(asset.id),
                      future:
                      ImageUtil.getImageFile(controller.type.value, asset),
                      builder: (context, snapshot) {
                        if (snapshot.hasData && snapshot.data != null) {
                          return InteractiveViewer(
                            minScale: 0.5,
                            maxScale: 3.0,
                            child: Image.file(
                              snapshot.data!,
                              fit: BoxFit.fitHeight,
                              width: double.infinity,
                              height: 400,
                              filterQuality: FilterQuality.high,
                            ),
                          );
                        }

                        return Container(
                          color: Colors.grey[800],
                          child: Icon(Icons.error, color: Colors.white60),
                        );
                      },
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      );
    });
  }
}
