import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:terepi_seged/pages/map/presentation/controllers/map_controller.dart';

class MeasuredPointsTableDialog extends StatelessWidget {
  final controller = Get.find<MapViewController>();
  MeasuredPointsTableDialog({super.key});

  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.only(top: 20.0),
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(children: [
                    IconButton(
                        onPressed: () {
                          Get.back();
                        },
                        icon: const Icon(Icons.close)),
                    SizedBox(
                      width: 10,
                    ),
                    IconButton(
                        onPressed: () {
                          controller.SaveMeasuredPointsToFile();
                        },
                        icon: const Icon(Icons.save)),
                  ]),
                  TextButton(
                      style: ButtonStyle(
                          overlayColor:
                              MaterialStateProperty.all(Colors.transparent)),
                      onPressed: () {
                        Get.back();
                      },
                      child: const Text(
                        'Bezár',
                        style: TextStyle(color: Colors.blue, fontSize: 14.0),
                      ))
                ],
              ),
            ),
            const Padding(
              padding: EdgeInsets.symmetric(horizontal: 40.0),
              child: Text(
                'Bemért pontok',
                style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
              ),
            ),
            const SizedBox(height: 5),
            Expanded(
              child: FutureBuilder<List<dynamic>>(
                future: controller.readMeasuredPoints(),
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return Center(child: CircularProgressIndicator());
                  }

                  if (snapshot.hasError) {
                    return Center(
                      child: Text(
                        snapshot.error.toString(),
                      ),
                    );
                  }
                  if (!snapshot.hasData) {
                    return const Center(
                        child: Text("No Data available.\n Create new Data"));
                  }
                  // print(snapshot.data);
                  // return const Center(child: Text("Data available."));
                  return ListView.builder(
                      itemCount: snapshot.data!.length,
                      shrinkWrap: true,
                      itemBuilder: (context, int index) {
                        var data = snapshot.data![index];
                        print("snapshot data:");
                        print(data);
                        return ListTile(
                            leading: CircleAvatar(
                                backgroundColor: const Color(0xff764abc),
                                child: Text((index + 1).toString())),
                            title: Text(data['pointNumber'].toString(),
                                style: TextStyle(fontWeight: FontWeight.bold)),
                            subtitle: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(data['description'],
                                    style: TextStyle(
                                        fontStyle: FontStyle.italic,
                                        color: Colors.grey.shade700)),
                                Text(
                                    "EovX: ${controller.formatEov.format(data['eovY'])} - EovY: ${controller.formatEov.format(data['eovX'])}",
                                    style: TextStyle(
                                        fontStyle: FontStyle.italic,
                                        color: Colors.grey.shade400)),
                                Text(
                                    "EovZ: ${controller.formatEovZ.format(data['altitude'] - data['poleHeight'])} (m)",
                                    style: TextStyle(
                                        fontStyle: FontStyle.italic,
                                        color: Colors.grey.shade400)),
                                Text(
                                    "H.hiba: ${controller.formatAltitudeError.format(data['horizontalError'])} (m) - V.hiba: ${controller.formatAltitudeError.format(data['verticalError'])} (m)",
                                    style: TextStyle(
                                        fontStyle: FontStyle.italic,
                                        color: Colors.grey.shade400)),
                              ],
                            ));
                      });
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
