// 内容
import 'package:PiliPlus/common/widgets/custom_icon.dart';
import 'package:PiliPlus/common/widgets/flutter/text/text.dart' as custom_text;
import 'package:PiliPlus/common/widgets/image_grid/image_grid_view.dart';
import 'package:PiliPlus/models/dynamics/result.dart';
import 'package:PiliPlus/pages/dynamics/widgets/rich_node_panel.dart';
import 'package:PiliPlus/utils/page_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

Widget content(
  BuildContext context, {
  required int floor,
  required ThemeData theme,
  required DynamicItemModel item,
  required bool isSave,
  required bool isDetail,
}) {
  TextSpan? richNodes = richNode(
    context,
    theme: theme,
    item: item,
  );
  final moduleDynamic = item.modules.moduleDynamic;
  final pics = moduleDynamic?.major?.opus?.pics;
  final text =
      moduleDynamic?.desc?.text ?? moduleDynamic?.major?.opus?.summary?.text;
  return Padding(
    padding: floor == 1
        ? const EdgeInsets.fromLTRB(12, 0, 12, 6)
        : const EdgeInsets.only(bottom: 6),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (moduleDynamic?.topic case final topic?)
          GestureDetector(
            onTap: () => Get.toNamed(
              '/dynTopic',
              parameters: {
                'id': topic.id!.toString(),
                'name': topic.name!,
              },
            ),
            child: Text.rich(
              TextSpan(
                children: [
                  WidgetSpan(
                    alignment: PlaceholderAlignment.middle,
                    child: Padding(
                      padding: const EdgeInsets.only(right: 4),
                      child: Icon(
                        size: 18,
                        CustomIcons.topic_tag,
                        color: theme.colorScheme.primary,
                      ),
                    ),
                  ),
                  TextSpan(text: topic.name),
                ],
              ),
              style: TextStyle(
                fontSize: floor != 1
                    ? 14
                    : isDetail && !isSave
                    ? 16
                    : 15,
                color: theme.colorScheme.primary,
              ),
            ),
          ),
        if (richNodes != null)
          isDetail && floor == 1
              ? SelectableText.rich(
                  richNodes,
                  style: isSave
                      ? const TextStyle(fontSize: 15)
                      : const TextStyle(fontSize: 16),
                  contextMenuBuilder: text == null || text.isEmpty
                      ? null
                      : (_, state) => _contextMenuBuilder(state, text),
                )
              : custom_text.Text.rich(
                  style: floor == 1
                      ? const TextStyle(fontSize: 15)
                      : const TextStyle(fontSize: 14),
                  richNodes,
                  maxLines: isSave ? null : 6,
                  onShowMore: () => PageUtils.pushDynDetail(item, isPush: true),
                  primary: theme.colorScheme.primary,
                ),
        if (pics != null && pics.isNotEmpty)
          ImageGridView(
            fullScreen: true,
            picArr: pics
                .map(
                  (item) => ImageModel(
                    width: item.width,
                    height: item.height,
                    url: item.url ?? '',
                    liveUrl: item.liveUrl,
                  ),
                )
                .toList(),
          ),
      ],
    ),
  );
}

Widget _contextMenuBuilder(EditableTextState state, String text) {
  return AdaptiveTextSelectionToolbar.buttonItems(
    buttonItems: state.contextMenuButtonItems
      ..add(
        ContextMenuButtonItem(label: '文本', onPressed: () => _onCopyText(text)),
      ),
    anchors: state.contextMenuAnchors,
  );
}

void _onCopyText(String text) {
  showDialog(
    context: Get.context!,
    builder: (context) => Dialog(
      child: Padding(
        padding: const .symmetric(horizontal: 20, vertical: 16),
        child: SelectableText(
          text,
          style: const TextStyle(fontSize: 15, height: 1.7),
        ),
      ),
    ),
  );
}
