import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:timeago_flutter/timeago_flutter.dart';

import 'package:toolheim/data/github_adapter.dart';
import 'package:toolheim/data/warband_roster.dart';
import 'package:toolheim/widgets/henchmengroup_list_tile_widget.dart';
import 'package:toolheim/widgets/hero_list_tile_widget.dart';
import 'package:toolheim/widgets/value_chip_widget.dart';
import 'package:toolheim/widgets/warband_drawer_widget.dart';

class WarbandRosterScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    if (github(context).activeRoster == null) {
      return Scaffold(
          appBar: AppBar(title: const Text('Toolheim')),
          body: Builder(builder: (BuildContext context) {
            return Center(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Image.asset(
                      'assets/images/heads.png',
                      width: 250,
                    ),
                    FlatButton(
                      onPressed: () {
                        Scaffold.of(context).openDrawer();
                      },
                      child: Text(
                        'Select a warband',
                        style: TextStyle(color: Colors.blue),
                      ),
                    ),
                  ]),
            );
          }),
          drawer: Drawer(
              child: SingleChildScrollView(child: WarbandDrawerWidget())));
    }

    WarbandRoster roster = github(context).activeRoster;

    return Scaffold(
        drawer: warbandDrawer(context),
        appBar: AppBar(title: Text(roster.name)),
        body: SingleChildScrollView(
            child: Column(children: [
          Padding(
            padding: const EdgeInsets.only(left: 10),
            child: Row(
              children: <Widget>[
                ValueChipWidget('CP', roster.campaignPoints.toString()),
                ValueChipWidget('Rating', roster.rating().toString()),
                ValueChipWidget('Rout Limit', roster.routLimit().toString()),
              ],
            ),
          ),
          Divider(),
          ...units(roster),
          warbandMetaInformation(roster)
        ])));
  }

  Widget warbandMetaInformation(WarbandRoster roster) {
    return Padding(
      padding: const EdgeInsets.only(left: 40, right: 40, top: 30),
      child: Table(
        columnWidths: {0: FixedColumnWidth(80)},
        children: <TableRow>[
          metaTableRow('Alignment', roster.alignment),
          metaTableRow('Objective', roster.objective),
          metaTableRow('Achivements', roster.achievments),
          metaTableRow('Shards', roster.shards),
          metaTableRow('GC', roster.gc),
          metaTableRow('Equipment', roster.equipment),
        ],
      ),
    );
  }

  TableRow metaTableRow(String name, dynamic value) {
    return TableRow(children: [
      Text(
        name.toString().trim() + ':',
        style: TextStyle(fontSize: 12),
        textAlign: TextAlign.right,
      ),
      Padding(
        padding: const EdgeInsets.only(left: 10, bottom: 10),
        child: Text(
          value.toString().trim(),
          style: TextStyle(fontWeight: FontWeight.bold),
        ),
      )
    ]);
  }

  Drawer warbandDrawer(BuildContext context) {
    return Drawer(
        child: Column(children: <Widget>[
      Expanded(child: SingleChildScrollView(child: WarbandDrawerWidget())),
      Container(
          color: Colors.brown,
          padding: EdgeInsets.all(10),
          child: Align(
              alignment: Alignment.bottomLeft,
              child: Timeago(
                  date: github(context).lastSync,
                  builder: (_, value) => Text(
                        'Last sync: ' + value,
                        style: TextStyle(color: Colors.white),
                      ))))
    ]));
  }

  List<Widget> units(WarbandRoster roster) {
    return [
      for (var hero in roster.heros) HeroListTileWidget(hero),
      Divider(),
      for (var henchmenGroup in roster.henchmenGroups)
        HenchmengroupListTileWidget(henchmenGroup),
      Divider()
    ];
  }

  GitHubAdapter github(context) => Provider.of<GitHubAdapter>(context);
}
