import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:get/get.dart';
import 'package:terepi_seged/pages/measured_data/presentation/controllers/measured_data_controller.dart';
import 'package:terepi_seged/pages/measured_data/presentation/views/seismogram_viewer_dialog.dart';

class MeasuredDataView extends GetView<MeasuredDataController> {
  const MeasuredDataView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        extendBody: true,
        appBar: AppBar(
          title: const Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('Mérési adatok'),
              Text(
                "Budapest",
                style: TextStyle(fontSize: 12.0),
              )
            ],
          ),
          actions: [
            Padding(
              padding: const EdgeInsets.only(right: 20.0),
              child: GestureDetector(
                onTap: () => Get.to(() => {}, transition: Transition.downToUp),
                child: const Icon(
                  Icons.more_vert,
                  size: 26.0,
                ),
              ),
            )
          ],
        ),
        body: Column(children: [
          Text("Mérési adatok"),
          StreamBuilder<QuerySnapshot>(
            stream: controller.seismicRecordSnapshot,
            builder:
                (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
              if (snapshot.hasError) {
                return const Text('Hiba ...');
              }
              if (snapshot.connectionState == ConnectionState.waiting) {
                return const Text('Adatbetöltés ...');
              }
              return Expanded(
                child: ListView(
                  // shrinkWrap: true,
                  children: snapshot.data!.docs
                      .map((DocumentSnapshot document) {
                        Map<String, dynamic> data =
                            document.data()! as Map<String, dynamic>;
                        return ListTile(
                            title: Text(data['sourcePoint'].toString()),
                            subtitle: Text(
                                '${data['sourceLine'].toString()} - ${(data['gpsDateTime'] as Timestamp).toDate()}'),
                            trailing: Wrap(spacing: -16, children: [
                              IconButton(
                                  onPressed: () {
                                    controller.showSeismicImage(data['ffid']);
                                  },
                                  icon: const Icon(Icons.image_outlined)),
                              IconButton(
                                  onPressed: () {},
                                  icon:
                                      const Icon(Icons.arrow_forward_ios_sharp))
                            ]));
                      })
                      .toList()
                      .cast(),
                ),
              );
            },
          )
        ]));
  }
}
