import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:sigap/src/features/auth/presentasion/controllers/forgot-password/forgot_password_controller.dart';
import 'package:sigap/src/features/auth/presentasion/widgets/auth_button.dart';
import 'package:sigap/src/features/auth/presentasion/widgets/auth_header.dart';
import 'package:sigap/src/shared/widgets/text/custom_text_field.dart';
import 'package:sigap/src/utils/constants/colors.dart';
import 'package:sigap/src/utils/constants/image_strings.dart';
import 'package:sigap/src/utils/constants/sizes.dart';
import 'package:sigap/src/utils/helpers/helper_functions.dart';

class ForgotPasswordScreen extends StatelessWidget {
  const ForgotPasswordScreen({super.key});

  @override
  Widget build(BuildContext context) {
    // Get the controller
    final controller = Get.find<ForgotPasswordController>();

    final isDarkMode = THelperFunctions.isDarkMode(context);

    // Set system overlay style
    SystemChrome.setSystemUIOverlayStyle(
      const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.dark,
      ),
    );

    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back,
            color: isDarkMode ? TColors.accent : TColors.primary,
          ),
          onPressed: controller.goBack,
        ),
      ),
      body: SafeArea(
        child: Center(
          child: SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.all(TSizes.defaultSpace),
              child: Obx(
                () => Form(
                  key: controller.formKey,
                  child:
                      controller.isEmailSent.value
                          ? _buildSuccessView(controller)
                          : _buildFormView(controller, context),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildFormView(
    ForgotPasswordController controller,
    BuildContext? context,
  ) {
    final isDark = THelperFunctions.isDarkMode(context!);
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        // Logo centered
        SvgPicture.asset(TImages.lightAppBgLogo, height: 100, width: 100),
        const SizedBox(height: 16),

        // Header centered
        const AuthHeader(
          crossAxisAlignment: CrossAxisAlignment.center,
          title: 'Forgot Password',
          subtitle: 'Enter your email to reset your password',
        ),

        const SizedBox(height: 24),

        // Email field
        Obx(
          () => CustomTextField(
            label: 'Email',
            controller: controller.emailController,
            validator: controller.validateEmail,
            keyboardType: TextInputType.emailAddress,
            errorText: controller.emailError.value,
            hintText: 'enter your email',
            prefixIcon: Icon(
              Icons.email_outlined,
              size: 20,
              color: isDark ? TColors.accent : TColors.primary,
            ),
          ),
        ),

        const SizedBox(height: 24),

        // Reset password button
        Obx(
          () => AuthButton(
            text: 'Reset Password',
            onPressed: controller.resetPassword,
            isLoading: controller.isLoading.value,
          ),
        ),
      ],
    );
  }

  Widget _buildSuccessView(ForgotPasswordController controller) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        // Logo centered
        SvgPicture.asset(TImages.lightAppBgLogo, height: 100, width: 100),
        const SizedBox(height: 32),

        // Success icon
        Icon(Icons.check_circle_outline, size: 80, color: TColors.success),

        const SizedBox(height: 24),

        // Success message
        Text(
          'Email Sent',
          style: TextStyle(
            fontSize: 24,
            fontWeight: FontWeight.bold,
            color: TColors.textPrimary,
          ),
        ),

        const SizedBox(height: 16),

        Text(
          'We have sent a password recovery instructions to your email.',
          textAlign: TextAlign.center,
          style: TextStyle(fontSize: 16, color: TColors.textSecondary),
        ),

        const SizedBox(height: 32),

        // Back to sign in button
        AuthButton(text: 'Back to Sign In', onPressed: controller.goBack),
      ],
    );
  }
}
