import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/model/repository/app_repository.dart';
import 'package:e_commerce_training/src/page/forgot_password/forgot_password_cubit.dart';
import 'package:e_commerce_training/src/page/forgot_password/forgot_password_state.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:e_commerce_training/src/widget/button_widget.dart';
import 'package:e_commerce_training/src/widget/custom_appbar.dart';
import 'package:e_commerce_training/src/widget/pending_action.dart';
import 'package:e_commerce_training/src/widget/text_field_widget.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class ForgotPassPage extends StatefulWidget {
  const ForgotPassPage({Key? key}) : super(key: key);

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

class _ForgotPassPageState extends State<ForgotPassPage> {
  final TextEditingController _emailController = TextEditingController();
  late ForgotPassCubit _forgotPassCubit;

  @override
  void initState() {
    AppRepository repository = AppRepository();
    _forgotPassCubit = ForgotPassCubit(repository);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: R.color.white,
      appBar: appBar(context, R.string.recover_password.tr()),
      body: BlocProvider(
        create: (context) => _forgotPassCubit,
        child: BlocConsumer<ForgotPassCubit, ForgotPassState>(
          listener: (context, state) {
            if (state is ForgotPassFailure)
              Util.showErrorSnackBar(context, state.error!);
            else if (state is ForgotPassSuccess) {
              Util.showToast(state.message);
              Navigator.of(context).pop();
            }
          },
          builder: (context, state) {
            return GestureDetector(
              onTap: () => Util.hideKeyboard(context),
              child: Stack(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(left: 20, right: 20, top: 40),
                    child: Column(
                      children: <Widget>[
                        TextFieldWidget(
                          labelText: R.string.email.tr(),
                          hintText: 'you@gmail.com',
                          controller: _emailController,
                          icon: R.drawable.ic_username,
                          textInputAction: TextInputAction.done,
                          errorText: _forgotPassCubit.errorEmail,
                          onChanged: (text) => _forgotPassCubit.validateEmail,
                          onSubmitted: (text) => _forgotPassCubit.forgotPass(_emailController.text),
                        ),
                        SizedBox(
                          height: 30,
                        ),
                        ButtonWidget(
                            title: R.string.send.tr(),
                            backgroundColor: R.color.primaryColor,
                            onPressed: () =>
                                state is ForgotPassLoading ? null : _forgotPassCubit.forgotPass(_emailController.text)),
                      ],
                    ),
                  ),
                  Visibility(
                    visible: state is ForgotPassLoading,
                    child: PendingAction(),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
