import 'package:driverapp/app-icons/driver_app_icons.dart';
import 'package:driverapp/core/viewModels/authentication_view_model.dart';
import 'package:driverapp/core/viewModels/project_view_model.dart';
import 'package:driverapp/root_page.dart';
import 'package:driverapp/uitl/app_toast.dart';
import 'package:driverapp/uitl/translations_delegate_base.dart';
import 'package:driverapp/uitl/utils.dart';
import 'package:driverapp/widgets/buttons/secondary_button.dart';
import 'package:driverapp/widgets/input/text_field.dart';
import 'package:driverapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';

// ignore: must_be_immutable
class RestPasswordPage extends StatelessWidget {
  final forgetPasswordFormKey = GlobalKey<FormState>();

  // String  password;
  // String  confirmedPassword;
  ProjectViewModel projectViewModel;
  AuthenticationViewModel authenticationViewModel;
  TextEditingController password = TextEditingController(text: "");
  TextEditingController confirmedPassword = TextEditingController(text: "");

  changePassword(BuildContext context) async {
    if (forgetPasswordFormKey.currentState.validate()) {
      forgetPasswordFormKey.currentState.save();
      await authenticationViewModel.changePassword(
          password.text, confirmedPassword.text);
      if (authenticationViewModel.isError) {
        Utils.showErrorToast(authenticationViewModel.error);
      } else {
        AppToast.showSuccessToast(
            message: "Your Password changed successfully");
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => RootPage()),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    authenticationViewModel = Provider.of(context);

    return AnimatedSwitcher(
      duration: Duration(microseconds: 350),
      child: AppScaffold(
        isShowAppBar: false,
        body: SingleChildScrollView(
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                FractionallySizedBox(
                  widthFactor: 0.80,
                  child: Column(
                    children: <Widget>[
                      SizedBox(
                        height: MediaQuery.of(context).size.height * 0.25,
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Container(
                            child: Icon(
                              DriverApp.qustion_mark,
                              size: 150,
                              color: Theme.of(context).primaryColor,
                            ),
                            margin: EdgeInsets.only(
                              top: 20,
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 20,
                      ),
                      Column(
                        children: <Widget>[
                          Center(
                            child: Text(
                              "Rest Password",
                              style: TextStyle(
                                  fontSize: 25,
                                  letterSpacing: 1,
                                  fontWeight: FontWeight.w600),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 30,
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Form(
                        key: forgetPasswordFormKey,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Container(
                              child: TextFields(
                                hintText: "Password",
                                controller: password,
                                keyboardType: TextInputType.number,
                                validator: (value) {
                                  if (value.isEmpty) {
                                    return "Please enter your password";
                                  }
                                  return null;
                                },
                                onSaved: (value) {
                                  // password = value;
                                },
                              ),
                            ),
                            SizedBox(
                              height: 20,
                            ),
                            Container(
                              child: TextFields(
                                controller: confirmedPassword,
                                hintText: "Confirm Password",
                                keyboardType: TextInputType.text,
                                validator: (value) {
                                  if (value.isEmpty) {
                                    return "Please enter your confirm password";
                                  }
                                  if (confirmedPassword.text != password.text) {
                                    return "Password your doesn't match your confirm password";
                                  }
                                  return null;
                                },
                                onSaved: (value) {
                                  // confirmedPassword = value;
                                },
                              ),
                            ),
                            SizedBox(
                              height: 20,
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 20,
                ),
                SizedBox(
                  height: 10,
                ),
                Container(
                    margin: EdgeInsets.all(10),
                    height: MediaQuery.of(context).size.height * 0.22,
                    child: Column(
                      children: <Widget>[
                        SecondaryButton(
                          label: "Change Password",
                          onTap: () async {
                            await changePassword(context);
                          },
                          disabled: authenticationViewModel.isLoading,
                          loading: authenticationViewModel.isLoading,
                        ),
                        SizedBox(
                          height: 30,
                        ),
                        SecondaryButton(
                          label: TranslationBase.of(context).cancel,
                          onTap: () {
                            Navigator.pushReplacement(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => RootPage()),
                            );
                          },
                          color: Color(0xffE9F1F1),
                          borderColor: Colors.black54,
                          textColor: Theme.of(context).primaryColor,
                        ),
                      ],
                    ))
              ],
            ),
          ),
        ),
      ),
    );
  }
}
