import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flashlink/config/theme/theme_extensions/theme_extension.dart';
import 'package:screenshot/screenshot.dart';
import 'package:flashlink/app/widgets/click_opacity.dart';

import '../../../../config/theme/dark_theme_colors.dart';
import '../../../../config/translations/strings_enum.dart';
import '../../../../utils/formater.dart';
import '../../../constants/assets.dart';
import '../../../widgets/ix_image.dart';
import '../../../widgets/submit_btn.dart';
import '../controllers/precode_controller.dart';

/// Pre Code 保存弹窗
class PrecodeSaveDialog extends StatelessWidget {
  final PrecodeController controller;

  const PrecodeSaveDialog({super.key, required this.controller});

  @override
  Widget build(BuildContext context) {
    return _buildDialogContent();
  }

  /// 构建弹窗内容
  Widget _buildDialogContent() {
    return Padding(
      padding: EdgeInsets.all(24.w),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Screenshot(
            controller: controller.screenshotController,
            child: Container(
              width: double.infinity,
              decoration: BoxDecoration(
                color: Get.reactiveTheme.highlightColor,
                borderRadius: BorderRadius.circular(20.r),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  // 内容区域
                  Padding(
                    padding: EdgeInsets.symmetric(
                      horizontal: 10.w,
                      vertical: 20.w,
                    ),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        // Logo
                        _buildLogo(),

                        10.verticalSpaceFromWidth,

                        Divider(
                          color: Get.reactiveTheme.dividerColor,
                          height: 1.w,
                        ),

                        10.verticalSpaceFromWidth,

                        // Pre Code 标题和数字
                        _buildPreCode(),

                        10.verticalSpaceFromWidth,

                        Divider(
                          color: Get.reactiveTheme.dividerColor,
                          height: 1.w,
                        ),

                        10.verticalSpaceFromWidth,

                        // 说明文字
                        _buildDescription(),

                        20.verticalSpaceFromWidth,

                        // 安全提示
                        _buildSecurityTip(),

                        20.verticalSpaceFromWidth,

                        // UID 信息
                        _buildUidInfo(),

                        10.verticalSpaceFromWidth,

                        // Premium 标签
                        _buildPremiumBadge(),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          50.verticalSpaceFromWidth,
          // Save 按钮
          _buildSaveButton(),

          20.verticalSpaceFromWidth,

          // Cancel 按钮
          _buildCancelButton(),
        ],
      ),
    );
  }

  /// Logo - 盾牌形状
  Widget _buildLogo() {
    return IXImage(
      source: Assets.splashLogo,
      width: 70.w,
      height: 94.w,
      sourceType: ImageSourceType.asset,
    );
  }

  /// Pre Code
  Widget _buildPreCode() {
    return Column(
      children: [
        Text(
          controller.preCode.value,
          style: TextStyle(
            fontSize: 28.sp,
            height: 1.2,
            fontWeight: FontWeight.w500,
            color: Get.reactiveTheme.textTheme.bodyLarge!.color,
            letterSpacing: 1.2,
          ),
        ),
      ],
    );
  }

  /// 说明文字
  Widget _buildDescription() {
    return Text(
      Strings.preCodeInfoMessage.tr,
      textAlign: TextAlign.center,
      style: TextStyle(
        fontSize: 14.sp,
        color: Get.reactiveTheme.hintColor,
        height: 1.4,
      ),
    );
  }

  /// 安全提示
  Widget _buildSecurityTip() {
    return Text(
      Strings.pleaseStoreSecurely.tr,
      textAlign: TextAlign.center,
      style: TextStyle(
        fontSize: 14.sp,
        color: Get.reactiveTheme.hintColor,
        height: 1.4,
      ),
    );
  }

  /// UID 信息
  Widget _buildUidInfo() {
    final uid = controller.uid;
    final displayUid = 'UID ${formatDeviceId(uid)}';

    return Text(
      displayUid,
      style: TextStyle(
        fontSize: 14.sp,
        color: DarkThemeColors.validTermColor,
        height: 1.4,
      ),
    );
  }

  /// Premium 标签
  Widget _buildPremiumBadge() {
    return Obx(
      () => IXImage(
        source: controller.isPremium.value ? Assets.premium : Assets.free,
        width: controller.isPremium.value ? 92.w : 92.w,
        height: 28.w,
        sourceType: ImageSourceType.asset,
      ),
    );
  }

  /// Save 按钮
  Widget _buildSaveButton() {
    return SubmitButton(
      prefixIcon: IXImage(
        source: Assets.preCodeSaveLocal,
        width: 20.w,
        height: 20.w,
        sourceType: ImageSourceType.asset,
      ),
      text: Strings.save.tr,
      onPressed: controller.saveToGallery,
    );
  }

  /// Cancel 按钮
  Widget _buildCancelButton() {
    return ClickOpacity(
      onTap: () {
        Navigator.of(Get.context!).pop();
      },
      child: Container(
        height: 52.w,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12.r),
          border: Border.all(color: Get.reactiveTheme.dividerColor, width: 1.w),
        ),
        alignment: Alignment.center,
        child: Text(
          Strings.cancel.tr,
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w600,
            color: Get.reactiveTheme.hintColor,
          ),
        ),
      ),
    );
  }
}

/// 盾牌形状裁剪器
class ShieldClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    final path = Path();
    final width = size.width;
    final height = size.height;

    // 绘制盾牌形状
    path.moveTo(width * 0.5, 0); // 顶部中心

    // 右上圆弧
    path.quadraticBezierTo(width * 0.75, 0, width, height * 0.2);

    // 右侧直线
    path.lineTo(width, height * 0.6);

    // 底部曲线到中心点
    path.quadraticBezierTo(width, height * 0.85, width * 0.5, height);

    // 从底部中心到左侧
    path.quadraticBezierTo(0, height * 0.85, 0, height * 0.6);

    // 左侧直线
    path.lineTo(0, height * 0.2);

    // 左上圆弧
    path.quadraticBezierTo(0, 0, width * 0.25, 0);

    path.close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
