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

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:i18n_translate/i18n.dart';
import 'package:{{ name }}/repository/AuthenticationRepository.dart';
import 'package:{{ name }}/bloc/blocs.dart';

class AuthResetLink extends StatelessWidget {
  final AuthenticationRepository authenticationRepository;

  AuthResetLink({Key key, @required this.authenticationRepository})
      : assert(authenticationRepository != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: BlocProvider(
        create: (context) {
          return ResetLinkBloc(
            authenticationRepository: authenticationRepository,
          );
        },
        child: AuthResetLinkForm(),
      ),
    );
  }
}

class AuthResetLinkForm extends StatefulWidget {
  @override
  State<AuthResetLinkForm> createState() => _AuthResetLinkFormState();
}

class _AuthResetLinkFormState extends State<AuthResetLinkForm> {
  final _emailController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    _onResetLinkButtonPressed() {
      BlocProvider.of<ResetLinkBloc>(context).add(
        ResetButtonPressed(
          email: _emailController.text,
        ),
      );
    }

    return BlocListener<ResetLinkBloc, ResetLinkState>(
      listener: (context, state) {
        if (state is ResetLinkFailure) {
          Scaffold.of(context).showSnackBar(
            SnackBar(
              content: Text('${state.error}'),
              backgroundColor: Colors.red,
            ),
          );
        }
        if (state is ResetLinkSuccces) {
          Navigator.of(context).pushNamedAndRemoveUntil(
              '/login', (Route<dynamic> route) => false);
        }
      },
      child: BlocBuilder<ResetLinkBloc, ResetLinkState>(
        builder: (context, state) {
          return LayoutBuilder(builder: (context, constraints) {
            return SingleChildScrollView(
              child: ConstrainedBox(
                constraints: BoxConstraints(
                    minWidth: constraints.maxWidth,
                    minHeight: constraints.maxHeight),
                child: IntrinsicHeight(
                  child: Padding(
                    padding:
                        EdgeInsets.symmetric(horizontal: 15.0, vertical: 30.0),
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          BackButton(
                            color: Colors.grey,
                            onPressed: () => Navigator.pop(context),
                          ),
                          Padding(
                            padding: EdgeInsets.symmetric(horizontal: 15.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisSize: MainAxisSize.max,
                              children: <Widget>[
                                SizedBox(height: 15.0),
                                Text(
                                    I18n.translate(
                                        context, 'login.forgot_password'),
                                    style: TextStyle(
                                      color: Color(0xff182779),
                                      fontFamily: 'CM Sans Serif',
                                      fontWeight: FontWeight.bold,
                                      fontSize: 32.0,
                                      height: 1.5,
                                    )),
                                SizedBox(height: 15.0),
                                Text(
                                  I18n.translate(
                                      context, 'login.reset_message'),
                                  style: TextStyle(
                                    color: Color(0xFF182779),
                                    fontSize: 16.0,
                                    height: 1.2,
                                  ),
                                ),
                                SizedBox(height: 30.0),
                                TextFormField(
                                  keyboardType: TextInputType.emailAddress,
                                  decoration: InputDecoration(
                                    labelText:
                                        I18n.translate(context, 'login.email'),
                                    prefixIcon: Icon(
                                      Icons.email,
                                      color: Color(0xff0251ff),
                                    ),
                                  ),
                                  controller: _emailController,
                                ),
                              ],
                            ),
                          ),
                          Expanded(
                            child: Container(),
                          ),
                          Container(
                            width: double.infinity,
                            padding: EdgeInsets.symmetric(horizontal: 15.0),
                            child: RaisedButton(
                              color: Color(0xff0251ff),
                              padding: EdgeInsets.all(16.0),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(14.0),
                              ),
                              onPressed: state is! ResetLinkLoading
                                  ? _onResetLinkButtonPressed
                                  : null,
                              child: Text(
                                I18n.translate(
                                    context, 'login.send_reset_link'),
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 18.0,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                        ]),
                  ),
                ),
              ),
            );
          });
        },
      ),
    );
  }
}
