import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'dart:io'; // Pour l'usage des fichiers (image)
import '../../widgets/fields/category_field.dart'; // Importation des widgets personnalisés
import '../../widgets/date_picker.dart';
import '../../widgets/fields/description_field.dart';
import '../../widgets/fields/link_field.dart';
import '../../widgets/fields/location_field.dart';
import '../../widgets/submit_button.dart';
import '../../widgets/fields/title_field.dart';
import '../../widgets/image_preview_picker.dart';
import '../../widgets/fields/tags_field.dart';
import '../../widgets/fields/attendees_field.dart';
import '../../widgets/fields/organizer_field.dart';
import '../../widgets/fields/transport_info_field.dart';
import '../../widgets/fields/accommodation_info_field.dart';
import '../../widgets/fields/privacy_rules_field.dart';
import '../../widgets/fields/security_protocol_field.dart';
import '../../widgets/fields/parking_field.dart';
import '../../widgets/fields/accessibility_field.dart';
import '../../widgets/fields/participation_fee_field.dart';

/// Page pour ajouter un événement
/// Permet à l'utilisateur de remplir un formulaire avec des détails sur l'événement
class AddEventPage extends StatefulWidget {
  final String userId;
  final String userFirstName;
  final String userLastName;

  const AddEventPage({
    super.key,
    required this.userId,
    required this.userFirstName,
    required this.userLastName,
  });

  @override
  _AddEventPageState createState() => _AddEventPageState();
}

class _AddEventPageState extends State<AddEventPage> {
  final _formKey = GlobalKey<FormState>(); // Clé pour la validation du formulaire

  // Variables pour stocker les données de l'événement
  String _title = '';
  String _description = '';
  DateTime? _selectedDate;
  DateTime? _endDate;
  String _location = 'Abidjan';
  String _category = '';
  String _link = '';
  String _organizer = '';
  List<String> _tags = [];
  int _maxParticipants = 0;
  LatLng? _selectedLatLng = const LatLng(5.348722, -3.985038); // Coordonnées par défaut
  File? _selectedImageFile; // Image sélectionnée
  String _status = 'Actif';
  String _organizerEmail = '';
  String _organizerPhone = '';
  int _participationFee = 0;
  String _privacyRules = '';
  String _transportInfo = '';
  String _accommodationInfo = '';
  bool _isAccessible = false;
  bool _hasParking = false;
  String _securityProtocol = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Créer un événement'),
        backgroundColor: const Color(0xFF1E1E2C),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ),
      backgroundColor: const Color(0xFF1E1E2C),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              padding: const EdgeInsets.all(16.0),
              child: Form(
                key: _formKey,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    // Section d'informations de base
                    _buildSectionHeader('Informations de base'),
                    ImagePreviewPicker(
                      onImagePicked: (File? imageFile) {
                        setState(() {
                          _selectedImageFile = imageFile;
                        });
                      },
                    ),
                    const SizedBox(height: 20),
                    TitleField(onSaved: (value) => setState(() => _title = value ?? '')),
                    const SizedBox(height: 12),
                    DescriptionField(onSaved: (value) => setState(() => _description = value ?? '')),
                    const SizedBox(height: 12),
                    DatePickerField(
                      selectedDate: _selectedDate,
                      onDatePicked: (picked) => setState(() {
                        _selectedDate = picked;
                      }),
                      label: 'Date de début',
                    ),
                    const SizedBox(height: 12),
                    DatePickerField(
                      selectedDate: _endDate,
                      onDatePicked: (picked) => setState(() {
                        _endDate = picked;
                      }),
                      label: 'Date de fin',
                    ),
                    const SizedBox(height: 12),
                    LocationField(
                      location: _location,
                      onLocationPicked: (value) => setState(() => _location = (value ?? 'Abidjan') as String),
                    ),
                    const SizedBox(height: 12),
                    CategoryField(
                      onSaved: (value) => setState(() => _category = value ?? ''),
                    ),
                    const SizedBox(height: 12),
                    LinkField(
                      onSaved: (value) => setState(() => _link = value ?? ''),
                    ),
                    const SizedBox(height: 12),
                    AttendeesField(
                      onSaved: (value) => setState(() => _maxParticipants = value ?? 0),
                    ),
                    const SizedBox(height: 12),
                    TagsField(
                      onSaved: (value) => setState(() => _tags = value ?? []),
                    ),
                    const SizedBox(height: 12),
                    OrganizerField(
                      onSaved: (value) => setState(() => _organizer = value ?? ''),
                    ),
                    const SizedBox(height: 12),
                    TransportInfoField(
                      onSaved: (value) => setState(() => _transportInfo = value ?? ''),
                    ),
                    const SizedBox(height: 12),
                    AccommodationInfoField(
                      onSaved: (value) => setState(() => _accommodationInfo = value ?? ''),
                    ),
                    const SizedBox(height: 12),
                    PrivacyRulesField(
                      onSaved: (value) => setState(() => _privacyRules = value ?? ''),
                    ),
                    const SizedBox(height: 12),
                    SecurityProtocolField(
                      onSaved: (value) => setState(() => _securityProtocol = value ?? ''),
                    ),
                    const SizedBox(height: 12),
                    ParkingField(
                      onSaved: (value) => setState(() => _hasParking = (value as bool?) ?? false),
                    ),
                    const SizedBox(height: 12),
                    AccessibilityField(
                      onSaved: (value) => setState(() => _participationFee = (value as int?) ?? 0),
                    ),
                    const SizedBox(height: 12),
                    ParticipationFeeField(
                      onSaved: (value) => setState(() => _participationFee = (value as int?) ?? 0),
                    ),
                    const SizedBox(height: 12),
                    SubmitButton(
                      onPressed: () {
                        if (_formKey.currentState?.validate() ?? false) {
                          // Log des données de l'événement avant l'envoi
                          print('Titre de l\'événement : $_title');
                          print('Description de l\'événement : $_description');
                          print('Date de début : $_selectedDate');
                          print('Date de fin : $_endDate');
                          print('Lieu : $_location');
                          print('Catégorie : $_category');
                          print('Lien de l\'événement : $_link');
                          print('Organisateur : $_organizer');
                          print('Tags : $_tags');
                          print('Maximum de participants : $_maxParticipants');
                          print('Image sélectionnée : $_selectedImageFile');
                          print('Transport : $_transportInfo');
                          print('Hébergement : $_accommodationInfo');
                          print('Règles de confidentialité : $_privacyRules');
                          print('Protocole de sécurité : $_securityProtocol');
                          print('Parking disponible : $_hasParking');
                          print('Accessibilité : $_isAccessible');
                          print('Frais de participation : $_participationFee');
                          // Logique d'envoi des données vers le backend...

                          ScaffoldMessenger.of(context).showSnackBar(
                            const SnackBar(content: Text('Événement créé avec succès !')),
                          );
                        } else {
                          ScaffoldMessenger.of(context).showSnackBar(
                            const SnackBar(content: Text('Veuillez remplir tous les champs requis')),
                          );
                        }
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // En-tête de section pour mieux organiser les champs
  Widget _buildSectionHeader(String title) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Text(
        title,
        style: const TextStyle(
          fontSize: 18,
          fontWeight: FontWeight.bold,
          color: Colors.white,
        ),
      ),
    );
  }
}
