/**
闲闲游戏 SDK

 */

import Gamecfg from "../common/gameCfg";
import { gameMethod } from "../common/gameMethod";
import { PlayerLogin, PlayerLoginPram } from "../common/Xyc";
import { SevBack } from "../common/Xys";
import Config from "../Config";
import { GameEvent, LoadEvent, PlatEvent } from "../data/const/EventConst";
import { AD_VIDEO_KEY, ConstItem, GameErrCode, PayProductParam, ReportRoleType } from "../data/const/TypeConst";
import GameDataCenter from "../data/GameDataCenter";
import GameController from "../GameController";
import UIHelp from "../logic/ui/UIHelp";
import EventMng from "../manager/EventMng";
import NativeManager from "../utils/NativeManager";
import PlatformBase from "./PlatformBase";
export default class PlatformXXGame extends PlatformBase {
    rewardVideo: any
    adId: string;
    constructor() {
        super()
        window["NativeNotify"] = {
            //native平台回调
            OnNativeNotify: function (eventType, eventDataString) {
                try {
                    console.log("NativeNotify OnNativeNotify eventType:" + eventType + ",eventDataString:" + eventDataString);
                    let dataDict = JSON.parse(eventDataString);
                    if (eventType == "XXGameLogin") { //登录
                        EventMng.emit(LoadEvent.LOAD_PROGRESS, 0.75, "login")
                        let params: PlayerLoginPram = {
                            pid: Config.pid,
                            param: [dataDict.uid, dataDict.token]
                        }
                        // if (dataDict.status != 0) {
                        //     GameDataCenter.plat.showErrDialog(GameErrCode.errPlatLogin)
                        //     return
                        // }
                        setTimeout(() => {
                            GameController.network.send(PlayerLogin.url, params, (result: SevBack) => {
                                console.log("后段验证返回", JSON.stringify(result))
                                if (result.type != 1) {
                                    if (gameMethod.isEmpty(result.win) || gameMethod.isEmpty(result.win.msgOut)) {
                                        GameDataCenter.plat.showErrDialog(GameErrCode.errGamePlatLogin)
                                    }
                                    return
                                }
                                EventMng.emit(LoadEvent.LOAD_PROGRESS, 1, "login")
                                if (result.type == 1 && result.platBack) {
                                    GameDataCenter.plat.instance.openId = result.platBack.openid
                                    GameDataCenter.plat.instance.loginParams = [result.platBack.token]
                                    console.log("后端验证成功", GameDataCenter.plat.instance.openId)

                                    // // 加载广告组件
                                    //GameDataCenter.plat.instance.initRewardedVideoAd(AD_VIDEO_KEY.welChest)
                                    // 获取邀请者uuid
                                    let inviter = ""
                                    GameDataCenter.plat.instance.login(inviter)
                                } else {
                                    console.log("PlayerLogin.url 返回数据异常")
                                    if (gameMethod.isEmpty(result.win) || gameMethod.isEmpty(result.win.msgOut)) {
                                        GameDataCenter.plat.showErrDialog(GameErrCode.errGamePlatLogin)
                                    }
                                }
                            })
                        }, 1000)
                    } else if (eventType == "XXGamePay") {  //支付
                        if (dataDict.status == 0) {
                            //充值成功
                            if (Config.paySuccAdokId != null) {
                                clearInterval(Config.paySuccAdokId)
                                Config.paySuccAdokId = null
                            }
                            Config.paySuccAdokIndex = 0
                            Config.paySuccAdokId = setInterval(() => {
                                Config.paySuccAdokIndex++
                                if (Config.paySuccAdokIndex >= 5) {
                                    clearInterval(Config.paySuccAdokId)
                                    Config.paySuccAdokId = null
                                }
                                GameDataCenter.time.sendAdok()
                            }, 1000)
                        } else if (dataDict.status == 1) {
                            //充值失败

                        } else {
                            //待审核中

                        }
                    }
                    else if (eventType == "XXGameLogout") {
                        //退出登录
                        GameDataCenter.plat.instance.restart();
                    }
                    else if (eventType == "getClipboardText") {
                        //EventMng.emit(duihuanEvent.GetText, dataDict.msg);
                    }
                    else {
                        console.log("NativeNotify OnNativeNotify not find eventType: " + eventType);
                    }
                }
                catch (error) {
                    console.log("NativeNotify OnNativeNotify error: " + error.stack);
                }
            },
        }
    }

    showLoginView(): void {
        console.log("平台sdk初始化...")
        EventMng.emit(LoadEvent.LOAD_PROGRESS, 0.25, "login")
        let packageInfo = Gamecfg.packageInfo.getItem(Config.pid)
        NativeManager.CallToNative("OnXXGameInit", {}, "")
    }
    // 上报用户信息
    reportUserInfo(res) {
        console.log("上报用户信息", res)
    }
    // 上报角色数据
    reportRole(event: ReportRoleType): void {
        if (GameDataCenter.sevBack == null) { return }
        if (GameDataCenter.sevBack.userInfo == null) { return }
        console.log("上报角色数据:", event)
        
        let gameServerId = GameDataCenter.sevBack?.userInfo?.a?.sid;
        let serverName = GameDataCenter.user.getServerNameBySid(GameDataCenter.sevBack.userInfo.a.sid) || GameDataCenter.sevBack.userInfo.a.sid;
        let roleId = GameDataCenter.sevBack?.userInfo?.a?.uuid; 
        let level = GameDataCenter.sevBack?.userInfo?.a?.level ?? 1;
        let roleName = GameDataCenter.sevBack?.userInfo?.a?.name;
        let argList = [];
        argList.push({ "Name": "gameServerId", "Value": gameServerId });
        argList.push({ "Name": "gameServerName", "Value": serverName });
        argList.push({ "Name": "roleId", "Value": roleId });
        argList.push({ "Name": "roleLevel", "Value": level.toString() });
        argList.push({ "Name": "roleName", "Value": roleName });
        if (event == ReportRoleType.create) {
            NativeManager.CallToNative("OnXXGamedCreateRole", argList, "")
        } else if (event == ReportRoleType.levelup) {
            NativeManager.CallToNative("OnXXGamedReportRole", argList, "")
        } else if (event == ReportRoleType.online) {
            NativeManager.CallToNative("OnXXGamedEnterGame", argList, "")
        }
    }
    pay(orderId: string, order10cs: string, param: PayProductParam): void {
        let gameServerId = GameDataCenter.sevBack?.userInfo?.a?.sid;
        let serverName = GameDataCenter.user.getServerNameBySid(GameDataCenter.sevBack.userInfo.a.sid) || GameDataCenter.sevBack.userInfo.a.sid;
        let roleId = GameDataCenter.sevBack?.userInfo?.a?.uuid; 
        let level = GameDataCenter.sevBack?.userInfo?.a?.level ?? 1;
        let roleName = GameDataCenter.sevBack?.userInfo?.a?.name;
        let argList = [];
        argList.push({ "Name": "gameServerId", "Value": gameServerId });
        argList.push({ "Name": "gameServerName", "Value": serverName });
        argList.push({ "Name": "itemName", "Value": param.productName });
        argList.push({ "Name": "itemId", "Value": param.productId });
        argList.push({ "Name": "roleId", "Value": roleId });
        argList.push({ "Name": "roleLevel", "Value": level.toString() });
        argList.push({ "Name": "roleName", "Value": roleName });
        argList.push({ "Name": "orderId", "Value": orderId });
        argList.push({ "Name": "price", "Value": param.price.toString() });
        NativeManager.CallToNative("OnXXGamePay", argList, "")
    }
    //打开浏览器打开链接
    openUrl(url: string) {
        NativeManager.CallToNative("OnOpenBrowser", [{ "Name": "url", "Value": url }], "")
    }
    getClipboardText(): void {
        NativeManager.CallToNative("getClipboardText", {}, "");
    }
    reShowLogInView(): void {
        NativeManager.CallToNative("OnXXGameLogin", {}, "");
    }

}