import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/utils/size_config.dart';
import 'package:tm_app/views/forget_password_create/strings/forgot_password_create_strings.dart';

import '../../../core/constants/assets.dart';
import '../../../core/routes/app_routes.dart';
import '../../../core/theme/colors.dart';
import '../../../core/utils/app_toast.dart';
import '../../../view_models/auth_view_model.dart';
import '../../login/widgets/login_textfield.dart';
import '../../shared/base_button.dart';

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

  @override
  State<TabletForgotPasswordCreatePage> createState() =>
      _TabletForgotPasswordCreatePageState();
}

class _TabletForgotPasswordCreatePageState
    extends State<TabletForgotPasswordCreatePage> {
  late final AuthViewModel viewModel;

  @override
  void initState() {
    viewModel = context.read<AuthViewModel>();
    viewModel.addListener(_viewModelListener);
    super.initState();
  }

  @override
  void dispose() {
    viewModel.removeListener(_viewModelListener);
    super.dispose();
  }

  void _viewModelListener() {
    if (viewModel.resetPasswordErrorMessage != null) {
      AppToast.error(context, viewModel.resetPasswordErrorMessage!);
    } else if (viewModel.isResetPasswordLoading == false &&
        viewModel.resetPasswordSuccess) {
      Router.neglect(context, () => const LoginScreenRoute().go(context));
      viewModel.clearResetFlow();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      body: Center(
        child: SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 24.0.w()),
          child: Consumer<AuthViewModel>(
            builder: (context, viewModel, _) {
              return Center(
                child: SizedBox(
                  width: 364,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 22.0.w()),
                        child: SvgPicture.asset(
                          AssetsManager.logoBig,
                          width: double.infinity,
                          height: 50.0.h(),
                        ),
                      ),
                      SizedBox(height: 72.0.h()),
                      Text(
                        ForgotPasswordCreateStrings.forgotPasswordTitle,
                        style: TextStyle(
                          fontSize: 28.0.sp(),
                          fontWeight: FontWeight.bold,
                          color: AppColors.grey80,
                        ),
                      ),
                      SizedBox(height: 16.0.h()),
                      Text(
                        ForgotPasswordCreateStrings.forgotPasswordSubtitle,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontSize: 14.0.sp(),
                          fontWeight: FontWeight.w400,
                          color: AppColors.grey60,
                        ),
                      ),
                      SizedBox(height: 40.0.h()),
                      LoginTextField(
                        controller: viewModel.newPasswordController,
                        focusNode: viewModel.newPasswordFocus,
                        label: ForgotPasswordCreateStrings.createNewPassword,
                        iconPath: AssetsManager.passwordIcon,
                        isPassword: true,
                        obscureText: viewModel.obscureForgotPassword,
                        onToggleVisibility:
                            viewModel.toggleForgotPasswordVisibility,
                      ),
                      SizedBox(height: 16.0.h()),
                      Container(
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.only(left: 16.0.w()),
                        child: Text(
                          ForgotPasswordCreateStrings.atLast5Character,
                          style: TextStyle(
                            fontSize: 14.0.sp(),
                            color: AppColors.grey60,
                          ),
                        ),
                      ),
                      SizedBox(height: 6.0.h()),
                      Container(
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.only(left: 16.0.w()),
                        child: Text(
                          ForgotPasswordCreateStrings.atLeast1uppercase,
                          style: TextStyle(
                            fontSize: 14.0.sp(),
                            color: AppColors.grey60,
                          ),
                        ),
                      ),
                      SizedBox(height: 6.0.h()),
                      Container(
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.only(left: 16.0.w()),
                        child: Text(
                          ForgotPasswordCreateStrings.atLeast1lowercase,
                          style: TextStyle(
                            fontSize: 14.0.sp(),
                            color: AppColors.grey60,
                          ),
                        ),
                      ),
                      SizedBox(height: 6.0.h()),
                      Container(
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.only(left: 16.0.w()),
                        child: Text(
                          ForgotPasswordCreateStrings.atLeastSpecial,
                          style: TextStyle(
                            fontSize: 14.0.sp(),
                            color: AppColors.grey60,
                          ),
                        ),
                      ),
                      SizedBox(height: 80.0.h()),
                      viewModel.isResetPasswordLoading
                          ? BaseButton(
                            isEnabled: viewModel.isNewPasswordValid,
                            text: ForgotPasswordCreateStrings.resetLink,
                            onPressed: null,
                            isLoading: true,
                          )
                          : BaseButton(
                            isEnabled: viewModel.isNewPasswordValid,
                            text: ForgotPasswordCreateStrings.resetLink,

                            onPressed:
                                viewModel.isNewPasswordValid
                                    ? () async {
                                      viewModel.resetPassword();
                                    }
                                    : null,
                          ),

                      TextButton(
                        onPressed: () {
                          context.pop();
                        },
                        child: Text(
                          ForgotPasswordCreateStrings.backToSignInButton,
                          style: TextStyle(
                            fontSize: 14.0.sp(),
                            fontWeight: FontWeight.w500,
                            color: AppColors.mainBlue,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
