import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:p_tits_pas/models/am_user_registration_data.dart';
import 'package:p_tits_pas/models/card_assets.dart';
import 'package:p_tits_pas/widgets/Summary.dart';
import 'package:p_tits_pas/widgets/custom_decorated_text_field.dart';
import 'package:p_tits_pas/widgets/image_button.dart';

Widget _buildDisplayFieldValue(BuildContext context, String label, String value, {bool multiLine = false, double fieldHeight = 50.0, double labelFontSize = 18.0}) {
  const FontWeight labelFontWeight = FontWeight.w600;

  // Ne pas afficher le label si labelFontSize est 0 ou si label est vide
  bool showLabel = label.isNotEmpty && labelFontSize > 0;

  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      if (showLabel)
        Text(label, style: GoogleFonts.merienda(fontSize: labelFontSize, fontWeight: labelFontWeight)),
      if (showLabel)
        const SizedBox(height: 4),
      // Utiliser Expanded si multiLine et pas de hauteur fixe, sinon Container
      multiLine && fieldHeight == null 
        ? Expanded(
            child: Container(
              width: double.infinity,
              padding: const EdgeInsets.symmetric(horizontal: 18.0, vertical: 12.0),
              decoration: BoxDecoration(
                image: const DecorationImage(
                  image: AssetImage('assets/images/input_field_bg.png'),
                  fit: BoxFit.fill,
                ),
              ),
              child: SingleChildScrollView( // Pour le défilement si le texte dépasse
                child: Text(
                  value.isNotEmpty ? value : '-',
                  style: GoogleFonts.merienda(fontSize: labelFontSize > 0 ? labelFontSize : 18.0), // Garder une taille de texte par défaut si label caché
                  maxLines: null, // Permettre un nombre illimité de lignes
                ),
              ),
            ),
          )
        : Container(
            width: double.infinity,
            height: multiLine ? null : fieldHeight,
            constraints: multiLine ? BoxConstraints(minHeight: fieldHeight) : null,
            padding: const EdgeInsets.symmetric(horizontal: 18.0, vertical: 12.0),
            decoration: BoxDecoration(
              image: const DecorationImage(
                image: AssetImage('assets/images/input_field_bg.png'),
                fit: BoxFit.fill,
              ),
            ),
            child: Text(
              value.isNotEmpty ? value : '-',
              style: GoogleFonts.merienda(fontSize: labelFontSize > 0 ? labelFontSize : 18.0),
              maxLines: multiLine ? null : 1,
              overflow: multiLine ? TextOverflow.visible : TextOverflow.ellipsis,
            ),
          ),
    ],
  );
}

class AmRegisterStep4Screen extends StatelessWidget {
  final ChildminderRegistrationData registrationData;

  const AmRegisterStep4Screen({super.key, required this.registrationData});

  Widget _buildAm1Card(BuildContext context, ChildminderRegistrationData data) {
    const double verticalSpacing = 28.0; // Espacement vertical augmenté
    const double labelFontSize = 22.0; // Taille de label augmentée

    List<Widget> details = [
      Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(child: _buildDisplayFieldValue(context, "Nom:", data.identity.lastName, labelFontSize: labelFontSize)),
          const SizedBox(width: 20),
          Expanded(child: _buildDisplayFieldValue(context, "Prénom:", data.identity.firstName, labelFontSize: labelFontSize)),
        ],
      ),
      const SizedBox(height: verticalSpacing),
      Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(child: _buildDisplayFieldValue(context, "Téléphone:", data.identity.phone, labelFontSize: labelFontSize)),
          const SizedBox(width: 20),
          Expanded(child: _buildDisplayFieldValue(context, "Email:", data.identity.email, multiLine: true, labelFontSize: labelFontSize)),
        ],
      ),
      const SizedBox(height: verticalSpacing),
      Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(child: _buildDisplayFieldValue(context, "Adresse:", "${data.identity.address}\n${data.identity.postalCode} ${data.identity.city}".trim(), labelFontSize: labelFontSize)),
          const SizedBox(width: 20),
        ],
      ),
    ];
    return SummaryCard(
      backgroundImagePath: CardColorHorizontal.peach.path,
      title: 'Informations d’identité',
      content: details,
      onEdit: () => Navigator.of(context).pushNamed('/am-register/step1', arguments: registrationData),
    );
  }

  Widget _buildAm2Card(BuildContext context, ChildminderRegistrationData data) {
    const double verticalSpacing = 28.0;
    const double labelFontSize = 22.0;

    List<Widget> myDetails = [
      Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(child: _buildDisplayFieldValue(context, "Date de naissance:", data.professional.dateOfBirth, labelFontSize: labelFontSize)),
          const SizedBox(width: 20),
          Expanded(child: _buildDisplayFieldValue(context, "Ville de naissance:", data.professional.birthCity, labelFontSize: labelFontSize)),
        ],
      ),
      const SizedBox(height: verticalSpacing),
      Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(child: _buildDisplayFieldValue(context, "Pays de naissance:", data.professional.birthCountry, labelFontSize: labelFontSize)),
          const SizedBox(width: 20),
          Expanded(child: _buildDisplayFieldValue(context, "Numéro de sécurité sociale:", data.professional.socialSecurityNumber, labelFontSize: labelFontSize)),
        ],
      ),
      const SizedBox(height: verticalSpacing),
      Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(child: _buildDisplayFieldValue(context, "Numéro d'agrément:", data.professional.agreementNumber, labelFontSize: labelFontSize)),
          const SizedBox(width: 20),
          Expanded(child: _buildDisplayFieldValue(context, "Nombre d'enfants maximum:", data.professional.maxChildren.toString(), labelFontSize: labelFontSize)),
        ],
      ),
    ];
    return SummaryCard(
      backgroundImagePath: CardColorHorizontal.lavender.path,
      title: 'Informations professionnelles',
      content: myDetails,
      onEdit: () => Navigator.of(context)
          .pushNamed('/am-register/step2', arguments: registrationData),
    );
  }

  Widget _buildMotivationCard(BuildContext context, ChildminderRegistrationData data) {
    return SummaryCard(
      backgroundImagePath: CardColorHorizontal.green.path,
      title: 'Motivation',
      content: [
        Expanded(child: CustomDecoratedTextField(
          controller: TextEditingController(text: data.presentationMessage),
          hintText: 'Parlez-nous de votre motivation',
          fieldHeight: 200,
          maxLines: 10,
          expandDynamically: true,
          readOnly: true,
          fontSize: 18.0,)),
      ],
      onEdit: () => Navigator.of(context)
          .pushNamed('/am-register/step3', arguments: registrationData),
    );
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      body: Stack(
        children: [
          Positioned.fill(
            child: Image.asset('assets/images/paper2.png', fit: BoxFit.cover, repeat: ImageRepeat.repeatY),
          ),
          Center(
            child: SingleChildScrollView(
              padding: const EdgeInsets.all(40.0),
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: screenSize.width / 4.0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text('Etape 4/4',
                        style: GoogleFonts.merienda(
                            fontSize: 16, color: Colors.black54)),
                    const SizedBox(height: 20),
                    Text('Récapitulatif de votre demande',
                        style: GoogleFonts.merienda(
                            fontSize: 22,
                            fontWeight: FontWeight.bold,
                            color: Colors.black87),
                        textAlign: TextAlign.center),
                    const SizedBox(height: 30),
                    _buildAm1Card(context, registrationData),
                    const SizedBox(height: 20),
                    if (registrationData.professional != null) ...[
                      _buildAm2Card(context, registrationData),
                      const SizedBox(height: 20),
                    ],
                    _buildMotivationCard(context, registrationData),
                    const SizedBox(height: 40),
                    ImageButton(
                      bg: 'assets/images/btn_green.png',
                      text: 'Soumettre ma demande',
                      textColor: const Color(0xFF2D6A4F),
                      width: 350,
                      height: 50,
                      fontSize: 18,
                      onPressed: () {
                        // Vérification des données requises
                        _showConfirmationModal(context);
                      },
                    )
                  ],
                ),
              ),
            ),
          ),
          Positioned(
            top: screenSize.height / 2 - 20,
            left: 40,
            child: IconButton(
              icon: Transform.flip(
                  flipX: true,
                  child: Image.asset('assets/images/chevron_right.png',
                      height: 40)),
              onPressed: () => Navigator.pop(context),
              tooltip: 'Retour',
            ),
          ),
        ],
      ),
    );
  }

  void _showConfirmationModal(BuildContext context) {
    showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext dialogContext) {
        return AlertDialog(
          title: Text(
            'Demande enregistrée',
            style: GoogleFonts.merienda(fontWeight: FontWeight.bold),
          ),
          content: Text(
            'Votre dossier a bien été pris en compte. Un gestionnaire le validera bientôt.',
            style: GoogleFonts.merienda(fontSize: 14),
          ),
          actions: <Widget>[
            TextButton(
              child: Text('OK',
                  style: GoogleFonts.merienda(fontWeight: FontWeight.bold)),
              onPressed: () {
                Navigator.of(dialogContext).pop();
                Navigator.of(context)
                    .pushNamedAndRemoveUntil('/login', (route) => false);
              },
            ),
          ],
        );
      },
    );
  }
}
