import 'dart:async';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tencent_kit/tencent_kit.dart';
import 'package:wechat_kit/wechat_kit.dart';

import '../utils/toast.dart';

const String WECHAT_APPID = 'wx9455bd2742259ef7';
const String WECHAT_UNIVERSAL_LINK = 'your wechat universal link'; // iOS 请配置
const String WECHAT_APPSECRET = 'c58946da98393252077073c29c828d08';
const String WECHAT_MINIAPPID = 'your wechat miniAppId';

typedef WeChatLogin = void Function(String code);

mixin WechatMixin<T extends StatefulWidget> on State<T> {
  Wechat _wechat;

  StreamSubscription<WechatAuthResp> _auth;
  StreamSubscription<WechatSdkResp> _share;
  StreamSubscription<WechatPayResp> _pay;
  StreamSubscription<WechatLaunchMiniProgramResp> _miniProgram;

  WechatAuthResp _authResp;

  void initState() {
    super.initState();
    _wechat = Wechat()
      ..registerApp(
        appId: WECHAT_APPID,
        universalLink: WECHAT_UNIVERSAL_LINK,
      );
    // _auth = _wechat.authResp().listen(_listenAuth);
    // _share = _wechat.shareMsgResp().listen(_listenShareMsg);
    // _pay = _wechat.payResp().listen(_listenPay);
    // _miniProgram = _wechat.launchMiniProgramResp().listen(_listenMiniProgram);
  }

  // wechat 登录
  void wechatLogin(WeChatLogin weChatLogin) async {
    if (await _wechat.isInstalled() && await _wechat.isSupportApi()) {
      _wechat.auth(
        scope: <String>[WechatScope.SNSAPI_USERINFO],
        state: 'auth',
      );
      print(
          "wechatLoginwechatLoginwechatLoginwechatLoginwechatLoginwechatLoginwechatLoginwechatLogin");
      if (_auth == null) {
        _auth = _wechat.authResp().listen((WechatAuthResp resp) {
          _authResp = resp;
          String content = 'auth: ${resp.errorCode} ${resp.errorMsg}';
          if (_authResp != null &&
              _authResp.errorCode == WechatSdkResp.ERRORCODE_SUCCESS) {
            weChatLogin.call(_authResp.code);
          } else {
            weChatLogin.call(null);
          }
        });
      }
    } else {
      weChatLogin.call(null);
    }
  }

  void wechatSharePage() {
    _wechat.shareWebpage(
      scene: WechatScene.TIMELINE,
      webpageUrl: 'https://www.baidu.com',
    );
  }

//  void wechatShareImage() async {
//    Dio dio = new Dio();
//    Directory documentsDir = await getApplicationDocumentsDirectory();
//    String documentsPath = documentsDir.path;
//    String path = "$documentsPath/demo/game.jpg";
//    await dio.download("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=229276664,935794386&fm=26&gp=0.jpg", "$path",
//        options: Options(headers: {HttpHeaders.acceptEncodingHeader: "*"}), // disable gzip
//        onReceiveProgress: (received, total) {
//      if (total != -1) {}
//    });
//    print(path);
//    await _wechat.shareImage(scene: WechatScene.SESSION, imageData: File(path).readAsBytesSync());
//  }

  void wechatShareImage(String path,String type) async {
    if(type == "chat"){
      // 这是 聊天
      await _wechat.shareImage(
          scene: WechatScene.SESSION, imageData: File(path).readAsBytesSync());
    }else if(type == "friend"){
      // 这是朋友圈
      await _wechat.shareImage(
          scene: WechatScene.TIMELINE, imageData: File(path).readAsBytesSync());
    }
  }

  void dispose() {
    _auth?.cancel();
    _auth = null;
    _share?.cancel();
    _share = null;
    _pay?.cancel();
    _pay = null;
    _miniProgram?.cancel();
    _miniProgram = null;
    _wechat = null;
    super.dispose();
  }
}
