import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

import '../../../../config/theme/dark_theme_colors.dart';
import '../../../../config/translations/strings_enum.dart';
import '../../../components/ix_snackbar.dart';
import '../../../constants/assets.dart';

class SubscriptionController extends GetxController {
  // 视频播放器控制器
  late VideoPlayerController videoController;
  final isVideoInitialized = false.obs;

  // 当前选中的订阅计划索引 (0: 年度, 1: 终身, 2: 月度, 3: 周度)
  final selectedPlanIndex = 0.obs;

  // 订阅计划列表
  List<Map<String, dynamic>> get plans => [
    {
      'price': '\$40.00',
      'period': Strings.perYear.tr,
      'title': Strings.yearlyPlan.tr,
      'badge': Strings.mostlyChoose.tr,
      'badgeBgColor': DarkThemeColors.bg1,
      'badgeTextColor': DarkThemeColors.subscriptionColor,
      'badgeBorderColor': DarkThemeColors.dividerColor, // null 表示没有边框
    },
    {
      'price': '\$58.00',
      'period': Strings.once.tr,
      'title': Strings.lifeTime.tr,
      'badge': null,
    },
    {
      'price': '\$58.00',
      'period': Strings.perYear.tr,
      'title': Strings.monthPlan.tr,
      'badge': null,
    },
    {
      'price': '\$1.00',
      'period': Strings.perWeek.tr,
      'title': Strings.weekPlan.tr,
      'badge': Strings.limitedTime.tr,
      'badgeBgColor': DarkThemeColors.primaryColor,
      'badgeTextColor': Colors.white,
      'badgeBorderColor': null,
    },
  ];

  @override
  void onInit() {
    super.onInit();
    _initializeVideoPlayer();
  }

  @override
  void onClose() {
    videoController.dispose();
    super.onClose();
  }

  // 初始化视频播放器
  void _initializeVideoPlayer() {
    videoController = VideoPlayerController.asset(Assets.subscriptionBg)
      ..initialize()
          .then((_) {
            isVideoInitialized.value = true;
            videoController.setLooping(true);
            videoController.setVolume(0); // 静音播放
            videoController.play();
          })
          .catchError((error) {
            print('视频初始化失败: $error');
          });
  }

  // 选择订阅计划
  void selectPlan(int index) {
    selectedPlanIndex.value = index;
  }

  // 确认变更
  void confirmChange() {
    // TODO: 实现确认订阅变更逻辑
    IXSnackBar.showIXSnackBar(
      title: Strings.success.tr,
      message: Strings.subscriptionChanged.tr,
    );
  }

  // 恢复购买
  void restorePurchases() {
    // TODO: 实现恢复购买逻辑
    IXSnackBar.showIXSnackBar(
      title: Strings.info.tr,
      message: Strings.restoringPurchases.tr,
    );
  }

  // 支付问题
  void handlePaymentIssue() {
    // TODO: 实现支付问题处理逻辑
    IXSnackBar.showIXSnackBar(
      title: Strings.info.tr,
      message: Strings.openingPaymentSupport.tr,
    );
  }
}
