import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../index.dart';

class DebugDialog extends GetView<TwelveEcgViewController> {
  const DebugDialog({Key? key, required this.imageBase64}) : super(key: key);

  final String imageBase64;

  // 主视图
  Widget _buildView() {
    const designWidth = 1280.0; // 设计尺寸宽度：1280
    final width = Get.width;
    final scale = width / designWidth; // 计算缩放比例
    final ScrollController scrollController = ScrollController();
    return Container(
      width: Get.width * 0.9 / scale,
      height: 240 * 3,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
      clipBehavior: Clip.antiAlias,
      child: Column(
        children: [
          _buildHead(),
          Expanded(
            child: Scrollbar(
              thumbVisibility: true,
              thickness: 10,
              radius: const Radius.circular(10),
              controller: scrollController,
              child: SingleChildScrollView(
                controller: scrollController,
                padding: const EdgeInsets.only(left: 10, right: 10, bottom: 20),
                physics: const BouncingScrollPhysics(),
                scrollDirection: Axis.horizontal,
                child: Center(
                  child: displayImage(imageBase64),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget displayImage(String base64Url) {
    // 去掉"data:image/png;base64,"前缀
    String base64Image = base64Url.split(',').last;

    // 解码Base64字符串为字节数组
    Uint8List imageData = base64Decode(base64Image);

    return Image.memory(
      imageData,
      fit: BoxFit.cover, // 根据需要设置适当的fit属性
    );
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: _buildView(),
    );
  }

  /// 构建弹窗顶部，右侧显示关闭按钮
  Widget _buildHead() {
    return SizedBox(
      height: 50,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          IconButton(
            onPressed: () => Get.back(),
            icon: const Icon(
              Icons.close,
              size: 30,
            ),
          ),
        ],
      ),
    );
  }
}
