import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:get_it/get_it.dart';

import '../dao/group_dao.dart';
import '../entity/group.dart';
import '../services/database.dart';
import '../widgets/text_field.dart';

final getIt = GetIt.instance;

class GroupRegistrationArguments {
  final int id;

  GroupRegistrationArguments(this.id);
}

class GroupRegistration extends StatefulWidget {
  static const routeName = 'group-registration';

  const GroupRegistration({Key? key}) : super(key: key);

  @override
  _GroupRegistrationState createState() => _GroupRegistrationState();
}

class _GroupRegistrationState extends State<GroupRegistration> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final TextEditingController name = TextEditingController();
  final TextEditingController description = TextEditingController();
  final TextEditingController clubId = TextEditingController();

  int? id;

  final DatabaseService database = getIt<DatabaseService>();

  final _title = 'Group Registration';

  final EdgeInsetsGeometry _defaultEdgeInsets =
      const EdgeInsets.symmetric(horizontal: 0.0, vertical: 16.0);

  @override
  void dispose() {
    name.dispose();
    description.dispose();
    clubId.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final GroupDao dao = database.groupDao;
    final args = ModalRoute.of(context)!.settings.arguments
        as GroupRegistrationArguments;

    return FutureBuilder<Group?>(
        future: dao.findGroupById(args.id),
        builder: (BuildContext context, AsyncSnapshot<Group?> snapshot) {
          if (snapshot.hasData) {
            final Group group = snapshot.data as Group;
            id = group.id;
            name.text = group.name;
            description.text = group.description;
          }

          return PlatformScaffold(
              appBar: PlatformAppBar(
                title: Text(_title),
              ),
              body: Form(
                  key: _formKey,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      CustomTextField(
                          labelText: 'Group Name',
                          hintText: 'Enter Group Name',
                          controller: name),
                      CustomTextField(
                          labelText: 'Description',
                          hintText: 'Enter Group Description',
                          controller: description),
                      Padding(
                          padding: _defaultEdgeInsets,
                          child: PlatformButton(
                              onPressed: () async {
                                await Future.delayed(
                                    const Duration(milliseconds: 300));
                                if (_formKey.currentState!.validate()) {
                                  if (id == null) {
                                    final Group group = Group(
                                        null,
                                        name.text,
                                        description.text,
                                        int.parse(clubId.text));
                                    await dao.insertGroup(group);
                                  } else {
                                    final Group group = Group(
                                        null,
                                        name.text,
                                        description.text,
                                        int.parse(clubId.text));
                                    await dao.updateGroup(group);
                                  }

                                  if (!mounted) return;
                                  ScaffoldMessenger.of(context).showSnackBar(
                                      const SnackBar(
                                          content: Text('Processing Data')));
                                  Navigator.pop(context);
                                }
                              },
                              child: const Text('Submit')))
                    ],
                  )));
        });
  }
}
