import 'package:flutter/Material.dart';
import 'package:get/get.dart';

import '../../utils/async_util.dart';
import '../../utils/http_handler.dart';
import '../api/atmob_api.dart';
import '../api/request/config_request.dart';
import '../api/response/config_response.dart';
import '../bean/member_pop_up_bean.dart';

class ConfigRepository {
  ConfigRepository._();

  final enable = false.obs;

  void refreshConfig() {
    AsyncUtil.retry(() => requestConfigsData(), Duration(seconds: 3),
            maxRetry: 100)
        .then((configsResponse) {
      final list = configsResponse.list;
      if (list == null || list.isEmpty) {
        debugPrint('refreshConfig....list is empty');
        return;
      }
      if (list.first.value is Map<String, dynamic>) {
        final memberPopUpBean =
            MemberPopUpBean.fromJson(list.first.value as Map<String, dynamic>);
        debugPrint(
            'refreshConfig....memberPopUpBean: ${memberPopUpBean.toJson()}');
        enable.value = memberPopUpBean.enable == true;
        debugPrint('refreshConfig....enable: ${enable.value}');
      }
    });
  }

  Future<ConfigResponse> requestConfigsData() {
    return atmobApi
        .confs(ConfigRequest(confCodes: ['member_pop_up']))
        .then(HttpHandler.handle(true));
  }
}

final configRepository = ConfigRepository._();
