import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:keyboard/resource/string.gen.dart';
import 'package:lottie/lottie.dart';

import '../../../resource/assets.gen.dart';
import '../../../resource/colors.gen.dart';
import '../../../utils/styles.dart';
import '../../../widget/horizontal_dashed_line.dart';
import '../../../widget/vertical_dots.dart';
import 'goods_surprise_controller.dart';

// 限时活动 惊喜买断价弹窗
class SurpriseDialog {
  static const String tag = 'SurpriseDialog';

  static void show({VoidCallback? clickConfirm,VoidCallback? clickCancel}) {
    SmartDialog.show(
      tag: tag,
      backType: SmartBackType.block,
      clickMaskDismiss: false,
      maskColor: ColorName.black70,
      onDismiss: () => Get.delete<GoodsSurpriseController>(),
      animationType: SmartAnimationType.centerScale_otherSlide,
      builder: (_) {
        final controller = Get.find<GoodsSurpriseController>();
        return Stack(
          children: [
            Positioned(
              top: 0,
              left: 0,
              right: 0,
              bottom: 0,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Stack(
                    clipBehavior: Clip.none,
                    children: [
                      Container(
                        width: 328.w,
                        height: 290.h,
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: Assets.images.bgSurpriseDialog.provider(),
                            fit: BoxFit.cover,
                          ),
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            SizedBox(height: 23.h),
                            Container(
                              width: 240.w,
                              height: 108.h,
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                  image:
                                      Assets.images.bgSurpriseDialogPrices
                                          .provider(),
                                  fit: BoxFit.cover,
                                ),
                              ),
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Obx(() {
                                    return _buildPricesPartOne(controller);
                                  }),
                                  HorizontalDashedLine(
                                    width: 200.w,
                                    color: Color(0xFFFF9451),
                                    strokeWidth: 2.h,
                                    dashLength: 5.w,
                                    dashSpace: 3.w,
                                  ),
                                  Obx(() {
                                    return Text(
                                      controller.firstAmount?.description ?? "",
                                      style: TextStyle(
                                        color: const Color(0xFFFFC172),
                                        fontSize: 10.sp,
                                        fontWeight: FontWeight.w400,
                                        height: 2,
                                      ),
                                    );
                                  }),
                                ],
                              ),
                            ),
                            SizedBox(height: 16.h),
                            GestureDetector(
                              onTap: () {
                                clickConfirm?.call();
                                SmartDialog.dismiss(tag: tag);
                              },
                              child: Container(
                                width: 240.w,
                                height: 60.h,
                                decoration: BoxDecoration(
                                  image: DecorationImage(
                                    image:
                                        Assets.images.bgSurpriseDialogButton
                                            .provider(),
                                    fit: BoxFit.cover,
                                  ),
                                ),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [

                                    SizedBox(
                                      child: Assets
                                          .images
                                          .iconSurpriseDialogButton
                                          .image(width: 120.w, height: 20.h),
                                    ),
                                    Obx(() {
                                      int totalMilliseconds =
                                          controller.timeLeft.value;
                                      int minutes = (totalMilliseconds ~/ 6000);
                                      int seconds =
                                          (totalMilliseconds ~/ 100) % 60;
                                      int milliseconds =
                                          (totalMilliseconds % 100); // 毫秒
                                      return Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        children: [
                                          _buildTimeBox(
                                            minutes.toString().padLeft(2, '0'),
                                          ),
                                          VerticalDots(
                                            height: 8.h,
                                            dotSize: 2.w,
                                          ),
                                          _buildTimeBox(
                                            seconds.toString().padLeft(2, '0'),
                                          ),
                                          VerticalDots(
                                            height: 8.h,
                                            dotSize: 2.w,
                                          ),
                                          _buildTimeBox(
                                            milliseconds.toString().padLeft(
                                              2,
                                              '0',
                                            ),
                                          ),
                                          Text(
                                            StringName.surpriseDialogEndDesc,
                                            style: Styles.getTextStyleWhiteW400(
                                              10.sp,
                                            ),
                                          ),
                                        ],
                                      );
                                    }),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Positioned(
                        top: -50,
                        right: 0,
                        left: 0,
                        child: Assets.images.iconSurpriseDialogTitle.image(
                          width: 264.w,
                          height: 95.h,
                        ),
                      ),
                    ],
                  ),

                  Container(
                    margin: EdgeInsets.only(top: 24.h),
                    child: GestureDetector(
                      onTap: () {
                        SmartDialog.dismiss(tag: tag);
                        clickCancel?.call();
                      },
                      child: Assets.images.iconCharacterDialogClose.image(
                        width: 40.r,
                        height: 40.r,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            IgnorePointer(
              child: Lottie.asset(
                Assets.anim.animSurpriseDialogData,
                repeat: false,
                width: 360.w,
              ),
            ),
          ],
        );
      },
    );
  }

  static Widget _buildPricesPartOne(GoodsSurpriseController controller) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          controller.firstAmount?.priceDescNumber ?? "",
          style: TextStyle(
            color: Colors.white,
            fontSize: 48.sp,
            fontWeight: FontWeight.w700,
          ),
        ),
        Column(
          children: [
            Container(
              width: 30.w,
              height: 18.h,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: Assets.images.iconSurpriseDialogOnly.provider(),
                  fit: BoxFit.cover,
                ),
              ),
              child: Center(
                child: Text(
                  StringName.surpriseDialogOnly,
                  style: TextStyle(
                    color: const Color(0xFFFF451D),
                    fontSize: 11.sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ),
            Text(
              controller.firstAmount?.priceDescUnit ?? '',
              style: TextStyle(
                color: Colors.white,
                fontSize: 14.sp,
                fontWeight: FontWeight.w700,
              ),
            ),
          ],
        ),
      ],
    );
  }

  static Widget _buildTimeBox(String value) {
    return Container(
      width: 18.w,
      height: 15.h,
      decoration: ShapeDecoration(
        color: Colors.white,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.r)),
      ),
      child: Center(
        child: Text(value, style: Styles.getTextStyleBlack204W500(10.sp)),
      ),
    );
  }
}


