import 'package:flutter/material.dart';
import 'package:tour_travel_agr/api/all_api.dart';
import 'package:tour_travel_agr/helper/argument_route/error_arguments.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/modal_dialog.dart';
import 'package:tour_travel_agr/helper/widget_responsive.dart';
import 'package:tour_travel_agr/resource/assets.dart';
import 'package:tour_travel_agr/resource/colors.dart';
import 'package:tour_travel_agr/resource/routes.dart';
import 'package:tour_travel_agr/resource/size.dart';
import 'package:tour_travel_agr/resource/style.dart';

class ResetPasswordView extends StatefulWidget {
  const ResetPasswordView({super.key, required this.idPath});

  final String idPath;

  @override
  State<ResetPasswordView> createState() => _ResetPasswordViewState();
}

class _ResetPasswordViewState extends State<ResetPasswordView> {
  final TextEditingController passwordController = TextEditingController();
  final TextEditingController newPasswordController = TextEditingController();

  bool buttonActive = false;
  bool idIsConfirmed = false;

  @override
  void initState() {
    checkIdPath();
    super.initState();
  }

  void checkIdPath() {
    Api.dataForgotPassword(widget.idPath).then((apiResponse) {
      if (apiResponse.error) {
        Navigator.pushNamedAndRemoveUntil(
          context,
          Routes.errorRoute,
          (route) => false,
          arguments: ErrorWidgetArguments(
              errorMessage: 'Link yang anda masukkan tidak valid'),
        );
      } else {
        setState(() {
          idIsConfirmed = true;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorManager.backgroundColor,
      body: ScreenResponsive(
        widget: (idIsConfirmed)
            ? bodyWidget(context)
            : Center(
                child: CircularProgressIndicator(
                  color: ColorManager.primary,
                ),
              ),
        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: "Reset Password",
            canGoback: false,
          ),
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(
                      top: AppMargin.m10,
                    ),
                    child: Center(
                      child: Image(
                        width: 216,
                        height: 216,
                        image: AssetImage(
                          Assets.resetPassword,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(
                      top: AppMargin.m25,
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "Reset",
                          style: getSemiBoldStyle(
                            color: Colors.black,
                            fontSize: 32,
                          ),
                        ),
                        Text(
                          "Password",
                          style: getSemiBoldStyle(
                            color: Colors.black,
                            fontSize: 32,
                          ),
                        )
                      ],
                    ),
                  ),
                  PasswordInput(
                    passwordController: passwordController,
                    labelText: "New Password",
                    marginTop: AppMargin.m20,
                    onChanged: (val) {
                      if (passwordController.text.isNotEmpty &&
                          newPasswordController.text.isNotEmpty) {
                        setState(() {
                          buttonActive = true;
                        });
                      } else {
                        setState(() {
                          buttonActive = false;
                        });
                      }
                    },
                  ),
                  PasswordInput(
                    passwordController: newPasswordController,
                    labelText: "Confirm New Password",
                    marginTop: AppMargin.m20,
                    onChanged: (val) {
                      if (passwordController.text.isNotEmpty &&
                          newPasswordController.text.isNotEmpty) {
                        setState(() {
                          buttonActive = true;
                        });
                      } else {
                        setState(() {
                          buttonActive = false;
                        });
                      }
                    },
                  ),
                  SizedBox(
                    height: AppMargin.m20,
                  ),
                  CustomButton(
                    text: "Submit",
                    colorButton: (!buttonActive) ? Colors.grey : null,
                    onTap: () {
                      if (buttonActive) {
                        Api.resetPassword(
                          widget.idPath,
                          passwordController.text,
                          newPasswordController.text,
                        ).then(
                          (apiResponse) {
                            if (apiResponse.error) {
                              modalDialogGlobal(
                                context: context,
                                title: "Gagal",
                                contentBody: apiResponse.msg,
                                buttonText: 'Ok',
                                tapButton: () => Navigator.pop(context),
                              );
                              return;
                            }

                            Navigator.pushNamedAndRemoveUntil(
                              context,
                              Routes.resetPasswordSuccess,
                              (route) => false,
                            );
                          },
                        );
                      }
                    },
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}
