import 'dart:typed_data';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:get/get.dart';
import 'package:terepi_seged/models/seismic_records.dart';
import 'package:terepi_seged/pages/measured_data/presentation/views/seismogram_viewer_dialog.dart';

class MeasuredDataController extends GetxController {
  final CollectionReference _seismicRecords =
      FirebaseFirestore.instance.collection('seismicRecords');

  // late Query<Map<String, dynamic>> records = _seismicRecords.orderBy('field');
  late Stream<QuerySnapshot> seismicRecordSnapshot;

  @override
  void onReady() async {
    super.onReady();
  }

  @override
  void onInit() {
    super.onInit();

    seismicRecordSnapshot = _seismicRecords
        // .orderBy('sourcePoint', descending: true)
        .where('gpsDateTime', isGreaterThan: DateTime(2024, 2, 25, 0, 0, 0))
        .orderBy('gpsDateTime', descending: true)
        .snapshots();
  }

  @override
  void onClose() async {
    super.onClose();

    // seismicRecordSnapshot.
  }

  void showSeismicImage(int ffid) async {
    late Uint8List image;
    Reference reference =
        FirebaseStorage.instance.ref().child('bp-4/${ffid}.jpg');

    await reference
        .getData()
        .then((value) => {image = value!})
        .catchError((error) => {});

    Get.to(
        () => SeismogramViewerDialog(
              imageUrl: '',
              image: image,
            ),
        transition: Transition.downToUp);
  }
}
