import 'dart:io';

import 'package:flutter/material.dart';
import 'package:rogapp/model/destination.dart';
import 'package:rogapp/utils/database_helper.dart';

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

  @override
  State<HistoryPage> createState() => _HistoryPageState();
}

class _HistoryPageState extends State<HistoryPage> {
  DatabaseHelper db = DatabaseHelper.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("History"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            FutureBuilder(
                future: db.getDestinations(),
                builder: (BuildContext context,
                    AsyncSnapshot<List<Destination>> snapshot) {
                  if (snapshot.connectionState == ConnectionState.done) {
                    if (snapshot.hasError) {
                      return Center(
                        child: Text(
                          '${snapshot.error} occurred',
                          style: TextStyle(fontSize: 18),
                        ),
                      );
                    } else if (snapshot.hasData) {
                      final dests = snapshot.data;
                      if (dests!.length > 0) {
                        return Center(
                          child: ListView.builder(itemBuilder:(ctx, index){
                            return ListTile(
                              title: Text(dests[index].name?? ""),
                              subtitle: Text(dests[index].address ?? ""),
                              leading: dests[0].photos != null ? Image.file(File(dests[0].photos!)) : Container(),
                            );
                          }),
                        );
                      } else {
                        return Center(child: Text("No checkin yet"));
                      }
                    }
                  }
                  else if(snapshot.connectionState == ConnectionState.waiting){
                    return Center(child: CircularProgressIndicator(),);
                  }
                  return Container();
                }),
          ],
        ),
      ),
    );
  }
}
