import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'dart:math' as math; // Pour la rotation du chevron
import '../../../models/parent_user_registration_data.dart'; // Import du modèle de données
import '../../../utils/data_generator.dart'; // Import du générateur de données
import '../../../widgets/custom_app_text_field.dart'; // Import du widget CustomAppTextField
import '../../../models/card_assets.dart'; // Import des enums de cartes

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

  @override
  State<ParentRegisterStep1Screen> createState() => _ParentRegisterStep1ScreenState();
}

class _ParentRegisterStep1ScreenState extends State<ParentRegisterStep1Screen> {
  final _formKey = GlobalKey<FormState>();
  late UserRegistrationData _registrationData;

  // Contrôleurs pour les champs (restauration CP et Ville)
  final _lastNameController = TextEditingController();
  final _firstNameController = TextEditingController();
  final _phoneController = TextEditingController();
  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();
  final _confirmPasswordController = TextEditingController();
  final _addressController = TextEditingController(); // Rue seule
  final _postalCodeController = TextEditingController(); // Restauré
  final _cityController = TextEditingController(); // Restauré

  @override
  void initState() {
    super.initState();
    _registrationData = UserRegistrationData();
    _generateAndFillData();
  }

  void _generateAndFillData() {
    final String genFirstName = DataGenerator.firstName();
    final String genLastName = DataGenerator.lastName();
    
    // Utilisation des méthodes publiques de DataGenerator
    _addressController.text = DataGenerator.address(); 
    _postalCodeController.text = DataGenerator.postalCode();
    _cityController.text = DataGenerator.city();

    _firstNameController.text = genFirstName;
    _lastNameController.text = genLastName;
    _phoneController.text = DataGenerator.phone();
    _emailController.text = DataGenerator.email(genFirstName, genLastName);
    _passwordController.text = DataGenerator.password();
    _confirmPasswordController.text = _passwordController.text;
  }

  @override
  void dispose() {
    _lastNameController.dispose();
    _firstNameController.dispose();
    _phoneController.dispose();
    _emailController.dispose();
    _passwordController.dispose();
    _confirmPasswordController.dispose();
    _addressController.dispose();
    _postalCodeController.dispose();
    _cityController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;

    return Scaffold(
      body: Stack(
        children: [
          // Fond papier
          Positioned.fill(
            child: Image.asset(
              'assets/images/paper2.png',
              fit: BoxFit.cover,
              repeat: ImageRepeat.repeat,
            ),
          ),

          // Contenu centré
          Center(
            child: SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                   // Indicateur d'étape (à rendre dynamique)
                  Text(
                    'Étape 1/5',
                    style: GoogleFonts.merienda(fontSize: 16, color: Colors.black54),
                  ),
                  const SizedBox(height: 10),
                  // Texte d'instruction
                  Text(
                    'Informations du Parent Principal',
                    style: GoogleFonts.merienda(
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                      color: Colors.black87,
                    ),
                    textAlign: TextAlign.center,
                  ),
                  const SizedBox(height: 30),

                  // Carte jaune contenant le formulaire
                  Container(
                    width: screenSize.width * 0.6,
                    padding: const EdgeInsets.symmetric(vertical: 50, horizontal: 50),
                    constraints: const BoxConstraints(minHeight: 570),
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage(CardColorHorizontal.peach.path),
                        fit: BoxFit.fill,
                      ),
                    ),
                    child: Form(
                      key: _formKey,
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Row(
                            children: [
                              Expanded(flex: 12, child: CustomAppTextField(controller: _lastNameController, labelText: 'Nom', hintText: 'Votre nom de famille', style: CustomAppTextFieldStyle.beige, fieldWidth: double.infinity)),
                              Expanded(flex: 1, child: const SizedBox()), // Espace de 4%
                              Expanded(flex: 12, child: CustomAppTextField(controller: _firstNameController, labelText: 'Prénom', hintText: 'Votre prénom', style: CustomAppTextFieldStyle.beige, fieldWidth: double.infinity)),
                            ],
                          ),
                          const SizedBox(height: 20),
                          Row(
                            children: [
                              Expanded(flex: 12, child: CustomAppTextField(controller: _phoneController, labelText: 'Téléphone', keyboardType: TextInputType.phone, hintText: 'Votre numéro de téléphone', style: CustomAppTextFieldStyle.beige, fieldWidth: double.infinity)),
                              Expanded(flex: 1, child: const SizedBox()), // Espace de 4%
                              Expanded(flex: 12, child: CustomAppTextField(controller: _emailController, labelText: 'Email', keyboardType: TextInputType.emailAddress, hintText: 'Votre adresse e-mail', style: CustomAppTextFieldStyle.beige, fieldWidth: double.infinity)),
                            ],
                          ),
                          const SizedBox(height: 20),
                          Row(
                            children: [
                              Expanded(flex: 12, child: CustomAppTextField(controller: _passwordController, labelText: 'Mot de passe', obscureText: true, hintText: 'Créez votre mot de passe', style: CustomAppTextFieldStyle.beige, fieldWidth: double.infinity, validator: (value) {
                                if (value == null || value.isEmpty) return 'Mot de passe requis';
                                if (value.length < 6) return '6 caractères minimum';
                                return null;
                              })),
                              Expanded(flex: 1, child: const SizedBox()), // Espace de 4%
                              Expanded(flex: 12, child: CustomAppTextField(controller: _confirmPasswordController, labelText: 'Confirmation', obscureText: true, hintText: 'Confirmez le mot de passe', style: CustomAppTextFieldStyle.beige, fieldWidth: double.infinity, validator: (value) {
                                if (value == null || value.isEmpty) return 'Confirmation requise';
                                if (value != _passwordController.text) return 'Ne correspond pas';
                                return null;
                              })),
                            ],
                          ),
                          const SizedBox(height: 20),
                          CustomAppTextField(
                            controller: _addressController, 
                            labelText: 'Adresse (N° et Rue)', 
                            hintText: 'Numéro et nom de votre rue', 
                            style: CustomAppTextFieldStyle.beige,
                            fieldWidth: double.infinity,
                          ),
                          const SizedBox(height: 20),
                          Row(
                            children: [
                              Expanded(flex: 1, child: CustomAppTextField(controller: _postalCodeController, labelText: 'Code Postal', keyboardType: TextInputType.number, hintText: 'Code postal', style: CustomAppTextFieldStyle.beige, fieldWidth: double.infinity)),
                              const SizedBox(width: 20),
                              Expanded(flex: 4, child: CustomAppTextField(controller: _cityController, labelText: 'Ville', hintText: 'Votre ville', style: CustomAppTextFieldStyle.beige, fieldWidth: double.infinity)),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),

          // Chevron de navigation gauche (Retour)
          Positioned(
            top: screenSize.height / 2 - 20, // Centré verticalement
            left: 40,
            child: IconButton(
              icon: Transform(
                alignment: Alignment.center,
                transform: Matrix4.rotationY(math.pi), // Inverse horizontalement
                child: Image.asset('assets/images/chevron_right.png', height: 40),
              ),
              onPressed: () => Navigator.pop(context), // Retour à l'écran de choix
              tooltip: 'Retour',
            ),
          ),

          // Chevron de navigation droit (Suivant)
          Positioned(
            top: screenSize.height / 2 - 20, // Centré verticalement
            right: 40,
            child: IconButton(
              icon: Image.asset('assets/images/chevron_right.png', height: 40),
              onPressed: () {
                if (_formKey.currentState?.validate() ?? false) {
                  _registrationData.updateParent1(
                    ParentData(
                      firstName: _firstNameController.text,
                      lastName: _lastNameController.text,
                      address: _addressController.text, // Rue
                      postalCode: _postalCodeController.text, // Ajout
                      city: _cityController.text, // Ajout
                      phone: _phoneController.text,
                      email: _emailController.text,
                      password: _passwordController.text,
                    )
                  );
                  Navigator.pushNamed(context, '/parent-register/step2', arguments: _registrationData);
                }
              },
              tooltip: 'Suivant',
            ),
          ),
        ],
      ),
    );
  }
} 