import 'package:flutter/material.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/core/routing/routing_manager.dart';
import 'package:taafee_mobile/features/account/business_logic_layer/account_controller.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/widgets/button.dart';
import '../../../../common/widgets/header_screen.dart';
import '../../../../common/widgets/loader.dart';
import '../../../../common/widgets/textfiled.dart';
import '../../../../common/widgets/toast.dart';

// ignore: must_be_immutable
class ChangePassword extends StatelessWidget {
  ChangePassword({super.key});
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final AccountController accountController = Get.find<AccountController>();
  final HomeController homeController = Get.find<HomeController>();
  final AuthController authController = Get.find<AuthController>();
  String newPassword = '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Obx(() {
        if (accountController.changePasswordState.loading) {
          return Loader(
            height: Get.height,
            width: Get.width,
          ).center();
        } else {
          return SingleChildScrollView(
              child: Form(
            key: formKey,
            child: Column(
              children: [
                HeaderScreen('change_password'.tr).paddingOnly(
                  top: 30,
                  left: 20,
                ),
                Column(
                  children: [
                    SizedBox(
                      height: Get.height * 0.18,
                    ),
                    Obx(() {
                      return TextFieldWidget(
                          onChange: (value) {
                            accountController.changePasswordModel.oldPassword =
                                value;
                          },
                          suffix: IconButton(
                            icon: const Icon(
                              Icons.remove_red_eye,
                            ),
                            onPressed: () {
                              authController
                                  .toggleChangePasswordOldPasswordVisibility();
                            },
                          ),
                          obscure: !authController
                              .changePasswordOldPasswordVisible.value,
                          keyboardType: TextInputType.name,
                          label: 'old_password'.tr,
                          validate: (value) {
                            if (value == '' || value == null) {
                              return "please_enter_the_password".tr;
                            }
                            if (value.length < 8) {
                              return 'password_is_short';
                            }
                            return null;
                          });
                    }),
                    const SizedBox(
                      height: 12,
                    ),
                    Obx(() {
                      return TextFieldWidget(
                          onChange: (value) {
                            newPassword = value;
                          },
                          suffix: IconButton(
                            icon: const Icon(
                              Icons.remove_red_eye,
                            ),
                            onPressed: () {
                              authController
                                  .toggleChangePasswordNewPasswordVisibility();
                            },
                          ),
                          obscure: !authController
                              .changePasswordNewPasswordVisible.value,
                          keyboardType: TextInputType.name,
                          label: 'new_password'.tr,
                          validate: (value) {
                            if (value == '' || value == null) {
                              return "please_enter_the_password".tr;
                            }
                            if (value.length < 8) {
                              return 'password_is_short'.tr;
                            }
                            return null;
                          });
                    }),
                    const SizedBox(
                      height: 12,
                    ),
                    Obx(() {
                      return TextFieldWidget(
                          onChange: (value) {
                            accountController.changePasswordModel.newPassword =
                                value;
                          },
                          suffix: IconButton(
                            icon: const Icon(
                              Icons.remove_red_eye,
                            ),
                            onPressed: () {
                              authController
                                  .toggleChangePasswordConfirmPasswordVisibility();
                            },
                          ),
                          obscure: !authController
                              .changePasswordConfirmPasswordVisible.value,
                          keyboardType: TextInputType.name,
                          label: 'confirm_new_password'.tr,
                          validate: (value) {
                            if (value != newPassword ||
                                value == '' ||
                                value == null) {
                              return "password_did't_identical".tr;
                            }
                            if (value.length < 8) {
                              return 'password_is_short'.tr;
                            }
                            return null;
                          });
                    }),
                  ],
                )
                    .paddingOnly(
                      top: 8.0,
                      left: 16,
                      right: 16,
                    )
                    .paddingSymmetric(
                        horizontal: Responsive.isTablet() ? 40 : 0),
                SizedBox(
                  height: Get.height * 0.11,
                ),
                ButtonWidget(
                        onTap: () {
                          if (formKey.currentState!.validate()) {
                            accountController.changePassword(
                              onSuccess: () {
                                RoutingManager.back();
                              },
                              onError: (e) {
                                Toast.showToast(e.toString());
                              },
                            );
                          }
                        },
                        title: 'save_changes'.tr)
                    .paddingSymmetric(
                        horizontal: Responsive.isTablet() ? 40 : 0),
                SizedBox(
                  height: Get.height * 0.1,
                ),
              ],
            ).paddingOnly(
              top: Responsive.isTablet() ? 30 : 0,
            ),
          ).paddingOnly(
            right: (homeController.isArabic.value) ? 16 : 0,
          ));
        }
      }),
    ).makeSafeArea();
  }
}
