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

import '../providers/player_provider.dart';
import '../theme/app_colors.dart';
import '../widgets/control_button.dart';

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

  @override
  State<PlaybackScreen> createState() => _PlaybackScreenState();
}

class _PlaybackScreenState extends State<PlaybackScreen> {
  final TextEditingController _indexController = TextEditingController();

  @override
  void dispose() {
    _indexController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final provider = context.watch<PlayerProvider>();
    final status = provider.status;
    final playlist = provider.playlist;

    return Scaffold(
      appBar: AppBar(title: const Text('播放控制')),
      body: RefreshIndicator(
        onRefresh: _handleRefresh,
        child: ListView(
          physics: const AlwaysScrollableScrollPhysics(),
          padding: const EdgeInsets.all(AppSpacing.md),
          children: [
          Card(
            child: Padding(
              padding: const EdgeInsets.all(AppSpacing.lg),
              child: Column(
                children: [
                  Text(
                    status.currentVideo ?? '暂无播放内容',
                    textAlign: TextAlign.center,
                    style: Theme.of(context).textTheme.headlineSmall,
                  ),
                  const SizedBox(height: AppSpacing.lg),
                  SizedBox(
                    width: 132,
                    height: 132,
                    child: DecoratedBox(
                      decoration: const BoxDecoration(
                        shape: BoxShape.circle,
                        gradient: AppColors.primaryGradient,
                      ),
                      child: IconButton(
                        onPressed: provider.isLoading
                            ? null
                            : () => context.read<PlayerProvider>().togglePlayPause(),
                        iconSize: 56,
                        color: Colors.white,
                        icon: Icon(
                          status.running && !status.paused
                              ? Icons.pause_rounded
                              : Icons.play_arrow_rounded,
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(height: AppSpacing.md),
                  Text(
                    status.running ? (status.paused ? '已暂停' : '播放中') : '未开始播放',
                    style: Theme.of(context).textTheme.bodyMedium,
                  ),
                ],
              ),
            ),
          ),
          const SizedBox(height: AppSpacing.md),
          Row(
            children: [
              Expanded(
                child: ControlButton(
                  label: '上一个',
                  icon: Icons.skip_previous_rounded,
                  isFilled: false,
                  onPressed: provider.isLoading
                      ? null
                      : () => context.read<PlayerProvider>().previous(),
                ),
              ),
              const SizedBox(width: AppSpacing.md),
              Expanded(
                child: ControlButton(
                  label: '下一个',
                  icon: Icons.skip_next_rounded,
                  isFilled: false,
                  onPressed: provider.isLoading
                      ? null
                      : () => context.read<PlayerProvider>().next(),
                ),
              ),
            ],
          ),
          const SizedBox(height: AppSpacing.lg),
          Card(
            child: Padding(
              padding: const EdgeInsets.all(AppSpacing.md),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('跳转到指定索引', style: Theme.of(context).textTheme.titleMedium),
                  const SizedBox(height: AppSpacing.md),
                  Row(
                    children: [
                      Expanded(
                        child: TextField(
                          controller: _indexController,
                          keyboardType: TextInputType.number,
                          decoration: const InputDecoration(
                            labelText: '输入 0 开始的索引',
                          ),
                        ),
                      ),
                      const SizedBox(width: AppSpacing.md),
                      FilledButton(
                        onPressed: provider.isLoading ? null : _handleGoto,
                        child: const Text('跳转'),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
          const SizedBox(height: AppSpacing.lg),
          Text('播放列表', style: Theme.of(context).textTheme.headlineSmall),
          const SizedBox(height: AppSpacing.md),
          if (playlist.isEmpty)
            const Card(
              child: Padding(
                padding: EdgeInsets.all(AppSpacing.md),
                child: Text('当前没有可播放视频'),
              ),
            ),
          ...playlist.asMap().entries.map((entry) {
            final selected = entry.key == status.currentIndex;
            return Padding(
              padding: const EdgeInsets.only(bottom: AppSpacing.sm),
              child: Card(
                color: selected ? AppColors.primary.withValues(alpha: 0.16) : null,
                child: ListTile(
                  onTap: provider.isLoading
                      ? null
                      : () => context.read<PlayerProvider>().gotoIndex(entry.key),
                  leading: CircleAvatar(
                    backgroundColor: selected ? AppColors.primary : AppColors.border,
                    child: Text('${entry.key + 1}'),
                  ),
                  title: Text(entry.value),
                  subtitle: Text(selected ? '当前播放' : '点击跳转'),
                  trailing: Icon(
                    selected ? Icons.equalizer_rounded : Icons.chevron_right_rounded,
                  ),
                ),
              ),
            );
          }),
          ],
        ),
      ),
    );
  }

  Future<void> _handleRefresh() async {
    final provider = context.read<PlayerProvider>();
    await Future.wait<void>([
      provider.fetchStatus(),
      provider.fetchPlaylist(),
    ]);
  }

  void _handleGoto() {
    final index = int.tryParse(_indexController.text.trim());
    if (index == null) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('请输入有效索引')),
      );
      return;
    }
    context.read<PlayerProvider>().gotoIndex(index);
  }
}
