import 'dart:developer';

import 'package:softplayer_web/api/grpc/test_auth.dart';
import 'package:softplayer_web/api/grpc/test_no_auth.dart';
import 'package:web/web.dart' as web;
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter/material.dart';
import 'package:grpc/grpc_web.dart';

void main() async {
  await dotenv.load(fileName: ".env");
  String backendURL = dotenv.env['SOFTPLAYER_BACKEND_URL']!;
  GrpcWebClientChannel grpcChannel = GrpcWebClientChannel.xhr(
    Uri.parse(backendURL),
  );

  runApp(MyApp(channel: grpcChannel));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key, required this.channel});
  final GrpcWebClientChannel channel;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: true,
      title: 'Softplayer',
      home: RootWidget(channel: channel),
    );
  }
}

class RootWidget extends StatefulWidget {
  final GrpcWebClientChannel channel;
  const RootWidget({super.key, required this.channel});

  @override
  State<StatefulWidget> createState() => _StateRootWidget();
}

class _StateRootWidget extends State<RootWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () async {
                TestAuthGrpc grpc = TestAuthGrpc(channel: widget.channel);
                grpc.init();
                await grpc.pong();
              },
              child: Text("pong"),
            ),
            ElevatedButton(
              onPressed: () async {
                TestNoAuthGrpc grpc = TestNoAuthGrpc(channel: widget.channel);
                grpc.init();
                await grpc.ping();
              },
              child: Text("ping"),
            ),
          ],
        ),
      ),
    );
  }
}
