import 'package:aitrainer_app/bloc/reset_password_bloc.dart';
import 'package:aitrainer_app/repository/user_repository.dart';
import 'package:aitrainer_app/util/trans.dart';
import 'package:aitrainer_app/widgets/app_bar_min.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_bloc/flutter_form_bloc.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';

import '../library_keys.dart';

// ignore: must_be_immutable
class ResetPasswordPage extends StatelessWidget with Trans {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    setContext(context);
    return BlocProvider(
        create: (context) => ResetPasswordFormBloc(
              userRepository: UserRepository(),
            ),
        child: Builder(builder: (context) {
          final loginBloc = BlocProvider.of<ResetPasswordFormBloc>(context);
          return Scaffold(
            key: _scaffoldKey,
            appBar: AppBarMin(),
            body: FormBlocListener<ResetPasswordFormBloc, String, String>(
              /* onSubmitting: (context, state) {
                LoadingDialog.show(context);
              }, */
              onSuccess: (context, state) {
                Navigator.of(context).pop();
              },
              onFailure: (context, state) {
                showInSnackBar(state.failureResponse);
              },
              child: SafeArea(
                bottom: false,
                child: Container(
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('asset/image/WT_login.jpg'),
                      fit: BoxFit.cover,
                      alignment: Alignment.center,
                    ),
                  ),
                  child: ModalProgressHUD(
                    child: buildResetPasswordForm(loginBloc),
                    inAsyncCall: loginBloc.loading == true,
                    opacity: 0.5,
                    color: Colors.black54,
                    progressIndicator: CircularProgressIndicator(),
                  ),
                ),
              ),
            ),
          );
        }));
  }

  Widget buildResetPasswordForm(ResetPasswordFormBloc formBloc) {
    return Form(
      key: _formKey,
      child: Container(
          padding: const EdgeInsets.only(left: 25, right: 50),
          child: ListView(shrinkWrap: false, padding: EdgeInsets.only(top: 150.0), children: <Widget>[
            Divider(
              color: Colors.transparent,
            ),
            Divider(),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                new InkWell(
                  child: new Text(t('I forgot the password'), style: TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
                ),
              ],
            ),
            Divider(),
            TextFieldBlocBuilder(
              key: LibraryKeys.loginEmailField,
              textFieldBloc: formBloc.emailField,
              decoration: InputDecoration(
                fillColor: Colors.white,
                filled: true,
                labelText: 'Email',
              ),
            ),
            Divider(
              color: Colors.transparent,
            ),
            Divider(
              color: Colors.transparent,
            ),
            Row(mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[
              new FlatButton(
                  key: LibraryKeys.loginOKButton,
                  child: Stack(
                    alignment: Alignment.center,
                    children: [
                      Image.asset('asset/icon/gomb_zold_a.png', width: 140, height: 60),
                      Text(
                        t("OK"),
                        style: GoogleFonts.archivoBlack(fontSize: 20, color: Colors.white),
                      ),
                    ],
                  ),
                  //Image.asset('asset/image/WT_OK.png', width: 100, height: 100),
                  onPressed: () => {formBloc.add(SubmitFormBloc())}),
            ]),
            Divider(
              color: Colors.transparent,
            ),
            Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[
              new InkWell(
                child: new Text(t('Login')),
                onTap: () => Navigator.of(context).pushNamed('login'),
              ),
              Spacer(flex: 1),
            ]),
          ])),
    );
  }

  void showInSnackBar(String error) {
    _scaffoldKey.currentState
        .showSnackBar(SnackBar(backgroundColor: Colors.orange, content: Text(error, style: TextStyle(color: Colors.white))));
  }
}