import 'dart:io';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

import '../../../model/word.dart';


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

  @override
  State<WordsScreen> createState() => _WordsScreenState();
}

class _WordsScreenState extends State<WordsScreen> {
  Future<Word> fetchWord() async {
    final response = await http.get(
        Uri.parse("https://5ddc-37-101-56-133.ngrok-free.app/api/words/tasto"));

    if (response.statusCode == 200) {
      return Word.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Failed to fetch Word');
    }
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Word>(
      future: fetchWord(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          // Display the fetched joke when data is available
          return Center(child: Text(
              snapshot.data!.translation,
              style: TextStyle(fontSize: 30, color: Colors.grey[800])
            )
          );
        } else if (snapshot.hasError) {
          // Display an error message if there's an error
          return Text('${snapshot.error}');
        } else {
          // Display a loading indicator while waiting for data
          return Center(child: CircularProgressIndicator());
        }
      },
    );
  }
}

