import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

import '../../../common/router/app_router.dart';
import '../../../common/widgets/prototype_ui.dart';
import '../application/news_controller.dart';

class NewsPage extends ConsumerWidget {
  const NewsPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final news = ref.watch(newsControllerProvider);
    final cs = ShadTheme.of(context).colorScheme;
    return SafeArea(
      top: false,
      child: ListView(
        padding: const EdgeInsets.fromLTRB(0, 8, 0, 86),
        children: [
          PrototypeFrame(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Row(
                  children: [
                    const Text(
                      '资讯',
                      style: TextStyle(fontSize: 17, fontWeight: FontWeight.w600),
                    ),
                    const Spacer(),
                    IconButton(
                      onPressed: () => ref.read(newsControllerProvider.notifier).refresh(),
                      icon: const Icon(Icons.refresh, size: 19),
                      color: const Color(0xFF9A968D),
                      padding: EdgeInsets.zero,
                      constraints: const BoxConstraints.tightFor(
                        width: 30,
                        height: 30,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 2),
                Text(
                  '信息整理 · 非投资建议',
                  style: TextStyle(fontSize: 13, color: cs.mutedForeground),
                ),
                const SizedBox(height: 10),
                for (final item in news.items) ...[
                  PrototypeCard(
                    margin: const EdgeInsets.only(bottom: 10),
                    child: InkWell(
                      onTap: () => context.push('${AppRoutes.newsDetail}?id=${item.id}'),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Container(
                                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
                                decoration: BoxDecoration(
                                  color: item.metal.color.withValues(alpha: 0.12),
                                  borderRadius: BorderRadius.circular(999),
                                ),
                                child: Text(
                                  item.metal.shortLabel,
                                  style: TextStyle(
                                    color: item.metal.color,
                                    fontWeight: FontWeight.w700,
                                    fontSize: 12,
                                  ),
                                ),
                              ),
                              const SizedBox(width: 8),
                              Text(
                                '${item.source} · ${_timeLabel(item.publishedAt)}',
                                style: TextStyle(fontSize: 12, color: cs.mutedForeground),
                              ),
                              const Spacer(),
                              Icon(Icons.chevron_right, size: 16, color: cs.mutedForeground),
                            ],
                          ),
                          const SizedBox(height: 10),
                          Text(
                            item.title,
                            style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w700),
                          ),
                          const SizedBox(height: 8),
                          Text(
                            item.summary,
                            style: TextStyle(
                              fontSize: 14,
                              color: cs.mutedForeground,
                              height: 1.55,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ],
            ),
          ),
        ],
      ),
    );
  }
}

String _timeLabel(DateTime value) {
  return '${value.hour.toString().padLeft(2, '0')}:${value.minute.toString().padLeft(2, '0')}';
}
