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 '../../../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;

  // 订阅计划列表
  final List<Map<String, dynamic>> plans = [
    {
      'price': '\$40.00',
      'period': 'Per year',
      'title': 'Yearly Plan',
      'badge': 'Mostly choose',
      'badgeBgColor': DarkThemeColors.bg1,
      'badgeTextColor': DarkThemeColors.subscriptionColor,
      'badgeBorderColor': DarkThemeColors.dividerColor, // null 表示没有边框
    },
    {'price': '\$58.00', 'period': 'once', 'title': 'Life time', 'badge': null},
    {'price': '\$58.00', 'period': 'Per', 'title': 'Month Plan', 'badge': null},
    {
      'price': '\$1.00',
      'period': 'Per week',
      'title': 'Week Plan',
      'badge': 'Limited Time',
      '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: 'Success',
      message: 'Subscription plan changed successfully',
    );
  }

  // 恢复购买
  void restorePurchases() {
    // TODO: 实现恢复购买逻辑
    IXSnackBar.showIXSnackBar(title: 'Info', message: 'Restoring purchases...');
  }

  // 支付问题
  void handlePaymentIssue() {
    // TODO: 实现支付问题处理逻辑
    IXSnackBar.showIXSnackBar(
      title: 'Info',
      message: 'Opening payment support...',
    );
  }
}
