import 'package:flutter/material.dart';

import '../../model/word.dart';
import 'components/words_screen.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

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


  @override
  State<homepagestful> createState() => _homepagestfulState();
}

class _homepagestfulState extends State<homepagestful> {
  late TextEditingController _textHomepageContoller = TextEditingController();
  late List<WordsScreen> listaWidget;
  late String _request = '';   //stringa dove memorizzare la parola

  //metodo per la richiesta
  Future<void> fetchWord() async {
    String text_controller = _textHomepageContoller.text;
    final response = await http.get(
        Uri.parse("https://315a-151-45-52-98.ngrok-free.app/api/words/$text_controller"));

    if (response.statusCode == 200) {
      final Map<String, dynamic> data = json.decode(response.body);
      if (data['status'] == true) {
        final request = data['translation'];
        setState(() {
          _request = request; //aggiorna lo stato della stringa
        });
      } else {
        final request = data['message'];
        setState(() {
          _request = request; //aggiorna lo stato della stringa con messsaggio not found
        });
      }
    } else {
      throw Exception('Failed to fetch Word');
    }
  }

  @override
  void dispose() {
    _textHomepageContoller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            const SizedBox(height: 50),
            Image.asset('assets/Logo/logoMottola.png', width: 250, height: 250),
            const SizedBox(height: 20),
            Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset('assets/icons/italy.png', width: 40, height: 40),
                  const SizedBox(width: 20),
                  Image.asset('assets/icons/next.png', width: 30, height:30),
                  const SizedBox(width: 10),
                  //Text('  -->  ', style: TextStyle(fontSize: 30, color: Colors.grey[800])),
                  Image.asset('assets/icons/flagMottola.png', width: 54, height: 54),
                ]),
            const SizedBox(height: 20),
            TextField(
              controller: _textHomepageContoller,
              keyboardType: TextInputType.multiline,
              maxLines: null,
              style: TextStyle(fontSize: 20, color: Colors.grey[800]),
              decoration: const InputDecoration(
                  hintText: 'Parola Italiana',
                  hintStyle: TextStyle(fontSize: 20)
              ),
            ),
            const SizedBox(height: 20),
            Container(
              margin: const EdgeInsets.all(10.0),
              width:  500,
              height: 100,
              decoration: BoxDecoration(
                color: const Color(0x9121781C),
                borderRadius: BorderRadius.circular(10.0),
              ),
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(_request,
                        style: const TextStyle(fontSize: 30, color: Color(0xFFFFFFFF),fontWeight: FontWeight.bold)
                    ),

                    //Align(
                    //  alignment: Alignment.center,
                    //  child: Text("ESEMPIO",
                    //      style: TextStyle(fontSize: 30, color: Color(0xFFFFFFFF),fontWeight: FontWeight.bold)),
                    //),
                  ]
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                if (_textHomepageContoller.text.isEmpty){
                  showDialog(
                      context: context,
                      builder: (context) {
                       return const AlertDialog(
                         content: Text('Inserire il vocabolo da tradurre',
                              style: TextStyle(color: Color(
                                  0xFFFF0000),
                                  fontSize: 25,
                                  fontWeight: FontWeight.bold)),
                       );
                      },
                  );
                } else {
                  fetchWord();
                }
              },

              child: Text('Traduci'),
              style: ElevatedButton.styleFrom(
                      backgroundColor: const Color(0xFFDF930B),
                      padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 20),
                      textStyle: const TextStyle(
                          fontSize: 15,
                          fontWeight: FontWeight.bold)),
            ),
          ],
        ),
      ),
    );
  }
}
