import 'package:flutter/material.dart';
import 'verifica_rca_screen.dart'; // Assicurati che l'import sia corretto

void main() {
  runApp(const MaterialApp(
    home: TestScrapingPage(),
  ));
}

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

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

class _TestScrapingPageState extends State<TestScrapingPage> {
  // Metti una targa vera qui per fare prima nei test
  final TextEditingController _targaController = TextEditingController(text: "AB123CD");
  String _risultato = "Nessun dato ancora";

  void _lanciaVerifica() async {
    // 1. Lancia la schermata di verifica
    final dataScadenza = await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => VerificaRcaScreen(targa: _targaController.text),
      ),
    );

    // 2. Quando torni indietro, se c'è un risultato, mostralo
    if (dataScadenza != null) {
      if (!mounted) return; // FIX: Prevents crash if widget is disposed
      
      setState(() {
        _risultato = "Scadenza trovata: $dataScadenza";
      });

      // Feedback visivo
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text("Data importata: $dataScadenza"), backgroundColor: Colors.green),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("TEST SCRAPING RCA"), backgroundColor: Colors.orange),
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text("Inserisci una targa reale per testare:", style: TextStyle(fontSize: 16)),
            const SizedBox(height: 10),
            TextField(
              controller: _targaController,
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
                labelText: "Targa",
                hintText: "Es. GA000GA",
              ),
              textCapitalization: TextCapitalization.characters,
            ),
            const SizedBox(height: 30),
            ElevatedButton.icon(
              onPressed: _lanciaVerifica,
              icon: const Icon(Icons.search),
              label: const Text("VERIFICA COPERTURA"),
              style: ElevatedButton.styleFrom(
                minimumSize: const Size(double.infinity, 50),
                textStyle: const TextStyle(fontSize: 18),
              ),
            ),
            const SizedBox(height: 40),
            const Divider(),
            const Text("RISULTATO:", style: TextStyle(fontWeight: FontWeight.bold, color: Colors.grey)),
            const SizedBox(height: 10),
            Text(
              _risultato,
              style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.blue),
            ),
          ],
        ),
      ),
    );
  }
}