import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:taafee_mobile/common/const/const.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/common/widgets/button.dart';
import 'package:taafee_mobile/common/widgets/text.dart';
import 'package:taafee_mobile/common/widgets/textfiled.dart';
import 'package:taafee_mobile/common/widgets/toast.dart';
import 'package:taafee_mobile/core/routing/routing_manager.dart';
import 'package:taafee_mobile/features/auth/business_logic_layer/auth_controller.dart';
import 'package:taafee_mobile/features/auth/presentation_layer/widgets/tail_auth.dart';
import 'package:taafee_mobile/features/home/business_logic_layer/home_controller.dart';

class ForgotPasswordScreen extends StatelessWidget {
  final _formKey = GlobalKey<FormState>();
  final AuthController authController = Get.find<AuthController>();
  final HomeController homeController = Get.find<HomeController>();
  ForgotPasswordScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        //    crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Obx(
            () => !homeController.isArabic.value
                ? SvgPicture.asset('assets/icons/arrow-left.svg')
                : SvgPicture.asset('assets/icons/arrow right.svg'),
          ).onTap(() {
            RoutingManager.back();
          }).paddingOnly(top: 16.0, left: 16.0, right: 16.0),
          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("forgot_password".tr),
                TextFieldWidget(
                  onChange: (value) {
                    authController.forgotPasswordModel.email = value;
                  },
                  keyboardType: TextInputType.emailAddress,
                  label: "email".tr,
                  validate: (value) {
                    if (value == null || value.isEmpty) {
                      return "please_enter_the_email".tr;
                    }
                    return null;
                  },
                ).center().paddingSymmetric(
                      vertical: 20,
                    ),
                RegularTextWidget(
                  'enter_your_email_to_reset_your_password_please_\n_we_will_send_verification_code_to_your_email.'
                      .tr,
                ),
                Obx(() {
                  return ButtonWidget(
                    isLoading: authController.forgotPasswordState.loading,
                    onTap: () {
                      if (_formKey.currentState!.validate()) {
                        authController.forgotPassword(
                          onSuccess: (value) {
                            RoutingManager.to(
                                RouteName.verificationCodeResetPassword);
                          },
                          onError: (error) {
                            if (error.toString() == "Unknown Error") {
                              Toast.showToast(
                                  "the_selected_email_is_invalid".tr);
                            }
                            if (error.toString() ==
                                "You Have no Internet Connection") {
                              Toast.showToast(
                                  "you_have_no_internet_connection".tr);
                            }
                          },
                        );
                      }
                    },
                    title: "next".tr,
                  ).paddingOnly(top: 30, bottom: 10);
                }),
              ],
            ).paddingSymmetric(horizontal: 20),
          ).expanded(6),
          const TailAuth().paddingSymmetric(horizontal: 20).expanded(2),
        ],
      )
          .paddingSymmetric(horizontal: Responsive.isTablet() ? 40 : 0)
          .paddingOnly(top: Responsive.isTablet() ? 20 : 0),
    ).makeSafeArea();
  }
}
