import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:aves/widgets/collection/album_controller.dart';
import 'package:aves/widgets/collection/album_sort.dart';

/// Pagina per selezionare un album (basata su AlbumRow)
class AlbumPickPage extends StatelessWidget {
  const AlbumPickPage({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = context.watch<AlbumController>();

    if (controller.loading) {
      return const Scaffold(
        body: Center(child: CircularProgressIndicator()),
      );
    }

    final pinned = controller.pinnedAlbums;
    final normal = controller.normalAlbums;

    return Scaffold(
      appBar: AppBar(
        title: const Text('Seleziona album'),
      ),
      body: ListView(
        children: [
          if (pinned.isNotEmpty)
            const _SectionHeader(title: 'Pinned'),

          ...pinned.map(
            (album) => _AlbumPickTile(
              album: album,
              onTap: () => Navigator.pop(context, album),
            ),
          ),

          if (normal.isNotEmpty)
            const _SectionHeader(title: 'Albums'),

          ...normal.map(
            (album) => _AlbumPickTile(
              album: album,
              onTap: () => Navigator.pop(context, album),
            ),
          ),
        ],
      ),
    );
  }
}

class _AlbumPickTile extends StatelessWidget {
  final AlbumRow album;
  final VoidCallback onTap;

  const _AlbumPickTile({
    required this.album,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: const Icon(Icons.folder, color: Colors.white70),
      title: Text(album.displayName),
      subtitle: Text('${album.fileCount} file'),
      onTap: onTap,
    );
  }
}

class _SectionHeader extends StatelessWidget {
  final String title;

  const _SectionHeader({required this.title});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.fromLTRB(16, 12, 16, 6),
      child: Text(
        title,
        style: const TextStyle(
          color: Colors.white70,
          fontSize: 14,
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }
}
