import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart'; // 引入PDFView
import 'package:get/get.dart';
import 'package:getx_scaffold/common/common.dart';

import 'certificate_viewer_controller.dart';

class CertificateViewerPage extends GetView<CertificateViewerController> {
  const CertificateViewerPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CertificateViewerController>(
      init: CertificateViewerController(),
      id: 'certificateviewer',
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: Text(controller.title)),
          body: Column(
            children: [
              SizedBox(height: 16,),
              Text(
                "点击可查看大图",
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              Expanded(
                child: ListView.builder(
                  padding: const EdgeInsets.all(16.0),
                  itemCount: controller.attachments.length,
                  itemBuilder: (context, index) {
                    final url = controller.attachments[index];
                    return _buildAttachmentItem(url);
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  /// 构建单个附件的显示项
  Widget _buildAttachmentItem(String url) {
    return GestureDetector(
      onTap: () {
        controller.openAttachment(url);
      }, // 点击跳转到详情页
      child: Card(
        margin: const EdgeInsets.only(bottom: 16.0),
        clipBehavior: Clip.antiAlias, // 确保内容不会溢出Card的圆角
        elevation: 4,
        // 等比缩放展示
        child: AspectRatio(
          aspectRatio: 4 / 3,
          child: controller.isPdf(url)
              ? Obx(() {
                  final bool loading = controller.isLoading[url] ?? true;
                  final String? localPath = controller.localPdfPaths[url];

                  if (loading) {
                    return _buildLoadingIndicator();
                  } else if (localPath != null && localPath.isNotEmpty) {
                    return IgnorePointer(
                      ignoring: true, // 设置为 true 来忽略所有指针事件
                      child: PDFView(
                        fitEachPage: true,
                        filePath: localPath,
                        fitPolicy: FitPolicy.WIDTH,
                        // 适配宽度
                        enableSwipe: false,
                        swipeHorizontal: false,
                        autoSpacing: false,
                        pageFling: false,
                        preventLinkNavigation: true, // 顺便禁用PDF内部链接的跳转
                      ),
                    );
                  } else {
                    // PDF加载失败
                    return _buildErrorIndicator();
                  }
                })
              : Image.network(
                  url,
                  fit: BoxFit.contain,
                  // 图片加载时显示loading
                  loadingBuilder: (context, child, loadingProgress) {
                    if (loadingProgress == null) return child;
                    return _buildLoadingIndicator();
                  },
                  // 图片加载失败时显示错误
                  errorBuilder: (context, error, stackTrace) {
                    return _buildErrorIndicator();
                  },
                ),
        ),
      ),
    );
  }

  // 辅助Widget：加载中指示器
  Widget _buildLoadingIndicator() {
    return const SizedBox(height: 200, child: Center(child: CircularProgressIndicator()));
  }

  // 辅助Widget：错误指示器
  Widget _buildErrorIndicator() {
    return const SizedBox(
      height: 200,
      child: Center(child: Icon(Icons.error_outline, color: Colors.red, size: 48)),
    );
  }
}
