import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:go_router/go_router.dart';
import 'package:pinput/pinput.dart';
import 'package:provider/provider.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 '../../shared/base_button.dart';
import '../strings/forgot_password_otp_strings.dart';
import '../../forget_password_create/pages/forgot_password_create_screen.dart';

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

  @override
  State<DesktopForgotPasswordOtpPage> createState() =>
      _DesktopForgotPasswordOtpPageState();
}

class _DesktopForgotPasswordOtpPageState
    extends State<DesktopForgotPasswordOtpPage> {
  late final AuthViewModel viewModel;

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

  void _viewModelListener() {
    if (viewModel.errorMessageOtp != null) {
      AppToast.error(context, viewModel.errorMessageOtp!);
    }

    if (viewModel.resetToken != null) {
      Router.neglect(
        context,
        () => Navigator.push(
          context,
          MaterialPageRoute(builder: (_) => const ForgotPasswordCreateScreen()),
        ),
      );
    }
  }

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

  @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(
                        ForgotPasswordOtpStrings.forgotPasswordTitle,
                        style: TextStyle(
                          fontSize: 28.0.sp(),
                          fontWeight: FontWeight.bold,
                          color: AppColors.grey80,
                        ),
                      ),
                      SizedBox(height: 16.0.h()),
                      Text(
                        ForgotPasswordOtpStrings.forgotPasswordSubtitle,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontSize: 14.0.sp(),
                          fontWeight: FontWeight.w400,
                          color: AppColors.grey60,
                        ),
                      ),
                      SizedBox(height: 40.0.h()),
                      Pinput(
                        controller: viewModel.otpController,
                        length: 5,
                        pinAnimationType: PinAnimationType.scale,
                        defaultPinTheme: PinTheme(
                          width: 50.0.w(),
                          height: 50.0.h(),
                          textStyle: TextStyle(
                            fontSize: 20.0.sp(),
                            fontWeight: FontWeight.bold,
                          ),
                          decoration: BoxDecoration(
                            color: AppColors.grey0,
                            borderRadius: BorderRadius.circular(10),
                            border: Border.all(color: AppColors.grey40),
                          ),
                        ),
                      ),
                      SizedBox(height: 24.0.h()),
                      viewModel.isLoadingOtp
                          ? const BaseButton(
                              isEnabled: false,
                              text: ForgotPasswordOtpStrings.resetLink,
                              onPressed: null,
                            )
                          : BaseButton(
                              isEnabled: viewModel.canSubmitOtpPage,
                              text: ForgotPasswordOtpStrings.resetLink,
                              onPressed: viewModel.canSubmitOtpPage
                                  ? () async {
                                      await viewModel.verifyOtp(context);
                                    }
                                  : null,
                            ),
                      const SizedBox(height: 12.0),
                      TextButton(
                        onPressed: () {
                          context.pop();
                        },
                        child: Text(
                          ForgotPasswordOtpStrings.backToSignInButton,
                          style: TextStyle(
                            fontSize: 14.0.sp(),
                            fontWeight: FontWeight.w500,
                            color: AppColors.mainBlue,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
