
/**
 * 凤起充值
 */

import { __PayData } from "../game/data/sdata/SManage";
import FqLogin from "../login/FqLogin";
import Main from "../main/Main";
import { HttpStateType, ReveData } from "../util/CHttp";

/**
 * 友盟 广告事件定义
 */
export const UM_EVENT_ID = cc.Enum({
    ad_pdl_t_0: 'ad_pdl_t_0',//潘多拉铁盒广告开始
    ad_pdl_t_1: 'ad_pdl_t_1',//潘多拉铁盒广告观看结束

    ad_pdl_m_0: 'ad_pdl_m_0',//潘多拉魔盒广告开始
    ad_pdl_m_1: 'ad_pdl_m_1',//潘多拉魔盒广告观看结束

    ad_power_0: 'ad_power_0',//看广告购买体力
    ad_power_1: 'ad_power_1',//看广告购买体力

    ad_money_0: 'ad_money_0',//看广告购买金币
    ad_money_1: 'ad_money_1',//看广告购买金币

    ad_fuhuo_0: 'ad_fuhuo_0',//复活
    ad_fuhuo_1: 'ad_fuhuo_1',//复活

    ad_color_2_0: 'ad_color_2_0',//2关颜色灯
    ad_color_2_1: 'ad_color_2_1',//2关颜色灯

    ad_color_5_0: 'ad_color_5_0',//5关颜色灯
    ad_color_5_1: 'ad_color_5_1',//5关颜色灯

    ad_wish_0: 'ad_wish_0',//每日祝福
    ad_wish_1: 'ad_wish_1',//每日祝福
});

export default class FqPay {

    public main: Main;
    public _payData: __PayData;
    /**
     * 充值回调
     * result : 0 成功  -1  失败
     */
    public callback: (result: number) => void;

    private orderId:number;//订单id

    constructor(main: Main) {
        this.main = main
    }
    /**
     * 
     * @param id 商品id
     */
    public pay(payData: __PayData, callback: (result: number) => void) {
        this.callback = callback
        this._payData = payData
        //创建订单
        this.createOrder((orderId) => {
            this.orderId = orderId
            this.openFqPay('' + orderId)
        })
    }
    /**
     * 创建订单
     * @param callback 
     */
    private createOrder(callback: (orderId: number) => void) {
        let msg = {
            payId: this._payData.id
        }
        this.main.gameHttp.sendJson('pay/v1/createOrder', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    callback(reve.data.orderId)
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }
    /**
     * String productCode = json.getString("productCode");
            String orderAmount = json.getString("orderAmount");
            String subject = json.getString("subject");
            String roleName = json.getString("roleName");
            String cpBillNo = json.getString("cpBillNo");
            String serverId = json.getString("serverId");
            String extraInfo = json.getString("extraInfo");

            paymentInfo.setOrderAmount(orderAmount);            //订单金额           必须字段
            paymentInfo.setSubject(subject);              //商品名             必须字段
            paymentInfo.setRoleName(roleName);           //角色名             必须字段
            paymentInfo.setCpBillNo(cpBillNo);    //CP订单号           必须字段
            paymentInfo.setUid(uid);                    //登录时成功拿到的UID  必须字段
            paymentInfo.setServerId(serverId);               //区服ID             必须字段
            paymentInfo.setExtraInfo(extraInfo);      //拓展信息            必须字段
     * @param orderId 
     */
    private openFqPay(orderId: string) {
        let player = this.main.player
        let user = this.main.userData
        let msg = {
            productCode:''+this._payData.id,
            productCodeAndroid:this._payData.googleId,
            productCodeIos:this._payData.iosId,
            orderAmount:''+this._payData.usd,
            subject:''+this._payData.name,
            roleName:player.role.name,
            cpBillNo:''+orderId,
            serverId:''+user.zone.id,
            extraInfo:''+orderId
        }

        if(cc.sys.isNative){
            if (cc.sys.os === cc.sys.OS_ANDROID) {
                let myself = this
                window._fqAndroidPayCallback = (str) => {
                    console.log('window._fqAndroidPayCallback', str)
                    myself.getPay()
                    return '1'
                }
    
                let className = "com/funqueue/android/xsdmx/MainActivity";
                let methodName = "pay";
                let methodSignature = "(Ljava/lang/String;)V";
                jsb.reflection.callStaticMethod(className, methodName, methodSignature, JSON.stringify(msg));
            } else if (cc.sys.os === cc.sys.OS_IOS || cc.sys.os === cc.sys.OS_OSX) {
                let myself = this
                window._fqIosPayCallback = (str)=>{
                    console.log('window._fqIosPayCallback' , str)
                    myself.getPay()
                    return '1'
                }
                jsb.reflection.callStaticMethod('AppController', 'pay:',JSON.stringify(msg));
            }
        }
       
    }
    private getPay() {
        let msg = {
            orderId: this.orderId
        }
        this.main.gameHttp.sendJson('pay/v1/checkPay', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    this.callback(0)
                    this.main.showReward(reve)
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }
    /**
     * 用户观看视频
     * result : -1:广告还未准备好
     *          0:广告未播放完
     *          1:播放完成，给奖励
     */
    public adVideo(callback:(result: number)=>void,eventId1:string,eventId2:string){
        cc.log('用户观看视频 adVideo');
        this.callback = callback;
        if(cc.sys.isNative){
            if (cc.sys.os === cc.sys.OS_ANDROID) {
                FqLogin.commitEvent(eventId1,'','')
                let myself = this
                window._fqAndroidAdCallback = (str) => {
                    console.log('window._fqAndroidAdCallback', str)
                    if(str == '1'){
                        myself.callback(1)
                        FqLogin.commitEvent(eventId2,'','')
                    }else if(str == '-1'){
                        myself.callback(-1)
                    }else{
                        myself.callback(0)
                    }
                    return '1'
                }
    
                let className = "com/funqueue/android/xsdmx/MainActivity";
                let methodName = "showRewardedVideo";
                let methodSignature = "(Ljava/lang/String;)V";
                jsb.reflection.callStaticMethod(className, methodName, methodSignature, ' jlsp1');
            } else if (cc.sys.os === cc.sys.OS_IOS) {
                FqLogin.commitEvent(eventId1,'','')
                let myself = this
                window._fqIosAdCallback = (str)=>{
                    console.log('window._fqIosAdCallback' , str)
                    if(str == '1'){
                        FqLogin.commitEvent(eventId2,'','')
                        myself.callback(1)
                    }else if(str == '-1'){
                        myself.callback(-1)
                    }else{
                        myself.callback(0)
                    }
                    return '1'
                }
                jsb.reflection.callStaticMethod('AppController', 'showRewardedVideo:','jlsp1');
            }else{
                this.callback(1)
            }
        }else{
            this.callback(1)
        }
        
    }



}
