import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/routes/app_routes.dart';
import 'package:tm_app/core/utils/size_config.dart';

import '../../../core/constants/assets.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';
import '../strings/forgot_password_strings.dart';

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

  @override
  State<DesktopForgotPasswordPage> createState() =>
      _DesktopForgotPasswordPageState();
}

class _DesktopForgotPasswordPageState extends State<DesktopForgotPasswordPage> {
  late final AuthViewModel viewModel;

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

  void _viewModelListener() {
    if (viewModel.successMessage != null) {
      AppToast.success(context, viewModel.successMessage!);
      Router.neglect(
        context,
        () => const ForgotPasswordOtpRouteData().pushReplacement(context),
      );
      viewModel.clearMessages();
    }

    if (viewModel.errorMessageForget != null) {
      AppToast.error(context, viewModel.errorMessageForget!);
      viewModel.clearMessages();
    }
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      body: Center(
        child: Consumer<AuthViewModel>(
          builder: (context, viewModel, _) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 110),
                  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(
                          ForgotPasswordStrings.forgotPasswordTitle,
                          style: TextStyle(
                            fontSize: 28.0.sp(),
                            fontWeight: FontWeight.bold,
                            color: AppColors.grey80,
                          ),
                        ),
                        SizedBox(height: 16.0.h()),
                        Text(
                          ForgotPasswordStrings.forgotPasswordSubtitle,
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            fontSize: 14.0.sp(),
                            fontWeight: FontWeight.w400,
                            color: AppColors.grey60,
                          ),
                        ),
                        SizedBox(height: 40.0.h()),
                        LoginTextField(
                          controller: viewModel.emailController,
                          focusNode: viewModel.emailFocus,
                          label: ForgotPasswordStrings.emailHint,
                          isPassword: false,
                        ),
                        SizedBox(height: 70.0.h()),
                        viewModel.isLoadingForgot
                            ? const BaseButton(
                              isEnabled: false,
                              text: ForgotPasswordStrings.resetLink,
                              onPressed: null,
                              isLoading: true,
                            )
                            : BaseButton(
                              isEnabled: viewModel.isEmailValid,
                              text: ForgotPasswordStrings.resetLink,
                              onPressed:
                                  viewModel.isEmailValid
                                      ? () async {
                                        await viewModel.forgotPassword(context);
                                      }
                                      : null,
                            ),
                        const SizedBox(height: 12),
                        TextButton(
                          onPressed: () {
                            Navigator.pop(context);
                          },
                          child: Text(
                            ForgotPasswordStrings.backToSignInButton,
                            style: TextStyle(
                              fontSize: 14.0.sp(),
                              fontWeight: FontWeight.w500,
                              color: AppColors.mainBlue,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(
                    vertical: 20.0.h(),
                    horizontal: 25.0.w(),
                  ),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(32.0),
                    child: Image.asset(
                      AssetsManager.forgotPasswordImage,
                      width: 796.0,
                      height: 944.0,
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}
