import 'package:aitrainer_app/bloc/password_reset/password_reset_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:google_fonts/google_fonts.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.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) => PasswordResetBloc(
              userRepository: UserRepository(),
            ),
        child: Builder(builder: (context) {
          return Scaffold(
            key: _scaffoldKey,
            appBar: AppBarMin(),
            body: BlocConsumer<PasswordResetBloc, PasswordResetState>(
              listener: (context, state) {
                if (state is PasswordResetError) {
                  ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(backgroundColor: Colors.orange, content: Text(t(state.message), style: TextStyle(color: Colors.white))));
                } else if (state is PasswordResetFinished) {
                  Navigator.of(context).pop();
                }
              },
              builder: (context, state) {
                final loginBloc = BlocProvider.of<PasswordResetBloc>(context);
                return 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: state is PasswordResetLoading,
                      opacity: 0.5,
                      color: Colors.black54,
                      progressIndicator: CircularProgressIndicator(),
                    ),
                  ),
                );
              },
            ),
          );
        }));
  }

  Widget buildResetPasswordForm(PasswordResetBloc loginBloc) {
    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(),
            TextFormField(
              key: LibraryKeys.loginEmailField,
              decoration: InputDecoration(
                contentPadding: EdgeInsets.only(left: 15, top: 15, bottom: 15),
                labelText: t('Email'),
                fillColor: Colors.white24,
                filled: true,
                border: OutlineInputBorder(
                  gapPadding: 4.0,
                  borderRadius: BorderRadius.circular(12.0),
                  borderSide: BorderSide(color: Colors.green[50]!, width: 0.4),
                ),
              ),
              initialValue: loginBloc.userRepository.user.email,
              autovalidateMode: AutovalidateMode.onUserInteraction,
              validator: (val) {
                final String? validator = loginBloc.emailValidation(val!);
                return validator == null ? null : t(validator);
              },
              onChanged: (value) => loginBloc.add(PasswordResetEmailChange(email: value)),
              keyboardType: TextInputType.emailAddress,
              style: new TextStyle(fontSize: 16, color: Colors.indigo),
            ),
            Divider(
              color: Colors.transparent,
            ),
            Divider(
              color: Colors.transparent,
            ),
            Row(mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[
              TextButton(
                  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),
                      ),
                    ],
                  ),
                  onPressed: () => loginBloc.add(PasswordResetSubmit())),
            ]),
            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),
            ]),
          ])),
    );
  }
}
