import 'package:electronic_assistant/resource/assets.gen.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import '../resource/colors.gen.dart';

typedef TalkShareCallback = void Function(ShareTalkType shareType,
    ShareTo shareTo, String fileName, String dialogTag);

void showTalkShareDialog(String? title,
    {required TalkShareCallback callback, List<ShareTalkType>? shareToType}) {
  const String tag = 'showTalkShareDialog';

  Rx<ShareTalkType> shareType = ShareTalkType.summary.obs;

  String getFileName() {
    if (shareType.value == ShareTalkType.summary) {
      return '[${StringName.talkTabSummary.tr}] $title.txt';
    } else if (shareType.value == ShareTalkType.mindMap) {
      return '$title.png';
    } else {
      return '[${StringName.talkTabOriginal.tr}] $title.txt';
    }
  }

  SmartDialog.show(
      tag: tag,
      maskColor: Colors.black54,
      alignment: const Alignment(0.0, 0.94),
      builder: (_) => Container(
            width: 336.w,
            padding: EdgeInsets.all(16.w),
            decoration: BoxDecoration(
              color: ColorName.white,
              borderRadius: BorderRadius.circular(12),
            ),
            child: IntrinsicHeight(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Obx(() {
                        return shareType.value == ShareTalkType.mindMap
                            ? Assets.images.iconShareFilePng
                                .image(width: 28.w, height: 32.w)
                            : Assets.images.iconTalkTxt
                                .image(width: 28.w, height: 32.w);
                      }),
                      SizedBox(width: 6.w),
                      Expanded(child: Obx(() {
                        return Text(getFileName(),
                            style: TextStyle(
                                fontSize: 14.sp,
                                fontWeight: FontWeight.bold,
                                color: ColorName.primaryTextColor));
                      })),
                      SizedBox(width: 18.w),
                      GestureDetector(
                        onTap: () {
                          SmartDialog.dismiss(tag: tag);
                        },
                        child: Assets.images.iconTalkShareClose
                            .image(width: 28.w, height: 28.w),
                      ),
                    ],
                  ),
                  SizedBox(height: 12.h),
                  Divider(color: '#F6F6F6'.toColor(), height: 1.h),
                  SizedBox(height: 20.h),
                  Obx(() {
                    final type = shareToType ?? getDefaultShareType();
                    return Row(
                      children: [
                        for (int i = 0; i < type.length; i++) ...[
                          Expanded(
                            child: _buildShareTypeItem(
                              _getTitle(
                                  (shareToType ?? getDefaultShareType())[i]),
                              _getIconProvider(
                                  (shareToType ?? getDefaultShareType())[i]),
                              shareType.value ==
                                  (shareToType ?? getDefaultShareType())[i],
                              () {
                                shareType.value =
                                    (shareToType ?? getDefaultShareType())[i];
                              },
                            ),
                          ),
                          if (i < type.length - 1)
                            SizedBox(width: 8.w), // Add gap between items
                        ],
                      ],
                    );
                  }),
                  SizedBox(height: 16.h),
                  GetPlatform.isIOS
                      ? GestureDetector(
                          onTap: () {
                            callback(shareType.value, ShareTo.ios,
                                getFileName(), tag);
                          },
                          child: Container(
                            margin: EdgeInsets.only(top: 8.h),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(8),
                              gradient: LinearGradient(
                                colors: [
                                  '#6177F2'.toColor(),
                                  '#8B9DFF'.toColor()
                                ],
                                begin: Alignment.centerLeft,
                                end: Alignment.centerRight,
                                stops: const [0, 1.0],
                              ),
                            ),
                            // width: 240.w,
                            height: 48.w,
                            child: Center(
                              child: Text(
                                "分享",
                                style: TextStyle(
                                    fontSize: 16.sp, color: ColorName.white),
                              ),
                            ),
                          ),
                        )
                      : Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              '发送至',
                              style: TextStyle(
                                  fontSize: 14.sp,
                                  color: ColorName.secondaryTextColor),
                            ),
                            SizedBox(height: 8.h),
                            Row(
                              children: [
                                _buildShareItem(StringName.dialogSendFriend.tr,
                                    Assets.images.iconWx.provider(), () {
                                  callback(shareType.value, ShareTo.wechat,
                                      getFileName(), tag);
                                }),
                                _buildShareItem(StringName.dialogSendFriend.tr,
                                    Assets.images.iconQq.provider(), () {
                                  callback(shareType.value, ShareTo.qq,
                                      getFileName(), tag);
                                }),
                              ],
                            )
                          ],
                        ),
                ],
              ),
            ),
          ));
}

Widget _buildShareTypeItem(String title, ImageProvider imageProvider,
    bool isCheck, void Function() onTap) {
  return GestureDetector(
    onTap: onTap,
    child: Container(
      padding: EdgeInsets.only(top: 14.h, bottom: 12.h),
      decoration: isCheck
          ? BoxDecoration(
              color: '#E7E9F6'.toColor(),
              border: Border.all(color: ColorName.colorPrimary, width: 2),
              borderRadius: BorderRadius.circular(8),
            )
          : BoxDecoration(
              color: '#F6F5F8'.toColor(),
              borderRadius: BorderRadius.circular(8),
            ),
      child: Column(
        children: [
          Image(image: imageProvider, width: 24.w, height: 24.w),
          SizedBox(width: 4.h),
          Text(title,
              style: TextStyle(
                  fontSize: 14.sp, color: ColorName.primaryTextColor)),
        ],
      ),
    ),
  );
}

Widget _buildShareItem(
    String itemName, ImageProvider imageProvider, void Function() onTap) {
  return Container(
    margin: EdgeInsets.symmetric(horizontal: 10.w),
    child: GestureDetector(
      onTap: onTap,
      child: Column(
        children: [
          Image(image: imageProvider, width: 40.w, height: 40.w),
          SizedBox(height: 6.h),
          Text(itemName,
              style: TextStyle(
                  fontSize: 14.sp, color: ColorName.secondaryTextColor)),
        ],
      ),
    ),
  );
}

String _getTitle(ShareTalkType type) {
  switch (type) {
    case ShareTalkType.summary:
      return StringName.talkTabSummary.tr;
    case ShareTalkType.mindMap:
      return StringName.talkMindMap.tr;
    case ShareTalkType.original:
      return StringName.talkTabOriginal.tr;
    default:
      return '';
  }
}

ImageProvider _getIconProvider(ShareTalkType type) {
  switch (type) {
    case ShareTalkType.summary:
      return Assets.images.iconTalkShareSummary.provider();
    case ShareTalkType.mindMap:
      return Assets.images.iconTalkMindMap.provider();
    case ShareTalkType.original:
      return Assets.images.iconTalkShareOriginal.provider();
    default:
      return const AssetImage(''); // Provide a default image if necessary
  }
}

List<ShareTalkType> getDefaultShareType() {
  return [ShareTalkType.summary, ShareTalkType.mindMap, ShareTalkType.original];
}

enum ShareTalkType { summary, mindMap, original }

enum ShareTo { wechat, qq, ios }
