import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'dart:ui' as ui;
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sharesdk_plugin/sharesdk_plugin.dart';

class QrShareImagePage extends StatefulWidget {
  String mobile;

  QrShareImagePage(this.mobile);

  @override
  State<StatefulWidget> createState() {
    return _QrShareImagePage();
  }
}

class _QrShareImagePage extends State<QrShareImagePage> {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        RepaintBoundary(
          key: globalKey,
          child: Container(
            width: MediaQuery.of(context).size.width * 0.8,
            height: (MediaQuery.of(context).size.width * 0.8) * 1.36,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10.w),
            ),
            child: Column(
              children: [
                Image.asset(
                  "assets/image/share_image_bg_yq.webp",
                  height: (MediaQuery.of(context).size.width * 0.8) * 0.47,
                  width: (MediaQuery.of(context).size.width * 0.8),
                ),
                Expanded(
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.vertical(
                        bottom: Radius.circular(10.w),
                      ),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Container(
                          width:90.w,
                          height: 30.h,
                          child: Text(
                            // S.of(context).yaoqingrenshoujihao_("${widget.mobile.substring(0, 3)}****${widget.mobile.substring(7, 11)}"),
                            S.of(context).wodeyaoqingma,
                            style: TextStyle(
                              fontWeight: MyFontWeight.semi_bold,
                              fontSize: 12.sp,
                              color: Color(0xFF2E3552),
                            ),
                          ),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: Color(0xFFFFC93B),
                            border: Border.all(
                              color: Color(0xFF2E3552),
                              width: 1,
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(left: 16.w, right: 24.w),
                          child: GestureDetector(
                            child: Text(
                              // S.of(context).saomashibieguanzhugongzonghao,
                              widget.mobile,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                color: Color(0xFF2E3552),
                                fontSize:30.sp,
                                fontWeight: MyFontWeight.semi_bold,
                              ),
                            ),
                            onTap: this.copy(widget.mobile),
                          )

                        ),
                        // Container(
                        //   margin: EdgeInsets.symmetric(horizontal: 68.w),
                        //   decoration: BoxDecoration(
                        //     border: Border.all(
                        //       color: Color(0xFF2E3552),
                        //       width: 1,
                        //     ),
                        //   ),
                        //   child: AspectRatio(
                        //     aspectRatio: 1,
                        //     child: QrImage(
                        //       data: "http://mp.hx.lotus-wallet.com/pages/invite/index?mobile=${widget.mobile ?? ""}",
                        //       version: QrVersions.auto,
                        //       size: 200.w,
                        //       gapless: true,
                        //     ),
                        //   ),
                        // ),
                        Container(
                          width:140.w,
                          height: 30.h,
                          child: Text(
                            // S.of(context).yaoqingrenshoujihao_("${widget.mobile.substring(0, 3)}****${widget.mobile.substring(7, 11)}"),
                            S.of(context).haoyoujiangliguize,
                            style: TextStyle(
                              fontWeight: MyFontWeight.semi_bold,
                              fontSize: 12.sp,
                              color: Color(0xFF2E3552),
                            ),
                          ),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: Color(0xFFFFC93B),
                            border: Border.all(
                              color: Color(0xFF2E3552),
                              width: 1,
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(left: 16.w, right: 24.w),
                          child: Text(
                            // S.of(context).saomashibieguanzhugongzonghao,
                            S.of(context).fenxiangyaoqingma,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              color: Color(0xFF2E3552),
                              fontSize: 14.sp,
                              fontWeight: MyFontWeight.semi_bold,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  flex: 1,
                ),
              ],
            ),
          ),
        ),
        SizedBox(
          height: 15,
        ),
        TextButton(
          onPressed: () {
            widgetToImage();
          },
          style: ButtonStyle(
            backgroundColor: MaterialStateProperty.all(
              Color(0xFFFFC93B),
            ),
            shape: MaterialStateProperty.all(
              RoundedRectangleBorder(
                side: BorderSide(
                  color: Color(0xFF2E3552),
                  width: 1,
                ),
                borderRadius: BorderRadius.circular(5),
              ),
            ),
            elevation: MaterialStateProperty.all(3),
            shadowColor: MaterialStateProperty.all(Colors.amber),
            padding: MaterialStateProperty.all(
              EdgeInsets.symmetric(
                vertical: 5.h,
                horizontal: 20.w,
              ),
            ),
          ),
          child: Text(
            S.of(context).fenxiangzhiweixin,
            style: TextStyle(
                color: Color(0xFF2E3552),
                fontSize: 18.sp,
                fontWeight: MyFontWeight.semi_bold),
          ),
        ),
      ],
    );
  }

  GlobalKey globalKey = GlobalKey();

  widgetToImage() async {
    BuildContext buildContext = globalKey.currentContext;
    if (null != buildContext) {
      RenderRepaintBoundary boundary = buildContext.findRenderObject();
      ui.Image image = await boundary.toImage(pixelRatio: 3);
      ByteData byteData =
      await image.toByteData(format: ui.ImageByteFormat.png);

      String filePath = "";
      if (Platform.isAndroid) {
        filePath = (await getExternalStorageDirectory()).path;
      } else {
        filePath = (await getApplicationDocumentsDirectory()).path;
      }
      filePath = "$filePath/${DateTime.now().millisecondsSinceEpoch}.webp";

      File file = File(filePath);
      if (!file.existsSync()) {
        file.createSync();
        print("object: ${file.path}");
      }
      file.writeAsBytesSync(byteData.buffer.asUint8List());

      SSDKMap params = SSDKMap()
        ..setGeneral(
          "",
          "",
          [
            file.path,
          ],
          "",
          file.path,
          "",
          "",
          "",
          "",
          "",
          SSDKContentTypes.image,
        );

      SharesdkPlugin.share(ShareSDKPlatforms.wechatSession, params,
              (state, userData, contentEntity, error) {
            print("share  state           !$state");
            print("share  userData        !$userData");
            print("share  contentEntity   !$contentEntity");
            print("share  error           !$error");
          });
    }
  }

  copy(String str) {
    print(str);
    Clipboard.setData(ClipboardData(text: str));
  }
}