import 'package:flutter/material.dart';
import '../models/prank_sound.dart';
import '../managers/data_manager.dart';
import '../managers/sound_manager.dart';
import '../widgets/big_player_panel.dart';

class FavoritesPage extends StatelessWidget {
  const FavoritesPage({super.key});

  final Color _favThemeColor = const Color(0xFF6C63FF);

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<List<PrankSound>>(
      valueListenable: DataManager().favoritesNotifier,
      builder: (context, favorites, child) {
        return Scaffold(
          appBar: AppBar(title: const Text('My Favorites')),
          body: Stack(
            alignment: Alignment.bottomCenter,
            children: [
              favorites.isEmpty
                  ? Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      padding: const EdgeInsets.all(30),
                      decoration: BoxDecoration(
                        color: Colors.grey[100],
                        shape: BoxShape.circle,
                      ),
                      child: Icon(Icons.favorite_border_rounded, size: 60, color: Colors.grey[300]),
                    ),
                    const SizedBox(height: 20),
                    Text(
                      'No favorites yet',
                      style: TextStyle(color: Colors.grey[400], fontSize: 16, fontWeight: FontWeight.bold),
                    ),
                  ],
                ),
              )
                  : ListView.builder(
                padding: const EdgeInsets.fromLTRB(20, 20, 20, 320), // 列表底部留白更多，防止被高位置的播放器遮挡
                itemCount: favorites.length,
                itemBuilder: (context, index) {
                  final sound = favorites[index];
                  return Dismissible(
                    key: Key(sound.title),
                    direction: DismissDirection.endToStart,
                    background: Container(
                      alignment: Alignment.centerRight,
                      padding: const EdgeInsets.only(right: 20),
                      decoration: BoxDecoration(
                        color: const Color(0xFFFF6584),
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: const Icon(Icons.delete_outline, color: Colors.white),
                    ),
                    onDismissed: (direction) {
                      DataManager().toggleFavorite(sound);
                    },
                    child: _buildFavTile(sound),
                  );
                },
              ),

              // 在收藏页，底部偏移量设为 120
              BigPlayerPanel(
                themeColor: _favThemeColor,
                bottomOffset: 120,
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildFavTile(PrankSound sound) {
    final manager = SoundManager();
    return ValueListenableBuilder<PrankSound?>(
        valueListenable: manager.currentSoundNotifier,
        builder: (context, currentSound, _) {
          final bool isActive = currentSound == sound;
          return Container(
            margin: const EdgeInsets.only(bottom: 12),
            decoration: BoxDecoration(
              color: isActive ? _favThemeColor.withOpacity(0.05) : Colors.white,
              borderRadius: BorderRadius.circular(20),
              border: isActive ? Border.all(color: _favThemeColor, width: 2) : null,
              boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.03), blurRadius: 10, offset: const Offset(0, 4))],
            ),
            child: ListTile(
              contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              leading: CircleAvatar(
                backgroundColor: _favThemeColor.withOpacity(0.1),
                child: isActive
                    ? Icon(Icons.equalizer, color: _favThemeColor, size: 20)
                    : Icon(Icons.favorite, color: _favThemeColor, size: 20),
              ),
              title: Text(sound.title, style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: isActive ? _favThemeColor : Colors.black87
              )),
              trailing: Icon(
                isActive ? Icons.pause_circle_filled : Icons.play_circle_fill,
                color: Colors.grey[300],
                size: 30,
              ),
              onTap: () => manager.play(sound),
            ),
          );
        }
    );
  }
}
