import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:get_it/get_it.dart';
import 'package:skillcourt/dao/club_dao.dart';
import 'package:skillcourt/entity/club.dart';
import 'package:skillcourt/services/database.dart';
import 'package:skillcourt/widgets/text_field.dart';

final getIt = GetIt.instance;

class ClubRegistrationArguments {
  final int id;

  ClubRegistrationArguments(this.id);
}

class ClubRegistration extends StatefulWidget {
  static const routeName = 'club-registration';

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

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

class _ClubRegistrationState extends State<ClubRegistration> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  final TextEditingController clubName = TextEditingController();
  final TextEditingController firstName = TextEditingController();
  final TextEditingController lastName = TextEditingController();
  Club? club;
  // var result;
  final DatabaseService database = getIt<DatabaseService>();

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

  @override
  void dispose() {
    firstName.dispose();
    lastName.dispose();
    clubName.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final ClubDao dao = database.clubDao;

    final args =
        ModalRoute.of(context)!.settings.arguments as ClubRegistrationArguments;

    return FutureBuilder(
        future: dao.findClubById(args.id),
        builder: (context, AsyncSnapshot<Club?> snapshot) {
          if (snapshot.hasData) {
            final Club club = snapshot.data as Club;
            firstName.text = club.ownerFirstName;
            lastName.text = club.ownerLastName;
            clubName.text = club.name;
          }

          return PlatformScaffold(
              appBar: PlatformAppBar(title: const Text('Club Registration')),
              body: Form(
                  key: _formKey,
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        CustomTextField(
                            controller: clubName,
                            labelText: 'Club Name',
                            hintText: 'Name your club'),
                        CustomTextField(
                            labelText: 'First Name',
                            hintText: "Input the owner's first name",
                            controller: firstName),
                        CustomTextField(
                            labelText: 'Last Name',
                            hintText: "Input the owner's last name",
                            controller: lastName),
                        Padding(
                            padding: _defaultEdgeInsets,
                            child: PlatformElevatedButton(
                              onPressed: () async {
                                await Future.delayed(
                                    const Duration(milliseconds: 300));
                                if (_formKey.currentState!.validate()) {
                                  if (args.id != -1) {
                                    final clubInfoDao = database.clubDao;
                                    club =
                                        await clubInfoDao.findClubById(args.id);
                                  }
                                  if (club == null) {
                                    club = Club(null, clubName.text,
                                        firstName.text, lastName.text);
                                    await dao.insertClub(club!);
                                  } else {
                                    club = Club(club!.id, clubName.text,
                                        firstName.text, lastName.text);
                                    await dao.updateClub(club!);
                                  }

                                  if (!mounted) return;

                                  ScaffoldMessenger.of(context).showSnackBar(
                                      const SnackBar(
                                          content: Text('Processing Data')));

                                  Navigator.pop(context);
                                }
                              },
                              child: const Text('Submit'),
                            ))
                      ])));
        });
  }
}
