import 'dart:developer';

import 'package:anyway/structs/trip.dart';
import 'package:auto_size_text/auto_size_text.dart';

import 'package:flutter/material.dart';

class Greeter extends StatefulWidget {
  final Trip trip;

  Greeter({
    required this.trip,
  });

  @override
  State<Greeter> createState() => _GreeterState();
}


class _GreeterState extends State<Greeter> {
  
  Widget greeterBuilder (BuildContext context, Widget? child) {
    ThemeData theme = Theme.of(context);
    TextStyle greeterStyle = TextStyle(color: theme.primaryColor, fontWeight: FontWeight.bold, fontSize: 24);

    Widget topGreeter;

    if (widget.trip.uuid != 'pending') {
      topGreeter = FutureBuilder(
        future: widget.trip.cityName,
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          if (snapshot.hasData) {
            return AutoSizeText(
              maxLines: 1,
              'Welcome to ${snapshot.data}!',
              style: greeterStyle
            );
          } else if (snapshot.hasError) {
            log('Error while fetching city name');
            return AutoSizeText(
              maxLines: 1,
              'Welcome to your trip!',
              style: greeterStyle
            );
          } else {
            return AutoSizeText(
              maxLines: 1,
              'Welcome to ...',
              style: greeterStyle
            );
          }
        }
      );
    } else {
      // still awaiting the trip
      // We can hopefully infer the city name from the cityName future
      // Show a linear loader at the bottom and an info message above
      topGreeter = Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FutureBuilder(
            future: widget.trip.cityName,
            builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
              if (snapshot.hasData) {
                return AutoSizeText(
                  maxLines: 1,
                  'Generating your trip to ${snapshot.data}...',
                  style: greeterStyle
                );
              } else if (snapshot.hasError) {
                // the exact error is shown in the central part of the trip overview. No need to show it here
                return AutoSizeText(
                  maxLines: 1,
                  'Error while loading trip.',
                  style: greeterStyle
                  );
              }
              return AutoSizeText(
                  maxLines: 1,
                  'Generating your trip...',
                  style: greeterStyle
                  );
            }
          ),
          Padding(
            padding: EdgeInsets.all(5),
            child: const LinearProgressIndicator()
          )
        ]
      );
    }

    return Center(
      child: Column(
        children: [
          // Padding(padding: EdgeInsets.only(top: 20)),
          topGreeter,
          Padding(
            padding: EdgeInsets.all(20),
            child: bottomGreeter
          ),
        ],
      )
    );
  }

  Widget bottomGreeter = const Text(
    "Busy day ahead? Here is how to make the most of it!",
    style: TextStyle(color: Colors.black, fontSize: 18),
    textAlign: TextAlign.center,
  );



  @override
  Widget build(BuildContext context) {
    return ListenableBuilder(
      listenable: widget.trip,
      builder: greeterBuilder,
    );
  }
}