import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';
import 'package:initial_folder/screens/login/reset/reset_screen.dart';
import 'package:initial_folder/size_config.dart';
import 'package:initial_folder/theme.dart';
import 'package:initial_folder/widgets/login_regist/header.dart';
import 'package:initial_folder/services/reset_service.dart'; 

class ResetSuccess extends StatelessWidget {
  static String routeName = "/reset_success";
  final String email; 

  ResetSuccess({Key? key, this.email = ''}) : super(key: key);

  final TextEditingController nameController = TextEditingController(text: '');
  final TextEditingController emailController = TextEditingController(text: '');
  final TextEditingController passwordController = TextEditingController(text: '');

  Future<void> resendEmail(BuildContext context) async {
    try {
      await ResetService().kirimEmail(email: email); 
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Link reset berhasil dikirim ulang ke email Anda')),
      );
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Gagal mengirim ulang email. Silakan coba lagi.')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final Brightness brightnessValue = MediaQuery.of(context).platformBrightness;
    final isDarkMode = Theme.of(context).brightness == Brightness.dark;
    SizeConfig().init(context);

    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text(
            'Reset Password',
            style: thirdTextStyle.copyWith(
                color: isDarkMode ? baruTextutih : baruTexthitam,
                fontWeight: bold),
          ),
        ),
        body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
            Icon(
              Icons.check_rounded,
              color: Colors.greenAccent[700],
              size: getProportionateScreenWidth(60),
            ),
            SizedBox(height: getProportionateScreenHeight(24)),
            Header(
              title: false,
              text2: "Sukses",
              text: 'Cek emailmu untuk lanjut mereset password',
              style: primaryTextStyle.copyWith(
                color: tenthColor,
                fontWeight: reguler,
                fontSize: getProportionateScreenWidth(14),
                letterSpacing: 0.5,
              ),
            ),
            SizedBox(height: getProportionateScreenHeight(44)),
            Container(
              alignment: Alignment.center,
              child: RichText(
                text: TextSpan(
                  children: <TextSpan>[
                    TextSpan(
                      text: 'Kirim Ulang  ',
                      style: thirdTextStyle.copyWith(
                        color: isDarkMode ?
                              primaryColor : primaryColorligtmode, 
                              fontWeight: bold,
                        fontSize: getProportionateScreenWidth(12),
                      ),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          resendEmail(context); // Panggil fungsi kirim ulang
                        },
                    ),
                    TextSpan(
                      text: "atau  ",
                      style: thirdTextStyle.copyWith(
                        color: isDarkMode ? baruTextutih : fifteenColor,
                        fontWeight: reguler,
                        fontSize: getProportionateScreenWidth(12),
                      ),
                    ),
                    TextSpan(
                      text: 'Masuk',
                      style: thirdTextStyle.copyWith(
                        color: isDarkMode ?primaryColor : primaryColorligtmode,
                        fontWeight: bold,
                        fontSize: getProportionateScreenWidth(12),
                      ),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          Navigator.of(context).pop(); // Kembali ke layar sebelumnya
                          Navigator.of(context).pop(); // Tutup reset_screen
                        },
                    ),
                  ],
                  style: primaryTextStyle.copyWith(
                    color: tenthColor,
                    fontWeight: reguler,
                    fontSize: getProportionateScreenWidth(14),
                  ),
                ),
              ),
            ),
          ]),
        ),
      ),
    );
  }
}
