import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:get_it/get_it.dart';

import './configuration.dart';
import './group_list.dart';
import './login_screen.dart';
import './player_list.dart';
import '../services/auth.dart';

final getIt = GetIt.instance;

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final Auth _auth = getIt<Auth>();

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<AuthState>(
        stream: _auth.authState(),
        // ignore: body_might_complete_normally
        builder: (BuildContext context, AsyncSnapshot<AuthState> snapshot) {
          if (snapshot.data == AuthState.pending) {
            // Do something to await AuthState completion
          } else if (snapshot.data == AuthState.unauthenticated) {
            Navigator.pushNamed(context, LoginScreen.routeName);
          }

          return PlatformScaffold(
              appBar: PlatformAppBar(
                title: const Text('Agineek',
                    style: TextStyle(
                        fontFamily: 'Collegiate',
                        fontSize: 46,
                        color: Colors.black)),
                backgroundColor: Colors.green,
                trailingActions: <Widget>[
                  PlatformIconButton(
                      onPressed: () {
                        Navigator.pushNamed(context, ConfigPage.routeName);
                      },
                      icon: Icon(context.platformIcons.settings))
                ],
              ),
              body: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                        flex: 1,
                        child: ListView(
                          padding: const EdgeInsets.symmetric(
                              vertical: 10.0, horizontal: 5.0),
                          children: [
                            _buildButton(
                                context: context,
                                label: 'Login',
                                routeName: LoginScreen.routeName),
                            PlatformButton(
                                onPressed: () => Navigator.pushNamed(
                                    context, LoginScreen.routeName),
                                child: const Text('Start Game'))
                          ],
                        )),
                    SizedBox(
                      height: 48.0,
                      child: ListView(
                        scrollDirection: Axis.horizontal,
                        children: <Widget>[
                          _buildIconButton(
                              context: context,
                              label: 'Players',
                              icon: Icon(context.platformIcons.person),
                              routeName: PlayerList.routeName),
                          _buildIconButton(
                              context: context,
                              label: 'Clubs',
                              icon: Icon(context.platformIcons.flag),
                              routeName: 'club-list'),
                          _buildIconButton(
                              context: context,
                              label: 'Groups',
                              icon: Icon(context.platformIcons.group),
                              routeName: GroupList.routeName),
                          const VerticalDivider(),
                          _buildIconButton(
                              context: context,
                              label: 'Statistics',
                              icon: const Icon(Icons.insert_chart),
                              routeName: 'statistics')
                        ],
                      ),
                    )
                  ]));
        });
  }

  PlatformButton _buildButton(
      {required BuildContext context,
      required String label,
      required String routeName}) {
    return PlatformButton(
        onPressed: () => Navigator.pushNamed(context, routeName),
        child: Text(label));
  }

  PlatformButton _buildIconButton(
      {required BuildContext context,
      required String label,
      required Icon icon,
      required String routeName}) {
    return PlatformButton(
        onPressed: () => Navigator.pushNamed(context, routeName),
        child: Column(children: [icon, Text(label)]));
  }
}
