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/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/p_input.dart';
import 'package:taafee_mobile/features/auth/presentation_layer/widgets/tail_auth.dart';

class VerificationCodeScreen extends StatelessWidget {
  final AuthController authController = Get.find<AuthController>();
  VerificationCodeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //  resizeToAvoidBottomInset: false,
      body: SingleChildScrollView(
        child: SizedBox(
          height: Get.height,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            //    crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              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) {
                      //   if (value.length == 6) {
                      authController.verify(
                        onSuccess: (value) {
                          RoutingManager.offAll(RouteName.superHome);
                        },
                        onError: (error) {
                          if (error.toString() == "Unknown Error") {
                            Toast.showToast("the_verify_code_is_invalid".tr);
                          }
                          if (error.toString() ==
                              "You Have no Internet Connection") {
                            Toast.showToast(
                                "you_have_no_internet_connection".tr);
                          }
                        },
                      );
                      // }
                    }, onChanged: (value) {
                      authController.verificationCodeModel.code = value;
                    }).paddingSymmetric(vertical: 20),
                    Obx(() {
                      return ButtonWidget(
                        isLoading: authController.verifyState.loading,
                        onTap: () {
                          if (authController
                              .verificationCodeModel.code.isEmpty) {
                            Toast.showToast("please_enter_the_code".tr);
                          } else {
                            authController.verify(
                              onSuccess: (value) {
                                RoutingManager.offAll(RouteName.superHome);
                              },
                              onError: (error) {
                                if (error.toString() == "Unknown Error") {
                                  Toast.showToast(
                                      "the_verify_code_is_invalid".tr);
                                }
                                if (error.toString() ==
                                    "You Have no Internet Connection") {
                                  Toast.showToast(
                                      "you_have_no_internet_connection");
                                }
                              },
                            );
                          }
                        },
                        title: "sign_up".tr,
                      ).paddingSymmetric(vertical: 30);
                    }),
                    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.verificationCodeModel.email);
                            });
                    })
                  ],
                ).paddingSymmetric(horizontal: 20),
              ).expanded(5),
              const TailAuth().paddingSymmetric(horizontal: 20).expanded(2),
            ],
          ).paddingSymmetric(horizontal: Responsive.isTablet() ? 40 : 0),
        ),
      ),
    ).makeSafeArea();
  }
}
