import 'dart:math';

import 'package:auto_route/src/route/page_route_info.dart';
import 'package:cpt_community/components/comments_dialog.dart';
import 'package:cpt_community/modules/community/community_page.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:plugin_basic/modules/global_web_page.dart';
import 'package:plugin_basic/modules/preview_photo_page.dart';
import 'package:plugin_basic/router/basic_page_router.dart';
import 'package:plugin_platform/engine/image/image_preview.dart';
import 'package:router/componentRouter/component_service_manager.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:shared/utils/color_utils.dart';
import 'package:shared/utils/goto_page.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/utils/dark_theme_util.dart';

class NewsFeedCardContent extends StatelessWidget {
  final String content;
  final List<dynamic>? imageUrls;
  int? rowMaxImageNum=3;
  int? textMaxLines = 3;
  bool? isPreviewImage = true;
  NewsFeedCardContent({
    Key? key,
    required this.content,
    required List<dynamic> this.imageUrls,
    this.rowMaxImageNum = 3,
    this.textMaxLines = 3,
    this.isPreviewImage = true,
  }) : super(key: key);


  @override
  Widget build(BuildContext context) {
    List imageUrlsList = [];
    if(imageUrls != null && imageUrls!.isNotEmpty){
      // dart 取出 imageUrls 里面的前 rowMaxImageNum 张图片
      imageUrlsList = imageUrls!.take(rowMaxImageNum!).toList();
    }
    int totalImageCount = imageUrls!.length;
    int otherImageCount = imageUrls!.length - rowMaxImageNum!;
    int rowActualImageCount = imageUrls!.length > rowMaxImageNum! ? rowMaxImageNum! : imageUrls!.length;
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          final maxHeight = constraints.maxHeight;
          final minHeight = constraints.minHeight;
          final maxWidth = constraints.maxWidth;
          // Log.d("---maxHeight-----$maxHeight-- $minHeight  $maxWidth--");
          return Container(
            width: double.infinity,
            // color: Colors.red,
            padding: const EdgeInsets.symmetric(horizontal: 15),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  MyTextView(
                    content,
                    textColor: context.appColors.textBlack,
                    fontSize: 15,
                    maxLines: textMaxLines,
                    isTextEllipsis: true,
                  ),
                  const SizedBox(height: 12),
                  // 图片
                  _buildImageSection(
                      context,
                      isPreviewImage!,
                      maxWidth - 15,
                      imageUrlsList,
                      imageUrls,
                      totalImageCount,
                      otherImageCount,
                      rowMaxImageNum!
                  ),
                ]
            )
          );
      }
    );
  }


  Widget _buildImageSection(BuildContext context,bool isPreviewImage, double maxWidth, List? imageUrls,List? totalImageUrls, int totalImageCount, int otherImageCount, int rowMaxImageNum) {
    if (imageUrls != null && imageUrls!.isNotEmpty) {
      final imageCount = imageUrls.length;
      return SizedBox(
        width: maxWidth,
        height: 87,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: List.generate(
              imageUrls.length,
              (index) => Container(
                height: 87,
                  width: (totalImageCount < rowMaxImageNum)? maxWidth/rowMaxImageNum - 16 : maxWidth/imageCount - 16,
                margin: EdgeInsets.only(right: (index!=imageCount-1) ? 16 : 0 ),
                color: Colors.transparent,
                child:Stack(
                    children: [
                      Hero(
                        tag: imageUrls[index],
                        child: MyLoadImage(
                          imageUrls[index],
                          height: 87,
                          // width: maxWidth/imageCount - 16,
                          width: (totalImageCount < rowMaxImageNum)? maxWidth/rowMaxImageNum - 16 : maxWidth/imageCount - 16,
                          fit: BoxFit.cover,
                          // fit:BoxFit.fitWidth,
                          cornerRadius: 5,
                          onClick: (){
                            if(isPreviewImage){
                              // 点击图片预览
                              // 过滤掉非字符串类型的元素
                              List<String> filteredImages = totalImageUrls?.whereType<String>().toList() ?? [];
                              // PreviewPhotoPage.startInstance(context: context, images: filteredImages, position: index);
                              // context.appRouter.push(PreviewPhotoPageRoute(images: filteredImages, position: index));
                              // GotoPage.pushPageByFade(
                              //     context: context,
                              //     targetPage:
                              //     PreviewPhotoPage(images: filteredImages, position: index),
                              // );
                              ImagePreviewEngine.multipleImagePreview(
                                  context,
                                  filteredImages,
                                  heroes: List.generate(filteredImages.length, (index) => filteredImages[index]),
                                  onLongPressAction: (url) {}
                              );
                            }
                          },
                        ),
                      ),
                      otherImageCount > 0 && index == imageCount-1 ?
                        Positioned(
                        bottom: 0,
                        right: 0,
                        child: Container(
                          padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
                          color: context.appColors.textBlack.withOpacity(0.5),
                          child: MyTextView(
                            "+$otherImageCount",
                            textColor: Colors.white,
                            fontSize: 12,
                          ),
                        ),
                      ): const SizedBox.shrink(),
                    ]
                  )
              ),
          )
        )
            );
      } else {
        return const SizedBox.shrink();
      }
    }
}