import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'dart:io';

import '../../widgets/category_field.dart';
import '../../widgets/date_picker.dart';
import '../../widgets/description_field.dart';
import '../../widgets/link_field.dart';
import '../../widgets/location_field.dart';
import '../../widgets/submit_button.dart';
import '../../widgets/title_field.dart';
import '../../widgets/image_preview_picker.dart';

class AddEventPage extends StatefulWidget {
  final String userId;
  final String userName;
  final String userLastName;

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

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

class _AddEventPageState extends State<AddEventPage> {
  final _formKey = GlobalKey<FormState>(); // Form key for validation

  String _title = '';
  String _description = '';
  DateTime? _selectedDate;
  String _location = 'Abidjan';
  String _category = '';
  String _link = '';
  LatLng? _selectedLatLng = const LatLng(5.348722, -3.985038); // Default coordinates
  File? _selectedImageFile; // Store the selected image

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Ajouter 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: [
                    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;
                      }),
                    ),
                    const SizedBox(height: 12),
                    LocationField(
                      location: _location,
                      selectedLatLng: _selectedLatLng,
                      onLocationPicked: (pickedLocation) => setState(() {
                        _selectedLatLng = pickedLocation;
                        _location = '${pickedLocation?.latitude}, ${pickedLocation?.longitude}';
                      }),
                    ),
                    const SizedBox(height: 12),
                    CategoryField(onSaved: (value) => setState(() => _category = value ?? '')),
                    const SizedBox(height: 12),
                    LinkField(onSaved: (value) => setState(() => _link = value ?? '')),
                  ],
                ),
              ),
            ),
          ),
          // Bouton en bas de l'écran
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: SubmitButton(
              onPressed: () async {
                if (_formKey.currentState!.validate()) {
                  _formKey.currentState!.save();
                  // Logic to add the event goes here
                }
              },
            ),
          ),
        ],
      ),
    );
  }
}
