import 'dart:html';

import 'package:grpc/grpc_web.dart';
import 'package:softplayer_dart_proto/main.dart';

class EnvironmentLocalData {
  EnvironmentLocalData({
    required this.uuid,
    required this.token,
  });
  String uuid;
  String token;
}

class EnvironmentsGrpc {
  final GrpcWebClientChannel channel;
  late EnvironmentsClient envStub;
  EnvironmentsGrpc({
    required this.channel,
  });

  void init() {
    envStub = EnvironmentsClient(channel);
  }

  Stream<List<String>> list() async* {
    List<String> envs = [];
    try {
      await for (var feature in envStub.list(Empty(),
          options: CallOptions(metadata: {
            "uuid": window.localStorage["uuid"]!,
            "token": window.localStorage["token"]!,
          }))) {
        envs.add(feature.data.name);
      }
    } catch (e) {
      rethrow;
    }
    yield envs;
  }
}
