import 'package:electronic_assistant/base/app_base_request.dart';
import 'package:electronic_assistant/data/api/atmob_api.dart';
import 'package:electronic_assistant/utils/async_util.dart';
import 'package:electronic_assistant/utils/cancel_future.dart';
import 'package:electronic_assistant/utils/http_handler.dart';

import '../api/response/example_info_response.dart';

class ConfigRepository {
  ConfigRepository._();

  ExampleInfoResponse? _exampleInfo;

  Future<ExampleInfoResponse> getExampleInfo() async {
    if (_exampleInfo != null) {
      return _exampleInfo!;
    }
    return await AsyncUtil.retryWhen(() => _requestExampleInfo(), 3,
        const Duration(seconds: 1), (error) => true);
  }

  Future<ExampleInfoResponse> _requestExampleInfo() {
    return atmobApi
        .exampleInfo(AppBaseRequest())
        .then(HttpHandler.handle(true))
        .then((response) {
      _exampleInfo = response;
      return response;
    });
  }
}

final configRepository = ConfigRepository._();
