import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'dart:io';

import '../../models/am_registration_data.dart';
import '../../models/card_assets.dart';
import '../../utils/data_generator.dart';
import '../../widgets/professional_info_form_screen.dart';

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

  @override
  State<AmRegisterStep2Screen> createState() => _AmRegisterStep2ScreenState();
}

class _AmRegisterStep2ScreenState extends State<AmRegisterStep2Screen> {
  String? _photoPathFramework;
  File? _photoFile;

  Future<void> _pickPhoto() async {
    // TODO: Remplacer par la vraie logique ImagePicker
    // final imagePicker = ImagePicker();
    // final pickedFile = await imagePicker.pickImage(source: ImageSource.gallery);
    // if (pickedFile != null) {
    //   setState(() {
    //     _photoFile = File(pickedFile.path);
    //     _photoPathFramework = pickedFile.path;
    //   });
    // } else {
      setState(() {
        _photoPathFramework = 'assets/images/icon_assmat.png';
        _photoFile = null;
      });
    // }
    print("Photo sélectionnée: $_photoPathFramework");
  }

  @override
  Widget build(BuildContext context) {
    final registrationData = Provider.of<AmRegistrationData>(context, listen: false);
    
    // Préparer les données initiales
    ProfessionalInfoData initialData = ProfessionalInfoData(
      photoPath: registrationData.photoPath,
      photoConsent: registrationData.photoConsent,
      dateOfBirth: registrationData.dateOfBirth,
      birthCity: registrationData.birthCity,
      birthCountry: registrationData.birthCountry,
      nir: registrationData.nir,
      agrementNumber: registrationData.agrementNumber,
      capacity: registrationData.capacity,
    );

    // Générer des données de test si les champs sont vides
    if (registrationData.dateOfBirth == null && registrationData.nir.isEmpty) {
      initialData = ProfessionalInfoData(
        photoPath: 'assets/images/icon_assmat.png',
        photoConsent: true,
        dateOfBirth: DateTime(1985, 3, 15),
        birthCity: DataGenerator.city(),
        birthCountry: 'France',
        nir: '${DataGenerator.randomIntInRange(1, 3)}${DataGenerator.randomIntInRange(80, 96)}${DataGenerator.randomIntInRange(1, 13).toString().padLeft(2, '0')}${DataGenerator.randomIntInRange(1, 100).toString().padLeft(2, '0')}${DataGenerator.randomIntInRange(100, 1000).toString().padLeft(3, '0')}${DataGenerator.randomIntInRange(100, 1000).toString().padLeft(3, '0')}${DataGenerator.randomIntInRange(10, 100).toString().padLeft(2, '0')}',
        agrementNumber: 'AM${DataGenerator.randomIntInRange(10000, 100000)}',
        capacity: DataGenerator.randomIntInRange(1, 5),
      );
    }

    return ProfessionalInfoFormScreen(
      stepText: 'Étape 2/4',
      title: 'Vos informations professionnelles',
      cardColor: CardColorHorizontal.green,
      initialData: initialData,
      previousRoute: '/am-register-step1',
      onPickPhoto: _pickPhoto,
      onSubmit: (data) {
        registrationData.updateProfessionalInfo(
          photoPath: _photoPathFramework ?? data.photoPath,
          photoConsent: data.photoConsent,
          dateOfBirth: data.dateOfBirth,
          birthCity: data.birthCity,
          birthCountry: data.birthCountry,
          nir: data.nir,
          agrementNumber: data.agrementNumber,
          capacity: data.capacity,
        );
        context.go('/am-register-step3');
      },
    );
  }
}