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 'package:taafee_mobile/features/home/business_logic_layer/home_controller.dart';

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

class VerificationCodeResetPasswordScreen extends StatelessWidget {
  final AuthController authController = Get.find<AuthController>();
  VerificationCodeResetPasswordScreen({super.key});
  final HomeController homeController = Get.find<HomeController>();
  @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),
          Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                BoldTextWidget("verification_code".tr),
                PinPutWidget(onCompleted: (value) {
                  authController.verifyForgotPassword(
                    onSuccess: (p0) {
                      RoutingManager.to(RouteName.resetPassword);
                    },
                    onError: (error) {
                      if (error.toString() == "Unknown Error") {
                        Toast.showToast("the_verify_code_is_invalid");
                      }
                      if (error.toString() ==
                          "You Have no Internet Connection") {
                        Toast.showToast('you_have_no_internet_connection'.tr);
                      }
                    },
                  );
                }, onChanged: (value) {
                  authController.forgotPasswordModel.code = value;
                }).paddingSymmetric(vertical: 30),
                Obx(() {
                  return ButtonWidget(
                          isLoading:
                              authController.verifyForgotPasswordState.loading,
                          onTap: () {
                            if (authController
                                .forgotPasswordModel.code.isEmpty) {
                              Toast.showToast("please_enter_the_code".tr);
                            } else {
                              authController.verifyForgotPassword(
                                onSuccess: (p0) {
                                  RoutingManager.to(RouteName.resetPassword);
                                },
                                onError: (error) {
                                  if (error.toString() ==
                                      "invalid_reset_code") {
                                    Toast.showToast(
                                        "the_verify_code_is_invalid".tr);
                                  }
                                  if (error.toString() ==
                                      "You Have no Internet Connection") {
                                    Toast.showToast(
                                        'you_have_no_internet_connection'.tr);
                                  }
                                },
                              );
                            }
                          },
                          title: "reset_password".tr)
                      .paddingSymmetric(vertical: 40);
                }),
                Obx(() {
                  return authController.resendCodeState.loading
                      ? Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            RegularTextWidget(
                              "resend_code".tr,
                              fontSize: 14,
                            ).center(),
                            const SizedBox(
                              width: 12,
                            ),
                            SizedBox(
                                width: 20,
                                height: 20,
                                child:
                                    const CircularProgressIndicator().center()),
                          ],
                        )
                      : RegularTextWidget(
                          "resend_code".tr,
                          fontSize: 14,
                        ).center().onTap(() {
                          authController.resendCode(
                              authController.forgotPasswordModel.email);
                        });
                })
              ],
            ).paddingSymmetric(horizontal: 20),
          ).expanded(5),
          const TailAuth().paddingSymmetric(horizontal: 20).expanded(2),
        ],
      )
          .paddingSymmetric(horizontal: Responsive.isTablet() ? 40 : 0)
          .makeSafeArea(),
    );
  }
}
