import 'package:farm_tpf/data/repository/user_repository.dart';
import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
import 'package:farm_tpf/presentation/custom_widgets/button_widget.dart';
import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
import 'package:farm_tpf/utils/const_color.dart';
import 'package:farm_tpf/utils/validators.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_dismisser/keyboard_dismisser.dart';

class ForgotPasswordScreen extends StatefulWidget {
  @override
  _ForgotPasswordScreenState createState() => _ForgotPasswordScreenState();
}

class _ForgotPasswordScreenState extends State<ForgotPasswordScreen> {
  UserRepository _userRepository = UserRepository();
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  GlobalKey<FormState> _formKey = GlobalKey();
  bool _autoValidate = false;
  TextEditingController _emailController = TextEditingController();
  String _email = "";

  @override
  void initState() {
    super.initState();
  }

  _validateInputs() async {
    if (_formKey.currentState!.validate()) {
      _formKey.currentState!.save();
      LoadingDialog.showLoadingDialog(context);
      _userRepository.forgotPassword(_email).then((value) {
        LoadingDialog.hideLoadingDialog(context);
        Navigator.pop(context);
        Utils.showSnackBarSuccess(message: "Gửi email thành công");
      }).catchError((error) {
        LoadingDialog.hideLoadingDialog(context);
        Utils.showSnackBarError(message: 'Email không tồn tại');
      });
    } else {
      _autoValidate = true;
    }
  }

  Widget _emailField() {
    return TextFormField(
      keyboardType: TextInputType.text,
      decoration: InputDecoration(labelText: "Email"),
      controller: _emailController,
      validator: (String? value) {
        return Validators.validateEmail(value ?? '');
      },
      onSaved: (newValue) {
        _email = newValue ?? '';
      },
    );
  }

  Widget _btnSubmit() {
    return ButtonWidget(
      title: 'Gửi',
      onPressed: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus) {
          currentFocus.unfocus();
        }
        _validateInputs();
      },
    );
  }

  @override
  Widget build(BuildContext context) => KeyboardDismisser(
      child: Scaffold(
          backgroundColor: Colors.white,
          key: _scaffoldKey,
          appBar: AppBarWidget(),
          body: Form(
              key: _formKey,
              child: SingleChildScrollView(
                padding: EdgeInsets.all(8.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SizedBox(
                      height: 14,
                    ),
                    Text(
                      'Quên mật khẩu',
                      style: TextStyle(fontSize: 26, fontWeight: FontWeight.w300, color: Colors.black),
                    ),
                    SizedBox(
                      height: 8,
                    ),
                    Text('Nhập email đăng ký mật khẩu vào ô bên dưới. Hệ thống sẽ gửi bạn một liên kết đặt lại mật khẩu qua email.',
                        style: TextStyle(fontSize: 15, fontWeight: FontWeight.w200, color: Colors.black54)),
                    _emailField(),
                    SizedBox(
                      height: 16.0,
                    ),
                    _btnSubmit()
                  ],
                ),
              ))));
}
