import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:kaer_with_panels/app/localization/app_translations.dart';
import 'package:kaer_with_panels/app/widgets/kr_app_text_style.dart';
import '../../../routes/app_pages.dart';
import '../../../services/kr_subscribe_service.dart';
import '../../../utils/kr_log_util.dart';
import '../controllers/kr_home_controller.dart';

/// 试用卡片组件
class KRHomeTrialCard extends GetView<KRHomeController> {
  const KRHomeTrialCard({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w),
      width: double.infinity,
      decoration: ShapeDecoration(
        color: Theme.of(context).cardColor,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(16.w),
        ),
      ),
      child: Padding(
        padding: EdgeInsets.all(14.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            // 顶部标题和订阅按钮
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  AppTranslations.kr_home.trialStatus,
                  style: KrAppTextStyle(
                    fontSize: 12,
                    color: Theme.of(context).textTheme.bodySmall?.color,
                  ),
                ),
                GestureDetector(
                  onTap: () => Get.toNamed(Routes.KR_PURCHASE_MEMBERSHIP),
                  child: Row(
                    children: [
                      Text(
                        AppTranslations.kr_home.subscribe,
                        style: KrAppTextStyle(
                          color: Colors.blue,
                          fontSize: 12,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        size: 12.w,
                        color: Colors.blue,
                      ),
                    ],
                  ),
                ),
              ],
            ),

            // 倒计时显示
            SizedBox(height: 10.w),
            Row(
              children: [
                Container(
                  padding: EdgeInsets.all(8.w),
                  decoration: BoxDecoration(
                    color: Colors.blue.withOpacity(0.1),
                    borderRadius: BorderRadius.circular(8.w),
                  ),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Icon(
                        Icons.timer_outlined,
                        color: Colors.blue,
                        size: 16.w,
                      ),
                      SizedBox(width: 4.w),
                      Text(
                        AppTranslations.kr_home.trialing,
                        style: KrAppTextStyle(
                          fontSize: 12,
                          color: Colors.blue,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(width: 12.w),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _buildCountdown(),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCountdown() {
    return Obx(() {
      final subscribeService = KRSubscribeService();
      final remainingTime = subscribeService.kr_trialRemainingTime.value;
      final isExpired = remainingTime.isEmpty;
    
      return Builder(
        builder: (context) => Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              remainingTime,
              style: KrAppTextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w500,
                color: isExpired
                    ? (DateTime.now().millisecondsSinceEpoch % 2000 < 1000
                        ? Colors.red
                        : Colors.blue)
                    : Theme.of(context).textTheme.bodyMedium?.color,
              ),
            ),
            SizedBox(height: 4.w),
            Text(
              AppTranslations.kr_home.trialEndMessage,
              style: KrAppTextStyle(
                fontSize: 12,
                color: Theme.of(context).textTheme.bodySmall?.color,
              ),
            ),
          ],
        ),
      );
    });
  }
} 