// This project is not supposed to be cross-platform,
// so we don't care about this warning
// ignore: avoid_web_libraries_in_flutter
import 'dart:html';

import 'package:flutter/material.dart';
import 'package:softplayer_web/api/grpc/accounts.dart';
import 'package:softplayer_web/components/sign_in_form.dart';
import 'package:softplayer_web/components/sign_up_form.dart';

class MenuPanel extends StatefulWidget implements PreferredSizeWidget {
  final TabName tab;
  final AccountsGrpc accountsGrpc;
  const MenuPanel({
    super.key,
    required this.tab,
    required this.accountsGrpc,
  }) : preferredSize = const Size.fromHeight(kToolbarHeight);
  @override
  final Size preferredSize;
  @override
  State<StatefulWidget> createState() => _MenuPanel();
}

enum TabName { home, catalog, about }

class _MenuPanel extends State<MenuPanel> {
  bool isSignedIn() {
    return window.localStorage.containsKey("token");
  }

  List<Widget> accountActions() {
    if (isSignedIn()) {
      return [
        IconButton(
            onPressed: () => print("acc"),
            icon: const Icon(Icons.account_circle))
      ];
    } else {
      return <Widget>[
        TextButton(
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (BuildContext context) => SignInForm(
                        accountsGrpc: widget.accountsGrpc,
                      ));
            },
            child: const Text("sign in")),
        TextButton(
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (BuildContext context) => SignUpForm(
                        accountsGrpc: widget.accountsGrpc,
                      ));
            },
            child: const Text("sign up")),
      ];
    }
  }

  @override
  PreferredSizeWidget build(BuildContext context) {
    return AppBar(
      title: Row(children: [
        TextButton(
            child: const Text("Softplayer"),
            onPressed: () {
              Navigator.pushNamed(context, "/");
            }),
      ]),
      backgroundColor: const Color.fromRGBO(46, 51, 78, 1.0),
      automaticallyImplyLeading: false,
      actions: accountActions(),
    );
  }
}
