import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import 'package:go_router/go_router.dart';

import '../../../core/l10n/app_localizations.dart';
import '../../../providers/help_center_provider.dart';

/// 协议详情页面
/// 路由: /protocol，通过 GoRouterState.extra 传入 ProtocolArgs。
class ProtocolScreen extends ConsumerWidget {
  const ProtocolScreen({
    super.key,
    required this.title,
    this.categoryCode = 'PROTOCOL',
  });

  /// 协议标题，用于在列表中匹配对应条目（如 "服务条款"）；
  /// 若为空则显示该分类下的第一条内容。
  final String title;

  /// CMS 分类码：'PROTOCOL'（注册协议）或 'FOLLOW_PROTOCOL'（交易员协议）
  final String categoryCode;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final cs = Theme.of(context).colorScheme;
    final asyncData = ref.watch(protocolByTitleProvider((categoryCode, title)));

    // AppBar 标题：有明确 title 时用 title，否则加载后用文章标题
    final appBarTitle = title.isNotEmpty
        ? title
        : asyncData.valueOrNull?.title ?? AppLocalizations.of(context)!.protocolDetail;

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.chevron_left, size: 28),
          onPressed: () => context.pop(),
        ),
        title: Text(
          appBarTitle,
          style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w600),
        ),
        centerTitle: true,
      ),
      body: asyncData.when(
        loading: () => const Center(child: CircularProgressIndicator()),
        error: (_, __) => Center(
          child: Text(AppLocalizations.of(context)!.loadFailed, style: TextStyle(color: cs.onSurface.withAlpha(153))),
        ),
        data: (item) {
          if (item == null || item.content.isEmpty) {
            return Center(
              child: Text(AppLocalizations.of(context)!.noContent, style: TextStyle(color: cs.onSurface.withAlpha(153))),
            );
          }
          return SingleChildScrollView(
            padding: const EdgeInsets.all(16),
            child: HtmlWidget(
              item.content,
              textStyle: TextStyle(
                color: cs.onSurface,
                fontSize: 14,
                height: 1.8,
              ),
            ),
          );
        },
      ),
    );
  }
}

/// 路由 extra 参数
class ProtocolArgs {
  const ProtocolArgs({required this.title, this.categoryCode = 'PROTOCOL'});
  final String title;
  final String categoryCode;
}
