import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/core/routing/routing_manager.dart';
import 'package:taafee_mobile/features/auth/business_logic_layer/auth_controller.dart';

import '../../../../common/const/const.dart';
import '../../../../common/widgets/button.dart';
import '../../../../common/widgets/text.dart';
import '../../../../common/widgets/textfiled.dart';
import '../../../../common/widgets/toast.dart';
import '../widgets/tail_auth.dart';

// ignore: must_be_immutable
class ResetPasswordScreen extends StatelessWidget {
  AuthController authController = Get.find<AuthController>();
  final _formKey = GlobalKey<FormState>();
  ResetPasswordScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        //    crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(
            width: 150,
            height: 150,
            child: SvgPicture.asset('assets/icons/tafee icon.svg'),
          ).center().expanded(3),
          Form(
            key: _formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                BoldTextWidget("reset_password".tr),
                Obx(() {
                  return TextFieldWidget(
                    onChange: (value) {
                      authController.forgotPasswordModel.password = value;
                    },
                    suffix: IconButton(
                      icon: const Icon(
                        Icons.remove_red_eye,
                      ),
                      onPressed: () {
                        authController
                            .toggleResetPasswordNewPasswordVisibility();
                      },
                    ),
                    obscure:
                        !authController.resetPasswordNewPasswordVisible.value,
                    keyboardType: TextInputType.emailAddress,
                    label: "new_password".tr,
                    validate: (value) {
                      if (value == null || value.isEmpty) {
                        return "please_enter_the_password".tr;
                      }
                      if (value.length < 8) {
                        return "The_password_is_short".tr;
                      }
                      return null;
                    },
                  ).center().paddingSymmetric(vertical: 20);
                }),
                Obx(() {
                  return TextFieldWidget(
                    onChange: (value) {},
                    suffix: IconButton(
                      icon: const Icon(
                        Icons.remove_red_eye,
                      ),
                      onPressed: () {
                        authController
                            .toggleResetPasswordConfirmPasswordVisibility();
                      },
                    ),
                    obscure: !authController
                        .resetPasswordConfirmPasswordVisible.value,
                    keyboardType: TextInputType.emailAddress,
                    label: "confirm_password".tr,
                    validate: (value) {
                      if (value !=
                          authController.forgotPasswordModel.password) {
                        return "password_did't_identical".tr;
                      }
                      return null;
                    },
                  ).center();
                }),
                Obx(() {
                  return ButtonWidget(
                          isLoading: authController.changePasswordState.loading,
                          onTap: () {
                            if (_formKey.currentState!.validate()) {
                              authController.changePassword(
                                onSuccess: (p0) {
                                  Toast.showToast(
                                      "password_changed_successfully".tr);
                                  RoutingManager.offAll(RouteName.superHome);
                                },
                                onError: (error) {
                                  if (error.toString() ==
                                      "You Have no Internet Connection") {
                                    Toast.showToast(
                                        "you_have_no_internet_connection".tr);
                                  }
                                },
                              );
                            }
                          },
                          title: "next".tr)
                      .paddingSymmetric(vertical: 40);
                }),
              ],
            ).paddingSymmetric(horizontal: 20),
          ).expanded(5),
          const TailAuth().paddingSymmetric(horizontal: 20).expanded(2),
        ],
      ).paddingSymmetric(horizontal: Responsive.isTablet() ? 40 : 0),
    );
  }
}
