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/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';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: 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().expanded(3),
          Form(
            key: _formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                BoldTextWidget("sign_in".tr),
                TextFieldWidget(
                  onChange: (value) {
                    authController.loginModel.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),
                Obx(() {
                  return TextFieldWidget(
                    onChange: (value) {
                      authController.loginModel.password = value;
                    },
                    suffix: IconButton(
                      icon: const Icon(
                        Icons.remove_red_eye,
                      ),
                      onPressed: () {
                        authController.toggleLoginPasswordVisibility();
                      },
                    ),
                    obscure: !authController.loginPasswordVisible.value,
                    keyboardType: TextInputType.emailAddress,
                    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();
                }),
                Row(
                  children: [
                    RegularTextWidget("${'forgot_password?'.tr}  ",
                        fontSize: 14),
                    BoldTextWidget("reset_password".tr).onTap(() {
                      RoutingManager.to(RouteName.forgotPassword);
                    }),
                  ],
                ),
                Obx(() {
                  return ButtonWidget(
                          isLoading: authController.loginState.loading,
                          onTap: () {
                            if (_formKey.currentState!.validate()) {
                              authController.login(
                                onSuccess: (value) {
                                  RoutingManager.offAll(RouteName.superHome);
                                },
                                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: "sign_in".tr)
                      .paddingOnly(top: 30, bottom: 10);
                }),
                Row(
                  children: [
                    RegularTextWidget(
                      "${'you_don\'t_have_account?'.tr} ",
                      fontSize: 14,
                    ),
                    BoldTextWidget("sign_up".tr).onTap(() {
                      RoutingManager.off(RouteName.register);
                    }),
                  ],
                )
              ],
            ).paddingSymmetric(horizontal: Responsive.isTablet() ? 40 : 20),
          ).expanded(6),
          const TailAuth().expanded(2),
        ],
      ),
    ).makeSafeArea();
  }
}
