"use strict";
const common_vendor = require("../../common/vendor.js");
const sheep_index = require("../index.js");
require("../helper/index.js");
const sheep_api_pay_order = require("../api/pay/order.js");
class SheepPay {
  constructor(payment, orderType, id) {
    this.payment = payment;
    this.id = id;
    this.orderType = orderType;
    this.payAction();
  }
  payAction() {
    const payAction = {
      WechatOfficialAccount: {
        wechat: () => {
          this.wechatOfficialAccountPay();
        },
        alipay: () => {
          this.redirectPay();
        },
        wallet: () => {
          this.walletPay();
        },
        mock: () => {
          this.mockPay();
        }
      },
      WechatMiniProgram: {
        wechat: () => {
          this.wechatMiniProgramPay();
        },
        alipay: () => {
          this.copyPayLink();
        },
        wallet: () => {
          this.walletPay();
        },
        mock: () => {
          this.mockPay();
        }
      },
      App: {
        wechat: () => {
          this.wechatAppPay();
        },
        alipay: () => {
          this.alipay();
        },
        wallet: () => {
          this.walletPay();
        },
        mock: () => {
          this.mockPay();
        }
      },
      H5: {
        wechat: () => {
          this.wechatWapPay();
        },
        alipay: () => {
          this.redirectPay();
        },
        wallet: () => {
          this.walletPay();
        },
        mock: () => {
          this.mockPay();
        }
      }
    };
    return payAction[sheep_index.sheep.$platform.name][this.payment]();
  }
  // 预支付
  prepay(channel) {
    return new Promise(async (resolve, reject) => {
      let data = {
        id: this.id,
        channelCode: channel,
        channelExtras: {}
      };
      if (["wx_pub", "wx_lite"].includes(channel)) {
        const openid = await sheep_index.sheep.$platform.useProvider("wechat").getOpenid(true);
        if (!openid) {
          this.bindWeixin();
          return;
        }
        data.channelExtras.openid = openid;
      }
      sheep_api_pay_order.PayOrderApi.submitOrder(data).then((res) => {
        res.code === 0 && resolve(res);
        if (res.code !== 0 && res.msg.indexOf("无效的openid") >= 0) {
          if (res.msg.indexOf("无效的openid") >= 0 || res.msg.indexOf("下单账号与支付账号不一致") >= 0) {
            this.bindWeixin();
          }
        }
      });
    });
  }
  // 微信小程序支付
  async wechatMiniProgramPay() {
    let { code, data } = await this.prepay("wx_lite");
    if (code !== 0) {
      return;
    }
    const payConfig = JSON.parse(data.displayContent);
    common_vendor.index.requestPayment({
      provider: "wxpay",
      timeStamp: payConfig.timeStamp,
      nonceStr: payConfig.nonceStr,
      package: payConfig.packageValue,
      signType: payConfig.signType,
      paySign: payConfig.paySign,
      success: (res) => {
        this.payResult("success");
      },
      fail: (err) => {
        if (err.errMsg === "requestPayment:fail cancel") {
          sheep_index.sheep.$helper.toast("支付已手动取消");
        } else {
          this.payResult("fail");
        }
      }
    });
  }
  // 余额支付
  async walletPay() {
    const { code } = await this.prepay("wallet");
    code === 0 && this.payResult("success");
  }
  // 模拟支付
  async mockPay() {
    const { code } = await this.prepay("mock");
    code === 0 && this.payResult("success");
  }
  // 支付宝复制链接支付  TODO 非繁人：待接入
  async copyPayLink() {
    let { error, data } = await this.prepay();
    if (error === 0) {
      common_vendor.index.showModal({
        title: "支付宝支付",
        content: "复制链接到外部浏览器",
        confirmText: "复制链接",
        success: (res) => {
          if (res.confirm) {
            sheep_index.sheep.$helper.copyText(data.pay_data);
          }
        }
      });
    }
  }
  // 支付宝支付  TODO 非繁人：待接入
  async alipay() {
    let that = this;
    const { error, data } = await this.prepay();
    if (error === 0) {
      common_vendor.index.requestPayment({
        provider: "alipay",
        orderInfo: data.pay_data,
        //支付宝订单数据
        success: (res) => {
          that.payResult("success");
        },
        fail: (err) => {
          if (err.errMsg === "requestPayment:fail [paymentAlipay:62001]user cancel") {
            sheep_index.sheep.$helper.toast("支付已手动取消");
          } else {
            that.payResult("fail");
          }
        }
      });
    }
  }
  // 微信支付  TODO 非繁人：待接入
  async wechatAppPay() {
    let that = this;
    let { error, data } = await this.prepay();
    if (error === 0) {
      common_vendor.index.requestPayment({
        provider: "wxpay",
        orderInfo: data.pay_data,
        //微信订单数据(官方说是string。实测为object)
        success: (res) => {
          that.payResult("success");
        },
        fail: (err) => {
          err.errMsg !== "requestPayment:fail cancel" && that.payResult("fail");
        }
      });
    }
  }
  // 支付结果跳转,success:成功，fail:失败
  payResult(resultType) {
    sheep_index.sheep.$router.redirect("/pages/pay/result", {
      id: this.id,
      orderType: this.orderType,
      payState: resultType
    });
  }
  // 引导绑定微信
  bindWeixin() {
    common_vendor.index.showModal({
      title: "微信支付",
      content: "请先绑定微信再使用微信支付",
      confirmText: "绑定",
      success: function(res) {
        if (res.confirm) {
          sheep_index.sheep.$platform.useProvider("wechat").bind();
        }
      }
    });
  }
}
function getPayMethods(channels) {
  const payMethods = [
    {
      icon: "/static/img/shop/pay/wechat.png",
      title: "微信支付",
      value: "wechat",
      disabled: true
    },
    {
      icon: "/static/img/shop/pay/alipay.png",
      title: "支付宝支付",
      value: "alipay",
      disabled: true
    }
    // 20240420注释
    // {
    //   icon: '/static/img/shop/pay/wallet.png',
    //   title: '余额支付',
    //   value: 'wallet',
    //   disabled: true,
    // },
    // {
    //   icon: '/static/img/shop/pay/apple.png',
    //   title: 'Apple Pay',
    //   value: 'apple',
    //   disabled: true,
    // },
    // {
    //   icon: '/static/img/shop/pay/wallet.png',
    //   title: '模拟支付',
    //   value: 'mock',
    //   disabled: true,
    // }
  ];
  const platform = sheep_index.sheep.$platform.name;
  const wechatMethod = payMethods[0];
  if (platform === "WechatOfficialAccount" && channels.includes("wx_pub") || platform === "WechatMiniProgram" && channels.includes("wx_lite") || platform === "App" && channels.includes("wx_app")) {
    wechatMethod.disabled = false;
  }
  wechatMethod.disabled = false;
  const alipayMethod = payMethods[1];
  if (platform === "WechatOfficialAccount" && channels.includes("alipay_wap") || platform === "WechatMiniProgram" && channels.includes("alipay_wap") || platform === "App" && channels.includes("alipay_app")) {
    alipayMethod.disabled = false;
  }
  return payMethods;
}
exports.SheepPay = SheepPay;
exports.getPayMethods = getPayMethods;
