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: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';
import 'package:xiao_pet_tracker/xiao_connector/view/xiao_connector_page.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 = [];

  @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('Uuid: ${_captureBoxes[i].uuid}'),
              onTap: () {
                AutoRouter.of(context).push(
                  RecordingsDetailsRoute(
                    type: _captureBoxes[i].type,
                    uuid: _captureBoxes[i].uuid,
                  ),
                );
              },
            );
          },
        ),
      ),
    );
  }

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