import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:terepi_seged/pages/tracking/presentation/controllers/tracking_controller.dart';

import 'handle.dart';
import 'live_stat_panel.dart';
import 'section_header.dart';
import 'track_list_item.dart';

class TrackingSheet extends StatelessWidget {
  const TrackingSheet();

  @override
  Widget build(BuildContext context) {
    return DraggableScrollableSheet(
      initialChildSize: 0.35, // kezdetben kis méret
      minChildSize: 0.2,
      maxChildSize: 0.85, // felfelé húzva nagy lista
      snap: true,
      snapSizes: const [0.2, 0.35, 0.85],
      builder: (_, scrollCtrl) => Container(
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.surface,
          borderRadius: const BorderRadius.vertical(top: Radius.circular(16)),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.2),
              blurRadius: 12,
              offset: const Offset(0, -4),
            ),
          ],
        ),
        child: CustomScrollView(
          controller: scrollCtrl,
          slivers: [
            // Handle
            SliverToBoxAdapter(child: Handle()),

            // Élő statisztika panel
            SliverToBoxAdapter(
              child: LiveStatsPanel(),
            ),

            // Mentett track-ek fejléc
            SliverToBoxAdapter(
              child: SectionHeader(
                title: 'Korábbi útvonalak',
                trailing: TextButton(
                  onPressed: TrackingController.to.loadSavedTracks,
                  child: const Text('Frissítés'),
                ),
              ),
            ),

            // Track lista
            Obx(() => SliverList.separated(
                  itemCount: TrackingController.to.savedTracks.length,
                  separatorBuilder: (_, __) => const Divider(height: 1),
                  itemBuilder: (_, i) => TrackListItem(
                    track: TrackingController.to.savedTracks[i],
                  ),
                )),

            const SliverPadding(padding: EdgeInsets.only(bottom: 20)),
          ],
        ),
      ),
    );
  }
}
