import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:forui/forui.dart';
import 'package:intl/intl.dart';
import 'package:xiao_pet_tracker/app_router/app_router.gr.dart';
import 'package:xiao_pet_tracker/xiao_connector/cubit/xiao_connector_cubit.dart';
import 'package:xiao_pet_tracker/xiao_connector/models/capture_box.dart';

@RoutePage()
class RecordingsPage extends StatelessWidget {
  const RecordingsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return const RecordingsView();
  }
}

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

  @override
  State<RecordingsView> createState() => _SettingsViewState();
}

class _SettingsViewState extends State<RecordingsView> {
  List<CaptureBox> _captureBoxes = [];
  final f = DateFormat('hh:mm - dd.MM.yyyy ');

  @override
  void initState() {
    _captureBoxes = context.read<XiaoConnectorCubit>().getAllCaptureBoxes();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Recordings'),
      ),
      body: RefreshIndicator(
        onRefresh: _pullRefresh,
        child: ListView.builder(
          itemCount: _captureBoxes.length,
          itemBuilder: (context, i) {
            return ListTile(
              leading: FIcon(FAssets.icons.pawPrint),
              title: Text('Collection Type: ${_captureBoxes[i].type}'),
              subtitle: Text(f.format(_captureBoxes[i].startTime)),
              onTap: () {
                AutoRouter.of(context).push(
                  RecordingsDetailsRoute(
                    type: _captureBoxes[i].type,
                    uuid: _captureBoxes[i].uuid,
                  ),
                );
              },
              onLongPress: () {
                showDialog(
                  context: context,
                  builder: (context) {
                    return AlertDialog(
                      title: const Text('Delete this recording?'),
                      content: const Text(
                        "Are you sure you want to delete this recording with all of it's data? This cannot be undone.",
                        textAlign: TextAlign.center,
                      ),
                      actions: [
                        TextButton(
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                          child: const Text('Cancel'),
                        ),
                        TextButton(
                          onPressed: () {
                            context
                                .read<XiaoConnectorCubit>()
                                .deleteRecording(_captureBoxes[i].uuid);
                            _pullRefresh();
                            Navigator.of(context).pop();
                          },
                          child: const Text('Delete'),
                        ),
                      ],
                    );
                  },
                );
              },
            );
          },
        ),
      ),
    );
  }

  Future<void> _pullRefresh() async {
    _captureBoxes = context.read<XiaoConnectorCubit>().getAllCaptureBoxes();
    setState(() {});
  }
}
