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:chat/views/moments/index/widgets/future_button.dart';
import 'package:chat/views/moments/index/widgets/grid_media.dart';
import 'package:chat/widgets/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MomentListItem extends StatelessWidget {
  final MomentItemModel item;
  MomentListItem({Key? key, required this.item}) : super(key: key);

  final actionStyle = ButtonStyle(
    tapTargetSize: MaterialTapTargetSize.shrinkWrap,
    padding: MaterialStateProperty.all(
        const EdgeInsets.symmetric(vertical: 4, horizontal: 8)),
    minimumSize: MaterialStateProperty.all(Size.zero),
    foregroundColor: MaterialStateProperty.all(AppColors.deep.withOpacity(0.6)),
    textStyle: MaterialStateProperty.all(
      const TextStyle(
        fontSize: 13,
        fontWeight: FontWeight.bold,
      ),
    ),
  );

  // Future<Uint8List?> getVideoThumbnail(String source) async {
  //   return await VideoThumbnail.thumbnailData(
  //     video: source,
  //     imageFormat: ImageFormat.JPEG,
  //     maxWidth: 128,
  //     quality: 25,
  //   );
  // }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(
        horizontal: AppSize.horizontalLargePadding,
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          CustomAvatar(item.user?.avatar),
          const SizedBox(width: 10),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  item.user?.nickname ?? '',
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: AppSize.fontSize + 1,
                    color: AppColors.darkBlue,
                  ),
                ),
                const SizedBox(height: 2),
                Text(
                  item.description ?? '',
                  style: const TextStyle(
                    fontSize: AppSize.fontSize,
                  ),
                ),
                const SizedBox(height: 8),
                Visibility(
                  visible: item.pictures?.isNotEmpty ?? false,
                  child: GridMedia(item.pictures),
                ),
                timeAndAction()
              ],
            ),
          )
        ],
      ),
    );
  }

  // Widget gridMedia() {
  //   if (item.pictures?.length == 1) {
  //     return ConstrainedBox(
  //       constraints: BoxConstraints(
  //         maxHeight: Get.width * 0.5,
  //         maxWidth: Get.width * 0.5,
  //         minHeight: 50,
  //         minWidth: 50,
  //       ),
  //       child: imageWidget(item.pictures![0], 0),
  //     );
  //   } else {
  //     return GridView.builder(
  //       padding: EdgeInsets.zero,
  //       shrinkWrap: true,
  //       physics: const NeverScrollableScrollPhysics(),
  //       gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
  //         crossAxisCount: 3,
  //         mainAxisSpacing: 8,
  //         crossAxisSpacing: 8,
  //         childAspectRatio: 1,
  //       ),
  //       itemCount: item.pictures?.length ?? 0,
  //       itemBuilder: (context, index) {
  //         final source = item.pictures![index];
  //         if (source.isImageFileName) {
  //           return imageWidget(source, index);
  //         }
  //         return videoWidget(source, index);
  //       },
  //     );
  //   }
  // }

  Widget timeAndAction() {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: Row(
              children: [
                Text(
                  item.time ?? '',
                  style: const TextStyle(
                    fontSize: AppSize.fontSize - 2,
                    color: AppColors.unactive,
                  ),
                ),
                item.isMe == true
                    ? InkWell(
                        onTap: () async {
                          OkCancelResult result = await showOkCancelAlertDialog(
                            style: AdaptiveStyle.iOS,
                            context: Get.context!,
                            title: '系统提示',
                            message: '删除后无法撤回',
                            okLabel: '确定',
                            cancelLabel: '取消',
                            defaultType: OkCancelAlertDefaultType.cancel,
                          );
                          if (result == OkCancelResult.ok) {
                            MomentController.to.delMoment(item);
                          }
                        },
                        child: const Padding(
                          padding: EdgeInsets.all(10.0),
                          child: Text(
                            '删除',
                            style: TextStyle(
                              fontSize: AppSize.fontSize - 2,
                              color: AppColors.darkBlue,
                            ),
                          ),
                        ),
                      )
                    : const SizedBox(),
              ],
            ),
          ),
          FutureTextButton(
            style: actionStyle,
            onPressed: () => MomentController.to.likeMoment(item),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Visibility(
                  visible: item.isLike ?? false,
                  child: const Icon(Icons.favorite, size: 14),
                  replacement: const Icon(Icons.favorite_border, size: 14),
                ),
                const SizedBox(width: 4),
                Padding(
                  padding: const EdgeInsets.only(bottom: 2.0),
                  child: Text('${item.likerCount ?? ''}'),
                ),
              ],
            ),
          ),
          const SizedBox(width: 6),
          TextButton(
            style: actionStyle,
            onPressed: () => MomentController.to.showReplyBar(item.dynamicId!),
            child: Row(
              children: [
                const Icon(Icons.message, size: 14),
                const SizedBox(width: 4),
                Padding(
                  padding: const EdgeInsets.only(bottom: 2.0),
                  child: Text('${item.comments?.length ?? ''}'),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
