import 'package:flutter/material.dart';
import 'package:preferences/preferences.dart';
import 'package:provider/provider.dart';
import 'package:toolheim/data/github_adapter.dart';

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

    //String _repository = 'Labernator/Mordheim';
    //String _path = 'Mordheim-BorderTownBurning/Warband Rosters';

    return Scaffold(
        appBar: AppBar(title: Text('Settings')),
        body: PreferencePage([
          PreferenceTitle('GitHub'),
          PreferenceText(
              'Provide a valid GitHub repository with username and project (username/repository-name). This repository must contain warband roster files in subfolders. See the sample project for a kickstart.'),
          TextFieldPreference(
            'Repository',
            'repository',
            defaultVal: 'f0086/toolheim-example',
          ),
          PreferenceText(
              'If your warband folders are placed in a subfolder, you can specify it here.'),
          TextFieldPreference('Path', 'path', defaultVal: '/players'),
          PreferenceTitle('Search for Warbands'),
          PreferenceText(
              'Search the given GitHub repository for valid Warband files (ending with .warband.yml). This step can be done at any time.'),
          FlatButton(
              onPressed: () async {
                showSyncWaitingDialog(context);
                await github.search();

                Navigator.of(context, rootNavigator: true).pop();
                if (github.syncErrors.length > 0) {
                  showErrorDialog(context, github);
                }
              },
              child:
                  Text('Start search', style: TextStyle(color: Colors.blue))),
        ]));
  }

  static Widget buildSyncErrors(BuildContext context) {
    GitHubAdapter github = Provider.of<GitHubAdapter>(context);

    return Column(children: [
      for (var error in github.syncErrors)
        Text(error, style: TextStyle(color: Colors.red))
    ]);
  }

  void showErrorDialog(context, github) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
              title: Text('Errors'),
              content:
                  Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
                Text("We've got some errors while searching for warbands"),
                SizedBox(
                  height: 50,
                ),
                buildSyncErrors(context),
              ]),
              actions: <Widget>[
                FlatButton(
                  child: Text('Close', style: TextStyle(color: Colors.blue)),
                  onPressed: () {
                    github.syncErrors.clear();
                    Navigator.of(context, rootNavigator: true).pop();
                  },
                )
              ]);
        });
  }

  void showSyncWaitingDialog(context) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Search warbands ...'),
            content: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
              Text(
                  'Checking the GitHub repository for suitable files. This can take a while ...'),
              SizedBox(
                height: 50,
              ),
              CircularProgressIndicator(),
            ]),
          );
        });
  }
}
