import 'package:flutter/material.dart';
import 'package:project_manager/utils/api/deliveries.dart';

import '../main.dart';
import '../utils/dataObjects/delivery.dart';

class DeliveryCard extends StatefulWidget {
  final String id;

  const DeliveryCard({Key? key, required this.id}) : super(key: key);

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

class _DeliveryCardState extends State<DeliveryCard> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Details for Delivery ID: ${widget.id}')),
        body: RenderDelivery(id: widget.id), // Pass widget.id to RenderDelivery
      ),
    );
  }
}

class RenderDelivery extends StatelessWidget {
  final String id; // Accept id as a parameter

  const RenderDelivery({Key? key, required this.id}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Future<Delivery> futureDelivery = fetchDelivery(id); // Use id here
    return Center(
      child: FutureBuilder<Delivery>(
        future: futureDelivery,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const CircularProgressIndicator();
          } else if (snapshot.hasData) {
            final delivery = snapshot.data!;
            return buildDelivery(delivery, context);
          } else {
            return const Text("Error retrieving data.");
          }
        },
      ),
    );
  }
}

Widget buildDelivery(Delivery delivery, BuildContext context) {
  return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Column(
            crossAxisAlignment: CrossAxisAlignment.start, // Align items to the left
            children: <Widget>[
              const Text("Items in delivery:", style: TextStyle(fontWeight: FontWeight.bold)),
              Text(delivery.itemName),
              const SizedBox(height: 20),
              const Text("Client Information", style: TextStyle(fontWeight: FontWeight.bold)),
              Text("${delivery.recipientName}, ${delivery.recipientPhone}"),
              Text("${delivery.addressLine1}, ${delivery.addressLine2}"),
              Text("${delivery.recipientCity}, ${delivery.recipientCountry} ${delivery.recipientZipCode}"),
              const SizedBox(height: 20),
              Text("Delivery Status: ${delivery.deliveryStatus.name}"),
              Text("Delivery Type: ${delivery.deliveryType.name}"),
              Text("Service Type: ${delivery.serviceType.name}"),
              const SizedBox(height: 8),
              TextButton(
                child: const Text('Leave'),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => MyDeliveryApp()),
                  );
                },
              ),
            ],
          ),
        ],
      ),
  );


}
