import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:kaer_with_panels/app/widgets/kr_local_image.dart';
import 'package:kaer_with_panels/app/localization/app_translations.dart';
import 'package:kaer_with_panels/app/widgets/kr_app_text_style.dart';
import '../controllers/kr_order_status_controller.dart';
import 'package:kaer_with_panels/app/routes/app_pages.dart';
import 'package:kaer_with_panels/app/widgets/hi_base_scaffold.dart';
import 'package:kaer_with_panels/app/widgets/hi_help_entrance.dart';
import 'package:kaer_with_panels/app/utils/kr_event_bus.dart';

/// 订单状态视图
class KROrderStatusView extends GetView<KROrderStatusController> {
  const KROrderStatusView({super.key});

  @override
  Widget build(BuildContext context) {
    return HIBaseScaffold(
      showBackgroundImage: false,
      title: AppTranslations.kr_orderStatus.title,
      backgroundColor: Theme.of(context).primaryColor,
      topContentAreaHeight: 110,
      child: Obx(
        () => Stack(
          children: [
            Center(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 0.w),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    // 状态文本
                    _buildStatusText(),
                    SizedBox(height: 16.h),
                    // 描述文本
                    _buildDescriptionText(),
                    SizedBox(height: 32.h),
                    // 操作按钮（支付成功时显示）
                    if (controller.kr_isPaymentSuccess.value)
                      _buildSuccessButtons(),
                    SizedBox(height: 100.w),
                  ],
                ),
              ),
            ),
            const HIHelpEntrance(isLight: false),
          ],
        ),
      ),
    );
  }

  /// 构建状态图标
  Widget _buildStatusIcon() {
    return KrLocalImage(
      imageName: controller.kr_statusIcon.value,
      width: 160.w,
      height: 160.w,
    );
  }

  /// 构建状态文本
  Widget _buildStatusText() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 24.w),
      child: Text(
        controller.kr_statusTitle.value,
        textAlign: TextAlign.center,
        style: KrAppTextStyle(
          fontSize: 22,
          fontWeight: FontWeight.w600,
          color: Theme.of(Get.context!).textTheme.bodyMedium?.color,
        ),
      ),
    );
  }

  /// 构建描述文本
  Widget _buildDescriptionText() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 24.w),
      child: Text(
        controller.kr_statusDescription.value,
        textAlign: TextAlign.center,
        style: KrAppTextStyle(
          fontSize: 14,
          color: Theme.of(Get.context!).textTheme.bodySmall?.color,
        ).copyWith(height: 1.5),
      ),
    );
  }

  /// 构建支付成功后的操作按钮
  Widget _buildSuccessButtons() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 24.w),
      child: Column(
        children: [
          // 返回首页按钮
          SizedBox(
            width: double.infinity,
            height: 48.h,
            child: Obx(() {
              // 获取当前的刷新状态
              final bool isRefreshing = controller.kr_isRefreshingData.value;

              return ElevatedButton(
                // 如果正在刷新数据，则禁用点击（设为 null）
                onPressed: isRefreshing
                    ? null
                    : () async {
                  // 1. 先触发业务层的数据刷新请求
                  controller.kr_subscribeService.kr_fetchAvailableSubscribes();

                  // 2. 发送事件总线通知
                  KREventBus().kr_sendMessage(KRMessageType.kr_payment);
                  // 4. 返回到首页
                  Get.offAllNamed(Routes.KR_HOME);
                },
                style: ElevatedButton.styleFrom(
                  backgroundColor: isRefreshing ? Colors.grey[300] : Colors.white,
                  foregroundColor: Colors.black,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(12.r),
                  ),
                  elevation: 0,
                  // 禁用时的样式
                  disabledBackgroundColor: Colors.grey[200],
                ),
                child: isRefreshing
                    ? Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    // Loading 动画
                    SizedBox(
                      height: 20.w,
                      width: 20.w,
                      child: CircularProgressIndicator(
                        color: Colors.black54,
                        strokeWidth: 2.w,
                      ),
                    ),
                    SizedBox(width: 12.w),
                    // 正在刷新的提示文字
                    Text(
                      "订阅更新中...", // 或者从 AppTranslations 获取
                      style: KrAppTextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                        color: Colors.black54,
                      ),
                    ),
                  ],
                )
                    : Text(
                  AppTranslations.kr_orderStatus.backToHome,
                  style: KrAppTextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    color: Colors.black,
                  ),
                ),
              );
            }),
          ),
          SizedBox(height: 12.h),
        ],
      ),
    );
  }
}
