import 'dart:js_interop';

import 'package:flutter/material.dart';
import 'package:grpc/grpc_web.dart';
import 'package:softplayer_web/api/grpc/environments.dart';

class EnvirnomentList extends StatefulWidget {
  EnvirnomentList({
    super.key,
    required this.channel,
  });

  final GrpcWebClientChannel channel;
  @override
  State<StatefulWidget> createState() => _EnvirnomentListState();
}

class _EnvirnomentListState extends State<EnvirnomentList> {
  late EnvironmentsGrpc envGrpc;
  List<String> envs = [];
  @override
  void initState() {
    super.initState();
    envGrpc = EnvironmentsGrpc(channel: widget.channel);
    envGrpc.init();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
            child: StreamBuilder(
                stream: envGrpc.list(),
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return CircularProgressIndicator();
                  } else if (snapshot.connectionState == ConnectionState.done) {
                    if (snapshot.hasError) {
                      return Text('Error!');
                    } else {
                      return Column(
                        children: snapshot.data!.map((e) => Text(e)).toList(),
                      );
                    }
                  }
                    return Text("err");
                })));
  }
}

//class EnvirnomentList extends StatelessWidget {
//  EnvirnomentList({
//    super.key,
//    required this.channel,
//  });
//  final GrpcWebClientChannel channel;
//  late List<String> envs;
//  late EnvironmentsGrpc envGrpc;
//  List<String> getEnvs() {
//    List<String> envs = [];
//    envGrpc.list().then((value) {
//      return value;
//    }).catchError((e) {
//      return envs;
//    });
//    return envs;
//  }
//
//  List<Widget> bootstrapCards(List<String> envs) {
//    List<Widget> cards = [];
//    envs.forEach((element) {
//      cards.add(Center(child: Text(element)));
//    });
//    return cards;
//  }
//
//
//  @override
//  Widget build(BuildContext context) {
//    envGrpc = EnvironmentsGrpc(channel: channel);
//    envGrpc.init();
//    envs = getEnvs();
//    return GridView.count(
//      crossAxisCount: 2,
//      children: bootstrapCards(envs),
//    );
// // children: List.generate(100, (index) {
// //   return Center(
// //     child: Text(
// //       'Item $index',
// //       style: Theme.of(context).textTheme.headlineSmall,
// //     ),
// //   );
// // }),
// // );
//   // return GridView.count(
//   //       children: bootstrapCards(getEnvs()),
//     //   );
//  }
//}
