mirrored_repositories/daruma-frontend/lib/ui
import 'package:contacts_service/contacts_service.dart';
import 'package:daruma/util/colors.dart';
import 'package:flutter/material.dart';

class AddMembersPage extends StatefulWidget {
  AddMembersPage({Key key, this.title}) : super(key: key);

  final String title;
  final String reloadLabel = 'Finalizar';
  final String fireLabel = 'Confirmar';
  final Color floatingButtonColor = redPrimaryColor;
  final IconData reloadIcon = Icons.refresh;
  final IconData fireIcon = Icons.check;

  @override
  _AddMembersPageState createState() => new _AddMembersPageState(
        floatingButtonLabel: this.fireLabel,
        icon: this.fireIcon,
        floatingButtonColor: this.floatingButtonColor,
      );
}

class _AddMembersPageState extends State<AddMembersPage> {
  List<Contact> _contacts = new List<Contact>();
  List<CustomContact> _uiCustomContacts = List<CustomContact>();
  List<CustomContact> _allContacts = List<CustomContact>();
  bool _isLoading = false;
  bool _isSelectedContactsView = false;
  String floatingButtonLabel;
  Color floatingButtonColor;
  IconData icon;

  _AddMembersPageState({
    this.floatingButtonLabel,
    this.icon,
    this.floatingButtonColor,
  });

  @override
  void initState() {
    super.initState();
    refreshContacts();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
        backgroundColor: redPrimaryColor,
      ),
      body: !_isLoading
          ? Container(
              child: ListView.builder(
                itemCount: _uiCustomContacts?.length,
                itemBuilder: (BuildContext context, int index) {
                  CustomContact _contact = _uiCustomContacts[index];
                  var _phonesList = _contact.contact.phones.toList();

                  return _buildListTile(_contact, _phonesList);
                },
              ),
            )
          : Center(
              child: CircularProgressIndicator(),
            ),
      floatingActionButton: new FloatingActionButton.extended(
        backgroundColor: floatingButtonColor,
        onPressed: () {
          if (!_isSelectedContactsView) {
            setState(() {
              _uiCustomContacts = _allContacts
                  .where((contact) => contact.isChecked == true)
                  .toList();
              _isSelectedContactsView = true;
              _restateFloatingButton(
                widget.reloadLabel,
                Icons.refresh,
                Colors.green,
              );
            });
          } else {
            List<Contact> members = new List<Contact>();
            for (var i = 0; i < _uiCustomContacts.length; i++) {
              members.add(_uiCustomContacts[i].contact);
            }

            Navigator.pop(context, members);
          }
        },
        icon: Icon(icon),
        label: Text(floatingButtonLabel),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }

  ListTile _buildListTile(CustomContact c, List<Item> list) {
    return ListTile(
      title: Text(c.contact.displayName ?? ""),
      subtitle: list.length >= 1 && list[0]?.value != null
          ? Text(list[0].value)
          : Text(''),
      trailing: Checkbox(
          activeColor: Colors.green,
          value: c.isChecked,
          onChanged: (bool value) {
            setState(() {
              c.isChecked = value;
            });
          }),
    );
  }

  void _restateFloatingButton(String label, IconData icon, Color color) {
    floatingButtonLabel = label;
    icon = icon;
    floatingButtonColor = color;
  }

  refreshContacts() async {
    setState(() {
      _isLoading = true;
    });
    var contacts = await ContactsService.getContacts();
    _populateContacts(contacts);
  }

  void _populateContacts(Iterable<Contact> contacts) {
    _contacts = contacts.where((item) => item.displayName != null).toList();
    _contacts.sort((a, b) => a.displayName.compareTo(b.displayName));
    _allContacts =
        _contacts.map((contact) => CustomContact(contact: contact)).toList();
    setState(() {
      _uiCustomContacts = _allContacts;
      _isLoading = false;
    });
  }
}

class CustomContact {
  final Contact contact;
  bool isChecked;

  CustomContact({
    this.contact,
    this.isChecked = false,
  });
}
