import 'package:get/get_rx/src/rx_types/rx_types.dart';

import '../../base/base_request.dart';
import '../../utils/http_handler.dart';
import '../api/atmob_api.dart';
import '../api/response/user_info_response.dart';

class UserRepository {

  UserRepository._();

  Rxn<UserInfoResponse> get userInfo => _userInfo;

  final Rxn<UserInfoResponse> _userInfo = Rxn<UserInfoResponse>();

  Future<UserInfoResponse> getUserInfo() {
    return atmobApi
        .userInfo(BaseRequest())
        .then(HttpHandler.handle(false))
        .then((response) {
      _userInfo.value = response;
      return response;
    });
  }

  // 检测是否为会员
  bool isVip() {
    // 检查用户信息和会员信息是否存在
    if (userInfo.value?.memberInfo == null) {
      return false;
    }

    final memberInfo = userInfo.value!.memberInfo!;

    // 检查是否是会员
    if (!memberInfo.isMember) {
      return false;
    }

    // 检查会员是否过期
    if (memberInfo.endTimestamp == null) {
      return false;
    }

    // 将时间戳转换为DateTime并比较
    final endTime = DateTime.fromMillisecondsSinceEpoch(memberInfo.endTimestamp ?? 0);
    final now = DateTime.now();

    return now.isBefore(endTime);
  }
}

final userRepository = UserRepository._();