import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:sport/bean/post.dart';
import 'package:sport/pages/social/post_share_page.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/utils/toast.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sport/sharesdk/tencent.dart';
import 'package:sport/sharesdk/wechat.dart';
import 'dart:io';
import 'dart:typed_data';

class MenuShareBottomContent extends StatefulWidget {
  final String url;
  final bool hasDownload;
  final GlobalKey poster;
  final String shareType;
  final Post post;

  MenuShareBottomContent(this.shareType,
      {this.post, this.url = "", this.hasDownload = false, this.poster});
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MenuShareBottomContentState();
  }
}
// shareType : Img / Link / Social

class _MenuShareBottomContentState extends State<MenuShareBottomContent>
    with TencentMixin, WechatMixin {
  List<Map<String, dynamic>> map;
  Uint8List postInstance;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    map = [
      {
        "value": "微信",
        "url": "share_icon_wechat",
        "callBack": () async {
          if (widget.shareType == "Img") {
            String path = await initFile();
            wechatShareImage(path, "chat");
          } else {
            print("-----");
          }
        }
      },
      {
        "value": "朋友圈",
        "url": "share_icon_wechatmonents",
        "callBack": () async {
          if (widget.shareType == "Img") {
            String path = await initFile();
            wechatShareImage(path, "friend");
          } else {
            print("-----");
          }
        }
      },
      {
        "value": "QQ",
        "url": "share_icon_qq",
        "callBack": () async {
          if (widget.shareType == "Img") {
            String path = await initFile();
            qqShare(path);
          } else {
            print("-----");
          }
        }
      },
      {
        "value": "社区",
        "url": "share_icon_community",
        "callBack": () {
          if (widget.shareType == "social") {
            NavigatorUtil.goPage(
                context, (context) => PostSharePage(widget.post));
          }
        }
      },
      {
        "value": "社区好友",
        "url": "share_icon_friends",
        "callBack": () {
          if (widget.shareType == "social") {
            NavigatorUtil.goPage(
                context, (context) => PostShareFriendsPage(widget.post));
          }
        }
      },
      {
        "value": "复制链接",
        "url": "share_icon_link",
        "callBack": () {
          Clipboard.setData(ClipboardData(text: widget.url));
          ToastUtil.show("复制成功");
        }
      },
      {
        "value": "下载图片",
        "url": "share_icon_download",
        "callBack": () {
          _capture().then((file) async {}).whenComplete(() {
            print("Complete-------------------------------------------------");
          });
        }
      },
    ];

    if (widget.hasDownload == null || !widget.hasDownload) {
      map.removeLast();
    }

    if (widget.url == null) {
      map.removeAt(5);
    }
    // 社区暂时先把前面的隐藏掉
    if (widget.shareType == "social") {
      map = map.sublist(map.length - 2, map.length);
    }
  }

  // 封装成灵活的更通用一点...
  Future<String> initFile() async {
    Uint8List pngBytes = await initFileUint8List();

    String sTempDir = (await getTemporaryDirectory()).path;
    bool isDirExist = await Directory(sTempDir).exists();
    if (!isDirExist) {
      Directory(sTempDir).create();
    }
    // QQ分享需要一个确实存在的图片
    File file = await File(
            sTempDir + "/poster-temp-${DateTime.now().millisecondsSinceEpoch}")
        .writeAsBytes(pngBytes);
    return file.path;
  }

  Future<Uint8List> initFileUint8List() async {
    try {
      RenderRepaintBoundary boundary =
          widget.poster.currentContext.findRenderObject();
      //boundary.toImage()转化为ui.Image对象,不会自动为包裹的组件添加背景，不设置可能会缺失背景
      var image = await boundary.toImage(pixelRatio: window.devicePixelRatio);
      //将image转化为byteData
      ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
      //这个对象就是图片数据
      Uint8List pngBytes = byteData.buffer.asUint8List();

      print("成功生成图片 ----------------------------------------");
      return pngBytes;
    } catch (e) {
      print(e);
    }
    return null;
  }

  Future<File> _capture() async {
    Uint8List pngBytes = await initFileUint8List();
    final result = await ImageGallerySaver.saveImage(pngBytes); //这个是核心的保存图片的插件
    print(result);
    ToastUtil.show("下载成功");
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return SizedBox(
      child: Container(
        margin: EdgeInsets.fromLTRB(24, 27, 24, 17),
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Expanded(
                  child: Divider(
                    endIndent: 12.0,
                  ),
                ),
                Text(
                  "分享至",
                  style: Theme.of(context).textTheme.bodyText2,
                ),
                Expanded(
                  child: Divider(
                    indent: 12.0,
                  ),
                )
              ],
            ),
            SizedBox(
              height: 12.0,
            ),
            Expanded(
              child: GridView(
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 4,
                      crossAxisSpacing: 12.0,
                      mainAxisSpacing: 12.0),
                  children: map
                      .map(
                        (e) => InkWell(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              Image.asset(
                                "lib/assets/img/${e["url"]}.png",
                                width: 44.0,
                                height: 44.0,
                              ),
                              Text(
                                "${e["value"]}",
                                style: Theme.of(context).textTheme.subtitle2,
                              )
                            ],
                          ),
                          onTap: () {
                            e["callBack"]();
                          },
                        ),
                      )
                      .toList()),
            )
          ],
        ),
      ),
      height: 232.0,
    );
  }
}

Future<bool> menuShareBottom(BuildContext context, String shareType,
    {Post post, String url, bool hasDownload, GlobalKey poster}) {
  return showModalBottomSheet(
    context: context,
    builder: (context) {
      return MenuShareBottomContent(shareType,
          post: post, url: url, hasDownload: hasDownload, poster: poster);
    },
    backgroundColor: Colors.white,
    elevation: 10,
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20), topRight: Radius.circular(20)),
    ),
  );
}
