import 'dart:developer';

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

import '../../../../core/routing/routing_manager.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverFillRemaining(
            hasScrollBody: false,
            child: Form(
              key: _formKey,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                //    crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const SizedBox(
                    height: 10,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Obx(() {
                        return ButtonWidget(
                          loaderColor: AppColors.primeColor,
                          isLoading: authController.guestState.loading,
                          onTap: () {
                            authController.guestSignIn(
                              onError: (error) {
                                if (error.toString() == "invalid_credentials") {
                                  Toast.showToast("invalid_credentials".tr);
                                }
                                if (error.toString() == "Unknown Error") {
                                  Toast.showToast("invalid_credentials".tr);
                                }
                                if (error.toString() ==
                                    "User is not verified") {
                                  Toast.showToast(
                                      "you_are_not_verified_yet".tr);
                                  RoutingManager.offAll(
                                      RouteName.verificationCodePage);
                                }
                                if (error.toString() ==
                                    "You Have no Internet Connection") {
                                  Toast.showToast(
                                      "you_have_no_internet_connection".tr);
                                }
                                log(error.toString());
                              },
                            );
                          },
                          title: 'skip'.tr,
                          width: Get.width * 0.35,
                          textColor: AppColors.textButtonColor,
                          color: Colors.transparent,
                        );
                      }),
                    ],
                  ),
                  SizedBox(
                    width: 150,
                    height: 150,
                    child: SvgPicture.asset('assets/icons/tafee icon.svg'),
                  ).center().paddingOnly(top: 20).expanded(1),
                  Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        BoldTextWidget(
                          "sign_up".tr,
                        ).paddingOnly(left: 4),
                        const SizedBox(
                          height: 16,
                        ),
                        TextFieldWidget(
                          onChange: (value) {
                            authController.registerModel.firstName = value;
                          },
                          keyboardType: TextInputType.text,
                          label: "first_name".tr,
                          validate: (value) {
                            if (value == null || value.isEmpty) {
                              return "please_enter_the_first_name".tr;
                            }
                            return null;
                          },
                        ).center().paddingSymmetric(
                              vertical: 4,
                            ),
                        TextFieldWidget(
                          onChange: (value) {
                            authController.registerModel.lastName = value;
                          },
                          keyboardType: TextInputType.text,
                          label: "last_name".tr,
                          validate: (value) {
                            if (value == null || value.isEmpty) {
                              return "please_enter_the_last_name".tr;
                            }
                            return null;
                          },
                        ).center().paddingSymmetric(
                              vertical: 4,
                            ),
                        TextFieldWidget(
                          onChange: (value) {
                            authController.registerModel.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: 4,
                            ),
                        Obx(() {
                          return TextFieldWidget(
                            onChange: (value) {
                              authController.registerModel.password = value;
                            },
                            keyboardType: TextInputType.text,
                            suffix: IconButton(
                              icon: const Icon(
                                Icons.remove_red_eye,
                              ),
                              onPressed: () {
                                authController
                                    .toggleRegisterPassowrdVisibilty();
                              },
                            ),
                            obscure:
                                !authController.registerPassowrdVisible.value,
                            label: "password".tr,
                            validate: (value) {
                              if (value == null || value.isEmpty) {
                                return "please_enter_the_password".tr;
                              }
                              if (value.length < 8) {
                                return "password_is_short".tr;
                              }
                              return null;
                            },
                          ).center().paddingSymmetric(vertical: 4);
                        }),
                        Obx(() {
                          return TextFieldWidget(
                            onChange: (value) {},
                            suffix: IconButton(
                              icon: const Icon(
                                Icons.remove_red_eye,
                              ),
                              onPressed: () {
                                authController
                                    .toggleRegisterConfirmPassowrdVisibilty();
                              },
                            ),
                            obscure: !authController
                                .registerConfirmPassowrdVisible.value,
                            keyboardType: TextInputType.text,
                            label: "confirm_password".tr,
                            validate: (value) {
                              if (authController.registerModel.password !=
                                  value) {
                                return "password_did't_identical".tr;
                              }
                              return null;
                            },
                          ).center().paddingSymmetric(vertical: 4);
                        }),
                        const SizedBox(
                          height: 22,
                        ),
                        Obx(() {
                          return ButtonWidget(
                                  isLoading:
                                      authController.registerModelState.loading,
                                  onTap: () {
                                    if (_formKey.currentState!.validate()) {
                                      authController.register(
                                        onSuccess: (value) {
                                          RoutingManager.to(
                                              RouteName.verificationCodePage);
                                        },
                                        onError: (error) {
                                          if (error.toString() ==
                                              "Unknown Error") {
                                            Toast.showToast(
                                                "email_is_already_exist".tr);
                                            //  RoutingManager.offAll(RouteName.login);
                                          }
                                          if (error.toString() ==
                                              "You Have no Internet Connection") {
                                            Toast.showToast(
                                                "you_have_no_internet_connection"
                                                    .tr);
                                          }
                                        },
                                      );
                                    }
                                  },
                                  title: "sign_up".tr)
                              .paddingOnly(bottom: 10);
                        }),
                        const SizedBox(
                          height: 8,
                        ),
                        Row(
                          children: [
                            RegularTextWidget('you_have_account?'.tr,
                                fontSize: 14),
                            const SizedBox(
                              width: 8,
                            ),
                            BoldTextWidget(
                              "sign_in".tr,
                              decoration: TextDecoration.underline,
                            ).onTap(() {
                              RoutingManager.off(RouteName.login);
                            }),
                          ],
                        ).paddingOnly(
                          left: Responsive.isTablet() ? 12 : 8,
                        )
                      ],
                    )
                        .paddingSymmetric(
                            horizontal: Responsive.isTablet() ? 40 : 20)
                        .expanded(2),
                  ),
                  const SizedBox(
                    height: 60.0,
                  ),
                  SizedBox(height: 0.15 * Get.height, child: const TailAuth()),
                ],
              ).paddingSymmetric(horizontal: Responsive.isTablet() ? 40 : 0),
            ),
          ),
        ],
      ),
    ).makeSafeArea();
  }
}
