import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badges/badges.dart';
import 'package:toolheim/data/github_adapter.dart';
import 'package:toolheim/screens/settings_screen.dart';
import 'package:url_launcher/url_launcher.dart';

class WarbandDrawerWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    GitHubAdapter github = Provider.of<GitHubAdapter>(context);

    if (github.repository == null || github.activeRoster == null) {
      return Padding(
        padding: const EdgeInsets.only(top: 100, left: 30, right: 30),
        child: Column(children: <Widget>[
          if (github.activeRoster == null)
            Text(
                'The repository is set, but no warbands are found. Try to search for warbands on the settings screen.')
          else
            Text(
                'There is no repository set up. Please open the settings and provide a valid GitHub repository.'),
          FlatButton(
            onPressed: () {
              Navigator.popAndPushNamed(context, '/settings');
            },
            child: Text(
              'Open Settings',
              style: TextStyle(color: Colors.blue),
            ),
          ),
          Text(
              'If you have no clue what this app is all about, open the help screen and read the introduction.'),
          FlatButton(
            onPressed: openHelpWebsite,
            child: Text(
              'Help',
              style: TextStyle(color: Colors.blue),
            ),
          ),
        ]),
      );
    }

    return Column(children: <Widget>[
      UserAccountsDrawerHeader(
        currentAccountPicture: CircleAvatar(
          child: Text(github.activeRoster.campaignPoints.toString() + ' CP'),
        ),
        margin: const EdgeInsets.all(0),
        otherAccountsPictures: <Widget>[
          IconButton(
              icon: Icon(Icons.refresh),
              color: Colors.white,
              highlightColor: Colors.brown,
              tooltip: 'Refresh warbands',
              onPressed: () async {
                await github.update();
                if (github.syncErrors.length > 0) {
                  showDialog(
                      context: context,
                      builder: (BuildContext context) {
                        return AlertDialog(
                            title: Text('We have some errors while updating.'),
                            content: SettingsScreen.buildSyncErrors(context),
                            actions: <Widget>[
                              FlatButton(child: Text('Ok'), onPressed: () {})
                            ]);
                      });
                }
              }),
          IconButton(
            icon: Icon(Icons.settings),
            color: Colors.white,
            highlightColor: Colors.brown,
            onPressed: () {
              Navigator.popAndPushNamed(context, '/settings');
            },
          ),
          IconButton(
              icon: Icon(Icons.help),
              color: Colors.white,
              highlightColor: Colors.brown,
              onPressed: openHelpWebsite),
        ],
        accountName: Text(github.activeRoster.name),
        accountEmail: Text(github.activeRoster.race),
      ),
      if (github.isSyncInProgress) LinearProgressIndicator(),
      for (var roster in github.rosters)
        ListTile(
          onTap: () {
            roster.unseen = false;
            github.activeRoster = roster;
            github.save();
            Navigator.of(context).pop();
          },
          title: Text(roster.name + ' (' + roster.playerName + ')',
              style: TextStyle(
                color: !roster.active ? Colors.black45 : Colors.black,
                fontWeight: roster.unseen ? FontWeight.bold : FontWeight.normal,
              )),
          subtitle: Text(roster.version.message),
          isThreeLine: true,
          trailing: Badge(
            badgeColor: Colors.black12,
            shape: BadgeShape.square,
            borderRadius: 2,
            badgeContent: Text(roster.rating().toString(),
                style: TextStyle(color: Colors.white)),
          ),
        ),
    ]);
  }

  void openHelpWebsite() async {
    const url = 'https://aaron-fischer.net/apw';
    if (await canLaunch(url)) {
      await launch(url);
    }
  }
}
