import { BridgePlugin } from '../bridge';

interface IWeixinPayParam {
  payOrderInfo: {
    /**
     * https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
     * 微信支付强制需要的参数
     */
    // APPID: 强制和微信绑定
    appId: string;
    // 预付单ID
    prepayId: string;
    partnerId: string;
    nonceStr: string;
    timeStamp: string;
    packageValue: string;
    sign: string;
  };
}

interface IWeixinPayResult {
  // 支付结果
  // '0':成功;
  // '1':支付失败/接口返回错误;
  // '2':取消;
  payResult: '0' | '1' | '2';
  // 状态描述
  message: string;
}

export class WeiXinPay<K> extends BridgePlugin<IWeixinPayParam, K>{
  protected get ntvPluginName() {
    return 'ntv_pay_weixin';
  }

  protected get availableMinVersion() {
    return '1.0.0';
  }

  protected bridgeExecChrome(pluginName: string | undefined): void {
    throw new Error('WeiXinPay() 未针对当前环境实现');
  }
}

/**
 * https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3
 * Tips: iPhone如果点击应用间返回按钮，会接收不到回调信息
 * Tips: 微信支付，如果没有安装微信直接提醒无法进行支付
 */
export const weixinPay = (param: IWeixinPayParam) => {
  return new WeiXinPay<IWeixinPayResult>(param).invokeOnce().then(result => {
    return result.cbData;
  })
}