
import 'dart:async';

import 'package:alipay_kit/alipay_kit.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:fluwx/fluwx.dart';
import 'package:twong/config/pay_config.dart';
import 'package:twong/models/details.dart';
// import 'package:wechat_kit/wechat_kit.dart';

import 'index.dart';

class Pay {
  static init() {
    _alipay.payResp().listen(_listenAliPay);
    // _wechat.registerApp(appId: PayConfig.wechatAppId,
    //     universalLink: PayConfig.universalLink);
    // _wechat.payResp().listen(_listenWechatPay);
    // _wechat.shareMsgResp().listen(_listenWechatShare);

    _alipay.isInstalled().then((value) {
      _aliInstalled = value;
      Log.d("检查完毕 支付宝${value ? '已安装' : '未安装'}");
    });
    // _wechat.isInstalled().then((value) {
    //   _weInstalled = value;
    //   Log.d("检查完毕 微信${value ? '已安装' : '未安装'}");
    // });

    registerWxApi(appId: PayConfig.wechatAppId, universalLink: PayConfig.universalLink);
  }

  static Alipay _alipay = Alipay();
  // static Wechat _wechat = Wechat();
  static bool _weInstalled = false;
  static bool _aliInstalled = false;

  // Alipay get alipay => _alipay;
  // Wechat get wechat => _wechat;
  static Function(bool) _callback;

  static void _listenAliPay(AlipayResp resp) {
    Log.d('Alipay : ${resp.resultStatus} - ${resp.result} ${resp.memo}');
    if(_callback != null) {
      // _callback(resp.resultStatus == Alipay);
      _callback(false);
    }
  }

  // static void _listenWechatPay(WechatPayResp resp) {
  //   Log.d('WechatPay : ${resp.returnKey} - ${resp.errorCode}: ${resp.errorMsg}');
  //   if(_callback != null) {
  //     _callback(resp.errorCode == WechatSdkResp.ERRORCODE_SUCCESS);
  //     _callback = null;
  //   }
  // }
  //
  // static void _listenWechatShare(WechatSdkResp resp) {
  //   Log.d('WechatShare : ${resp.errorCode}: ${resp.errorMsg}');
  //   if(_callback != null) {
  //     _callback(resp.errorCode == WechatSdkResp.ERRORCODE_SUCCESS);
  //     _callback = null;
  //   }
  // }

  static void aliPay(String conf, {Function(bool) callback}) {
    if(!_aliInstalled) {
      BotToast.showText(text: "没有安装支付宝");
      return;
    }
    _callback = callback;
    Log.d("begin alipay ...");
    _alipay.payOrderSign(orderInfo: conf);
  }

  static void wechatPay(dynamic payConf, {Function(bool) callback}) {
    // if (!_weInstalled) {
    //   BotToast.showText(text: "没有安装微信");
    //   return;
    // }
    Log.d("begin wechat pay ...");
    payWithWeChat(appId: payConf["appid"],
            partnerId: payConf["partnerid"],
            prepayId: payConf["prepayid"],
            packageValue: payConf["package"],
            nonceStr: payConf["noncestr"],
            timeStamp: payConf["timestamp"],
            sign: payConf["sign"]).then((value) {
              if(callback != null) {
                callback(value);
              }
    });
    // _callback = callback;
    // _wechat.pay(appId: payConf["appid"],
    //     partnerId: payConf["partnerid"],
    //     prepayId: payConf["prepayid"],
    //     package: payConf["package"],
    //     nonceStr: payConf["noncestr"],
    //     timeStamp: payConf["timestamp"].toString(),
    //     sign: payConf["sign"]);
  }

  static void wechatShare(bool friend, Details data, {Function(bool) callback}) {
    // if(!_weInstalled) {
    //   BotToast.showText(text: "没有安装微信");
    //   return;
    // }
    Log.d("begin share ...");
    // _wechat.shareWebpage(
    //     scene: friend ? WechatScene.SESSION : WechatScene.TIMELINE,
    //     webpageUrl: "https://twong.shotshock.shop/share${data.storeInfo.id}",
    //     title: data.storeInfo.store_name,
    //     description: data.storeInfo.store_info);
    shareToWeChat(WeChatShareWebPageModel(
      "https://twong.shotshock.shop/mobilex/share?id=${data.storeInfo.id}",
      title: data.storeInfo.store_name,
      description: data.storeInfo.store_info,
      thumbnail: WeChatImage.network(data.storeInfo.image),
      scene: friend ? WeChatScene.SESSION : WeChatScene.TIMELINE,
    )).then((value) {
      if(callback != null) {
        callback(value);
      }
    });
  }
}