//import 'dart:async';
import 'package:agineek_pad_plugin/agineek_pad_plugin.dart';
import 'package:flutter/material.dart';
//import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isSearching = false;
  int numberOfPads= 0;
  int logCount = 5;
  bool noPadsConnectedError = false;

  @override
  void initState() {
    super.initState();
    _isSearching = false;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget> [
              SizedBox(height: 30),
              StreamBuilder<dynamic>(
                  stream: AgineekPadPlugin.numPadsConnected,
                  builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (snapshot.hasData) {
                      numberOfPads = snapshot.data as int;
                      return Text('Number of pads connecting: ${snapshot.data}');
                    } else {
                      if (numberOfPads <= 0) {
                        return const Text('No pads connected.');
                      } else {
                        return Text('Number of pads connected: $numberOfPads');
                      }
                    }
                  }
              ),
              const SizedBox(height: 30),
              if (_isSearching) ElevatedButton(
                  onPressed: () async {
                    final bool isSearching = await AgineekPadPlugin.stopConnectPads();
                    setState(() {
                      _isSearching = isSearching;
                    });
                  },
                  child: const Text('Stop Search')
              ) else ElevatedButton(
                onPressed: () async {
                  final bool isSearching = await AgineekPadPlugin.startConnectPads();
                  setState(() {
                    _isSearching = isSearching;
                    noPadsConnectedError = false;
                  });
                },
                child: const Text('Start Search')
              ),
              if (noPadsConnectedError) Center(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    "You cannot start a game with no pads connected.",
                    style: TextStyle(
                      color: Colors.red
                    ),
                  ),
                ),
              ),
              if (!_isSearching && (numberOfPads > 0)) ElevatedButton(
                  onPressed: () async {
                    //changed to button is only enabled when at least 1 pad connected.
                    /*if (numberOfPads <= 0)
                      {
                        setState(() {noPadsConnectedError = true;});
                      }
                    else {*/
                    final bool gameStarted = await AgineekPadPlugin.startGame();
                  },
                  child: Text("Start game")
              ),
              const SizedBox(height: 40),
              const Text(
                  'Log',
                style: TextStyle(
                  color: Colors.indigo
                ),
              ),
              const SizedBox(height: 20),
              Expanded(
                child: ListView.builder(
                  itemCount : logCount,
                  itemBuilder: (context, index) {
                    return Padding(
                      padding: const EdgeInsets.symmetric(vertical: 1.0, horizontal: 4.0),
                      child: Card(
                        child: ListTile(
                          title: Text("Example pad #${index} got hit"),
                          leading: Icon(
                            Icons.access_alarm,
                            color: Colors.deepPurple,
                          ),
                        ),
                      ),
                    );
                  },
                ),
              )
            ]
          ),
        ),
      ),
    );
  }
}
