// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:tour_travel_agr/api/all_api.dart';
import 'package:tour_travel_agr/helper/components_widget/custom_appbar.dart';
import 'package:tour_travel_agr/helper/components_widget/password_input.dart';
import 'package:tour_travel_agr/helper/components_widget/widget_button.dart';
import 'package:tour_travel_agr/helper/function/session_check.dart';
import 'package:tour_travel_agr/helper/modal_dialog.dart';
import 'package:tour_travel_agr/helper/prefs.dart';
import 'package:tour_travel_agr/helper/widget_responsive.dart';
import 'package:tour_travel_agr/resource/colors.dart';
import 'package:tour_travel_agr/resource/font.dart';
import 'package:tour_travel_agr/resource/routes.dart';
import 'package:tour_travel_agr/resource/size.dart';
import 'package:tour_travel_agr/resource/strings.dart';
import 'package:tour_travel_agr/resource/style.dart';

class ChangePasswordView extends StatefulWidget {
  const ChangePasswordView({super.key});

  @override
  State<ChangePasswordView> createState() => _ChangePasswordViewState();
}

class _ChangePasswordViewState extends State<ChangePasswordView> {
  final TextEditingController nowPasswordController = TextEditingController();

  final TextEditingController newPasswordController = TextEditingController();

  final TextEditingController confirmNewPasswordController =
      TextEditingController();

  bool buttonActive = false;
  @override
  void initState() {
    checkSession().then((isLogin) {
      if (isLogin == false) {
        Navigator.pushNamedAndRemoveUntil(
          context,
          Routes.loginRoute,
          (route) => false,
        );
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorManager.backgroundColor,
      body: ScreenResponsive(
        widget: bodyWidget(context),
        widthScreen: MediaQuery.of(context).size.width,
      ),
    );
  }

  Container bodyWidget(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        top: AppPadding.safeAreaTop(context),
        left: AppPadding.p20,
        right: AppPadding.p20,
        bottom: AppPadding.safeAreaBot(context),
      ),
      child: Column(
        children: [
          const CustomAppBar(
            text: "Change Password",
          ),
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: AppMargin.m20,
                  ),
                  PasswordInput(
                    passwordController: nowPasswordController,
                    labelText: "Password Saat Ini",
                    borderSideActive: true,
                    labelColor: ColorManager.primary,
                    onChanged: (val) {
                      if (nowPasswordController.text.isNotEmpty &&
                          newPasswordController.text.isNotEmpty &&
                          confirmNewPasswordController.text.isNotEmpty) {
                        setState(() {
                          buttonActive = true;
                        });
                      } else {
                        buttonActive = false;
                      }
                    },
                  ),
                  Container(
                    margin: EdgeInsets.only(
                      top: AppMargin.m20,
                    ),
                    child: Text(
                      "Ubah password anda",
                      style: getRegularStyle(
                        color: Colors.black,
                        fontSize: 15,
                        fontFamily: FontConstants.mulish,
                      ),
                    ),
                  ),
                  SizedBox(
                    height: AppMargin.m12,
                  ),
                  PasswordInput(
                    passwordController: newPasswordController,
                    labelText: "Password Baru",
                    borderSideActive: true,
                    labelColor: ColorManager.primary,
                    onChanged: (val) {
                      if (nowPasswordController.text.isNotEmpty &&
                          newPasswordController.text.isNotEmpty &&
                          confirmNewPasswordController.text.isNotEmpty) {
                        setState(() {
                          buttonActive = true;
                        });
                      } else {
                        buttonActive = false;
                      }
                    },
                  ),
                  PasswordInput(
                    marginTop: AppMargin.m12,
                    passwordController: confirmNewPasswordController,
                    labelText: "Konfirmasi Password Baru",
                    borderSideActive: true,
                    labelColor: ColorManager.primary,
                    onChanged: (val) {
                      if (nowPasswordController.text.isNotEmpty &&
                          newPasswordController.text.isNotEmpty &&
                          confirmNewPasswordController.text.isNotEmpty) {
                        setState(() {
                          buttonActive = true;
                        });
                      } else {
                        setState(() {
                          buttonActive = false;
                        });
                      }
                    },
                  ),
                  Container(
                    margin: const EdgeInsets.only(
                      top: 50,
                    ),
                    child: CustomButton(
                      text: "Update",
                      colorButton: (buttonActive) ? null : Colors.grey,
                      onTap: () async {
                        if (buttonActive) {
                          if (newPasswordController.text !=
                              confirmNewPasswordController.text) {
                            modalDialogGlobal(
                              context: context,
                              title: "Informasi",
                              contentBody: "Konfirmasi password tidak cocok",
                              buttonText: "Ok",
                              tapButton: () => Navigator.pop(context),
                            );
                          }
                          await EasyLoading.show(
                            status: Strings.pleaseWait,
                            maskType: EasyLoadingMaskType.none,
                          );
                          Api.changePassword(
                                  nowPasswordController.text,
                                  newPasswordController.text,
                                  confirmNewPasswordController.text)
                              .then(
                            (apiresponse) {
                              EasyLoading.dismiss();
                              if (apiresponse.error) {
                                if (apiresponse.data['code'] ==
                                    "WRONG_SESSION_ID") {
                                  modalDialogGlobal(
                                    context: context,
                                    title: "Session Expired",
                                    contentBody: Strings.sessionExpired,
                                    buttonText: "OK",
                                    tapButton: () {
                                      removeSessionId();
                                      Navigator.pushNamedAndRemoveUntil(
                                        context,
                                        Routes.loginRoute,
                                        (route) => false,
                                      );
                                    },
                                  );
                                } else {
                                  modalDialogGlobal(
                                    context: context,
                                    title: "Gagal",
                                    contentBody: apiresponse.msg,
                                    buttonText: "OK",
                                    tapButton: () => Navigator.pop(context),
                                  );
                                }
                              } else {
                                modalDialogGlobal(
                                  context: context,
                                  title: "Sukses",
                                  contentBody: apiresponse.msg,
                                  buttonText: "Ok",
                                  tapButton: () {
                                    Navigator.pop(context);
                                    Navigator.pop(context);
                                  },
                                );
                              }
                            },
                          );
                        }
                      },
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}
