import 'package:fforte/addCam/add_cam_main.dart';
import 'package:fforte/db_helper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_popup_card/flutter_popup_card.dart';
// * Site that shows all entries in the databases

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

  @override
  State<ViewCams> createState() => _ViewCamsState();
}

class _ViewCamsState extends State<ViewCams> {
  // var declaration
  late Future<List<Map<String, dynamic>>> place;
  late Future<List<Map<String, dynamic>>> templates;

  // loads the entries
  @override
  void initState() {
    super.initState();
    place = DBHelper().getPlace();
    templates = DBHelper().getTemplates();
  }

  // functions to delete all entries LOCALLY
  void delAllPlaces() async {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text(AppLocalizations.of(context)!.deleteEverything),
            content: SingleChildScrollView(
              child: ListBody(children: <Widget>[
                Text(AppLocalizations.of(context)!.deleteEverythingContent)
              ]),
            ),
            actions: <Widget>[
              TextButton(
                  onPressed: () {
                    var placeDB = DBHelper();
                    placeDB.deleteAllPlaces();
                    setState(() {
                      place = DBHelper().getPlace();
                    });

                    Navigator.of(context).pop();
                  },
                  child: Text(AppLocalizations.of(context)!.deleteEverything)),
              TextButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: Text(AppLocalizations.of(context)!.cancel))
            ],
          );
        });
  }

  void delAllTemplates() async {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text(AppLocalizations.of(context)!.deleteEverything),
            content: SingleChildScrollView(
              child: ListBody(children: <Widget>[
                Text(AppLocalizations.of(context)!.deleteEverythingContent)
              ]),
            ),
            actions: <Widget>[
              TextButton(
                  onPressed: () {
                    var placeDB = DBHelper();
                    placeDB.deleteAllTemplates();
                    setState(() {
                      templates = DBHelper().getTemplates();
                    });
                    Navigator.of(context).pop();
                  },
                  child: Text(AppLocalizations.of(context)!.deleteEverything)),
              TextButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: Text(AppLocalizations.of(context)!.cancel))
            ],
          );
        });
  }

  // The widet tree with taps to differentiate between templates and finished entries
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
          appBar: AppBar(
              bottom: TabBar(tabs: [
                Tab(text: AppLocalizations.of(context)!.completed),
                Tab(
                  text: AppLocalizations.of(context)!.uncompleted,
                )
              ]),
              title: Text(AppLocalizations.of(context)!.viewplacesappbar)),
          body: TabBarView(
            children: [
              FutureBuilder<List<Map<String, dynamic>>>(
                future: place,
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return const CircularProgressIndicator();
                  } else if (snapshot.hasError) {
                    return Text('Error: ${snapshot.error}');
                  } else {
                    return Scaffold(
                      floatingActionButton: FloatingActionButton(
                        child: const Icon(Icons.delete),
                        onPressed: () {
                          delAllPlaces();
                        },
                      ),
                      body: Column(
                        children: [
                          Expanded(
                              child: ListView.builder(
                            itemCount: snapshot.data!.length,
                            itemBuilder: (context, index) {
                              Map<String, dynamic> place =
                                  snapshot.data![index];
                              return ListTile(
                                title: Text('Standort ${index + 1}'),
                                subtitle: Text(
                                    'ID: ${place['CID']} DATUM: ${place['Datum']}'),
                                trailing: Checkbox(
                                  value: place['Sent'] == 0 ? false : true,
                                  onChanged: null,
                                ),
                                onTap: () async {
                                  showPopupCard(
                                      context: context,
                                      builder: (context) {
                                        return PopupCard(
                                          color: Theme.of(context).cardColor,
                                          shape: RoundedRectangleBorder(
                                              borderRadius:
                                                  BorderRadius.circular(12)),

                                            child: Container(
                                              
                                              decoration:
                                                  BoxDecoration(
                                                    borderRadius: BorderRadius.circular(12),
                                                    color: Theme.of(context).cardColor,
                                                    boxShadow: [
                                                BoxShadow(
                                                    color: Theme.of(context).primaryColor,
                                                    spreadRadius: 7,
                                                    blurRadius: 7,
                                                    offset: const Offset(0, 3))
                                              ]),
                                              child: Padding(
                                                padding: const EdgeInsets.all(8.0),
                                                child: SingleChildScrollView(
                                                  child: Column(
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment.start,
                                                    children: [
                                                      Text(
                                                          '${AppLocalizations.of(context)!.date}: ${place['Datum']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.rudel}: ${place['Rudel']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.fftyp}: ${place['Datum']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.bearsafe}: ${place['Bearsafe']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.status}: ${place['Status']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.sttyp}: ${place['STTyp']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.foto}/${AppLocalizations.of(context)!.film}: ${place['FotoFilm']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.kschlonr}: ${place['KSchloNr']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.ktage1}: ${place['KTage1']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.ktage2}: ${place['KTage2']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.pickkontdat}: ${place['KontDat']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.pickabbaudat}: ${place['AbbauDat']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.ausvon}: ${place['AusVon']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.ausbis}: ${place['AusBis']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.sommerzeit}: ${place['MEZ']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.betreuung}: ${place['Betreuung']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.locations}: ${place['Standort']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.adresse3}: ${place['Adresse3']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.bland}: ${place['BLand']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.lkr}: ${place['Lkr']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.beiort}: ${place['BeiOrt']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.platzung}: ${place['Platzung']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.altstort}: ${place['AltStOrt']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.adresse1}: ${place['Adresse1']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.adresse2}: ${place['Adresse2']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.auftrag}: ${place['Auftrag']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.kontabsp}: ${place['KontAbsp']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.sonstbemerkungen}: ${place['SonstBem']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.fkontakt1}: ${place['FKontakt1']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.fkontakt2}: ${place['FKontakt2']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.fkontakt3}: ${place['FKontakt3']}'),
                                                      Text(
                                                          '${AppLocalizations.of(context)!.intkomm}: ${place['IntKomm']}'),
                                                    ],
                                                  ),
                                                ),
                                              ),
                                            ),
                                          
                                        );
                                      });
                                },
                              );
                            },
                          ))
                        ],
                      ),
                    );
                  }
                },
              ),
              FutureBuilder<List<Map<String, dynamic>>>(
                  future: templates,
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return const CircularProgressIndicator();
                    } else if (snapshot.hasError) {
                      return Text('Error: ${snapshot.error}');
                    } else {
                      return Scaffold(
                        floatingActionButton: FloatingActionButton(
                          child: const Icon(Icons.delete),
                          onPressed: () {
                            delAllTemplates();
                          },
                        ),
                        body: Column(
                          children: [
                            Expanded(
                              child: ListView.builder(
                                  itemCount: snapshot.data!.length,
                                  itemBuilder: (context, index) {
                                    Map<String, dynamic> templates =
                                        snapshot.data![index];
                                    return ListTile(
                                      onTap: () {
                                        Navigator.push(
                                            context,
                                            MaterialPageRoute(
                                                builder: (context) =>
                                                    AddCamMain(
                                                      isTemplate: true,
                                                      templateData: templates,
                                                    )));
                                      },
                                      title: Text('Place ${index + 1}'),
                                      subtitle: Text(
                                          'ID: ${templates['CID']} DATUM: ${templates['Datum']} RUDEL: ${templates['Rudel']} STATUS: ${templates['Status']}'),
                                    );
                                  }),
                            )
                          ],
                        ),
                      );
                    }
                  })
            ],
          )),
    );
  }
}
