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 '../../config/translations/strings_enum.dart';
import '../data/models/launch/upgrade.dart';
import '../widgets/submit_btn.dart';

class UpdateDialog extends StatelessWidget {
  final Upgrade? upgrade;
  final VoidCallback? onUpdate;
  final VoidCallback? onLater;

  const UpdateDialog({
    super.key,
    required this.upgrade,
    this.onUpdate,
    this.onLater,
  });

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.r)),
      child: Container(
        constraints: BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width * 0.85,
          maxHeight: 360.w,
        ),
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            Container(
              width: double.infinity,
              decoration: BoxDecoration(
                color: Get.reactiveTheme.highlightColor,
                border: Border.all(
                  color: Get.reactiveTheme.dividerColor,
                  width: 1.w,
                ),
                borderRadius: BorderRadius.all(
                  Radius.circular(24.r),
                ), // 四个角 16dp
              ),
              child: Column(
                children: [
                  24.verticalSpace,
                  Icon(
                    Icons.rocket,
                    size: 96.w,
                    color: Get.reactiveTheme.primaryColor,
                  ),
                  16.verticalSpace,
                  // Title
                  Text(
                    Strings.newVersionAvailable.tr,
                    style: TextStyle(
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      height: 1.4,
                      color: Get.reactiveTheme.textTheme.bodyLarge!.color,
                    ),
                  ),
                  4.verticalSpace,
                  // Version
                  Text(
                    "${upgrade?.versionName}",
                    style: TextStyle(
                      fontSize: 12.sp,
                      height: 1.4,
                      color: Get.reactiveTheme.hintColor,
                    ),
                  ),
                  12.verticalSpace,
                  SingleChildScrollView(
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 24.w),
                      child: ConstrainedBox(
                        constraints: BoxConstraints(
                          maxHeight: 80.w, // 设置最大高度
                        ),
                        child: SingleChildScrollView(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              ...upgrade!.info!
                                  .split('\n')
                                  .map(
                                    (text) => Column(
                                      children: [
                                        _buildBulletPoint(text),
                                        4.verticalSpace,
                                      ],
                                    ),
                                  ),
                              16.verticalSpace,
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Positioned(
              bottom: 24.w,
              left: 0,
              right: 0,
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                child: Row(
                  children: [
                    if (upgrade?.forced == false)
                      Expanded(
                        child: SubmitButton(
                          onPressed: onLater ?? () {},
                          text: Strings.later.tr,
                        ),
                      ),
                    if (upgrade?.forced == false) 8.horizontalSpace,
                    Expanded(
                      child: SubmitButton(
                        text: Strings.updateNow.tr,
                        onPressed: onUpdate,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBulletPoint(String text) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '• ',
          style: TextStyle(
            fontSize: 12.sp,
            height: 1.4,
            color: Get.reactiveTheme.hintColor,
          ),
        ),
        Expanded(
          child: Text(
            text,
            style: TextStyle(
              fontSize: 12.sp,
              height: 1.4,
              color: Get.reactiveTheme.hintColor,
            ),
          ),
        ),
      ],
    );
  }
}
