import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/configs/app_size.dart';
import 'package:chat/controllers/moment_controller.dart';
import 'package:chat/models/moment/moment_model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MomentListItemReplay extends StatelessWidget {
  final int? maxDisplayCount;
  final int? maxLine;
  final int index;
  final MomentItemModel item;
  final void Function(Comment value)? reply;
  const MomentListItemReplay({
    Key? key,
    required this.index,
    this.maxDisplayCount,
    this.reply,
    this.maxLine,
    required this.item,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final listLength = item.comments?.length ?? 0;
    return Padding(
      padding: const EdgeInsets.symmetric(
          horizontal: AppSize.horizontalLargePadding),
      child: DecoratedBox(
        decoration: BoxDecoration(
          color: AppColors.unactive.withOpacity(0.1),
          borderRadius: const BorderRadius.all(Radius.circular(4)),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ListView.separated(
              padding: const EdgeInsets.symmetric(
                horizontal: AppSize.horizontalPadding,
              ),
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount:
                  listLength > 3 ? maxDisplayCount ?? listLength : listLength,
              itemBuilder: (context, index) {
                final comment = item.comments![index];
                return GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    if (comment.isMe) return;
                    reply?.call(comment);
                  },
                  onLongPress: () async {
                    if (comment.isMe) {
                      OkCancelResult result = await showOkCancelAlertDialog(
                        style: AdaptiveStyle.iOS,
                        context: Get.context!,
                        title: '系统提示',
                        message: '删除后无法恢复',
                        okLabel: '确定',
                        cancelLabel: '取消',
                        defaultType: OkCancelAlertDefaultType.cancel,
                      );
                      if (result == OkCancelResult.ok) {
                        MomentController.to
                            .delReply(index, item.dynamicId!, comment);
                      }
                    } else {
                      reply?.call(comment);
                    }
                  },
                  child: Padding(
                    padding: const EdgeInsets.symmetric(
                      vertical: AppSize.verticalPadding,
                    ),
                    child: replayItem(comment),
                  ),
                );
              },
              separatorBuilder: (context, index) => const Divider(height: 0.4),
            ),
            if (maxDisplayCount != null && listLength > 3)
              Padding(
                padding: const EdgeInsets.all(
                  AppSize.verticalPadding,
                ),
                child: GestureDetector(
                  onTap: () {
                    MomentController.to.pushToDetail(index);
                  },
                  child: Row(
                    children: [
                      Text(
                        '查看全部$listLength条回复',
                        style: const TextStyle(
                          color: AppColors.active,
                          fontSize: 12,
                        ),
                      ),
                      const Padding(
                        padding: EdgeInsets.only(
                          top: 2.0,
                        ),
                        child: Icon(
                          Icons.keyboard_arrow_down,
                          color: AppColors.active,
                          size: 16,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }

  Widget replayItem(Comment comment) {
    final name = comment.user?.nickname ?? '';
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // MomentAvatar(imageUrl: comment.user?.avatar),
        // const SizedBox(width: 10),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              if (comment.parent == null)
                Text.rich(TextSpan(children: [
                  TextSpan(
                    text: '$name:',
                    style: const TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 13,
                      color: AppColors.darkBlue,
                    ),
                  ),
                  TextSpan(
                    text: comment.content ?? '',
                    style: const TextStyle(
                      fontWeight: FontWeight.normal,
                      color: AppColors.active,
                      fontSize: 13,
                    ),
                  ),
                ]))
              else
                Text.rich(
                  TextSpan(children: [
                    TextSpan(
                      text: name,
                      style: const TextStyle(
                        fontSize: 13,
                      ),
                    ),
                    const TextSpan(
                      text: ' 回复 ',
                      style: TextStyle(
                        fontWeight: FontWeight.normal,
                        color: AppColors.active,
                        fontSize: 13,
                      ),
                    ),
                    TextSpan(
                      text: '${comment.parent?.nickname}: ',
                      style: const TextStyle(
                        fontSize: 13,
                      ),
                    ),
                    TextSpan(
                      text: comment.content ?? '',
                      style: const TextStyle(
                        fontWeight: FontWeight.normal,
                        color: AppColors.active,
                        fontSize: 13,
                      ),
                    ),
                  ]),
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    // fontSize: AppSize.titleFontSize,
                    color: AppColors.darkBlue,
                  ),
                ),
              // Text(
              //   comment.content ?? '',
              //   maxLines: maxLine ?? 99,
              //   overflow: TextOverflow.ellipsis,
              // )
            ],
          ),
        )
      ],
    );
  }
}
