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

import '../providers/character_provider.dart';
import '../providers/device_provider.dart';
import '../providers/player_provider.dart';
import '../providers/wifi_provider.dart';
import '../theme/app_colors.dart';
import '../widgets/control_button.dart';
import '../widgets/status_card.dart';

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

  @override
  Widget build(BuildContext context) {
    final deviceProvider = context.watch<DeviceProvider>();
    final playerProvider = context.watch<PlayerProvider>();
    final wifiProvider = context.watch<WifiProvider>();
    final characterProvider = context.watch<CharacterProvider>();
    final device = deviceProvider.status;
    final player = playerProvider.status;
    final wifi = wifiProvider.status;
    final characterName = characterProvider.activePack?.name ?? '未同步';

    return Scaffold(
      appBar: AppBar(title: const Text('ShowenV2 控制台')),
      body: RefreshIndicator(
        onRefresh: () async {
          await Future.wait<void>([
            context.read<DeviceProvider>().refresh(),
            context.read<PlayerProvider>().bootstrap(),
            context.read<WifiProvider>().bootstrap(),
            context.read<CharacterProvider>().refresh(),
          ]);
        },
        child: ListView(
          padding: const EdgeInsets.all(AppSpacing.md),
          children: [
            StatusCard(
              title: '设备连接',
              value: device.connected ? '已连接' : '未连接',
              subtitle: '${device.ipAddress ?? deviceProvider.deviceIp} · ${device.connectionType.toUpperCase()}',
              icon: Icons.devices_rounded,
              accentColor: device.connected ? AppColors.success : AppColors.warning,
            ),
            const SizedBox(height: AppSpacing.md),
            StatusCard(
              title: '当前角色',
              value: characterName,
              subtitle: characterProvider.activeFilename.isEmpty
                  ? '点击角色页切换内容包'
                  : characterProvider.activeFilename,
              icon: Icons.face_rounded,
              accentColor: AppColors.secondary,
            ),
            const SizedBox(height: AppSpacing.md),
            StatusCard(
              title: '当前播放状态',
              value: player.currentVideo ?? '暂无播放视频',
              subtitle: player.running
                  ? (player.paused ? '已暂停' : '播放中')
                  : '等待播放',
              icon: Icons.play_circle_outline_rounded,
              accentColor: player.paused ? AppColors.warning : AppColors.primary,
            ),
            const SizedBox(height: AppSpacing.md),
            StatusCard(
              title: 'WiFi 摘要',
              value: wifi.connected ? (wifi.ssid ?? '已连接') : '未连接网络',
              subtitle: wifi.ip ?? '可通过热点或 BLE 配网',
              icon: Icons.wifi_rounded,
              accentColor: wifi.connected ? AppColors.info : AppColors.border,
            ),
            const SizedBox(height: AppSpacing.lg),
            Row(
              children: [
                Expanded(
                  child: ControlButton(
                    label: '去对话',
                    icon: Icons.chat_bubble_rounded,
                    onPressed: () => context.go('/chat'),
                  ),
                ),
                const SizedBox(width: AppSpacing.md),
                Expanded(
                  child: ControlButton(
                    label: '换角色',
                    icon: Icons.face_rounded,
                    isFilled: false,
                    onPressed: () => context.go('/characters'),
                  ),
                ),
              ],
            ),
            const SizedBox(height: AppSpacing.lg),
            Text('快捷控制', style: Theme.of(context).textTheme.headlineSmall),
            const SizedBox(height: AppSpacing.md),
            Row(
              children: [
                Expanded(
                  child: ControlButton(
                    label: player.running && !player.paused ? '暂停' : '播放',
                    icon: player.running && !player.paused
                        ? Icons.pause_rounded
                        : Icons.play_arrow_rounded,
                    onPressed: playerProvider.isLoading
                        ? null
                        : () => context.read<PlayerProvider>().togglePlayPause(),
                  ),
                ),
                const SizedBox(width: AppSpacing.md),
                Expanded(
                  child: ControlButton(
                    label: '上一个',
                    icon: Icons.skip_previous_rounded,
                    isFilled: false,
                    onPressed: playerProvider.isLoading
                        ? null
                        : () => context.read<PlayerProvider>().previous(),
                  ),
                ),
                const SizedBox(width: AppSpacing.md),
                Expanded(
                  child: ControlButton(
                    label: '下一个',
                    icon: Icons.skip_next_rounded,
                    isFilled: false,
                    onPressed: playerProvider.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),
                    _InfoRow(label: '设备 IP', value: device.ipAddress ?? deviceProvider.deviceIp),
                    _InfoRow(label: '连接方式', value: device.connectionType.toUpperCase()),
                    _InfoRow(
                      label: '实时通道',
                      value: deviceProvider.webSocketConnected ? 'WebSocket 已连接' : 'WebSocket 重连中',
                    ),
                    _InfoRow(label: '播放索引', value: '${player.currentIndex + 1}/${player.playlistLength}'),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _InfoRow extends StatelessWidget {
  const _InfoRow({required this.label, required this.value});

  final String label;
  final String value;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: AppSpacing.sm),
      child: Row(
        children: [
          Expanded(child: Text(label, style: Theme.of(context).textTheme.bodyMedium)),
          Text(value, style: Theme.of(context).textTheme.bodyLarge),
        ],
      ),
    );
  }
}
