import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:softplayer_web/core/api/v1/test.dart';
import 'package:softplayer_web/core/tokens/application/tokens_application.dart';

final pingControllerProvider = AsyncNotifierProvider<PingController, String>(
  PingController.new,
);

class PingController extends AsyncNotifier<String> {
  @override
  Future<String> build() async {
    return 'Idle';
  }

  Future<void> ping() async {
    state = const AsyncLoading();

    state = await AsyncValue.guard(() async {
      final testGrpc = ref.read(testGrpcProvider);
      final tokenCtrl = ref.read(tokensControllerProvider.notifier);
      await tokenCtrl.checkTokens();

      await testGrpc.pong();

      return 'Ping successful';
    });
  }
}
