/**
 * 止零h5版本 sdk配置
game_id  h5_cWFlTwvg2aLP2GUA
game_secret   ak_LGOfbtYUmEq7nLvh1gFRN0pDdxw3xPDJ

微信内跳转入口是
https://www.fenyouwangluo.com/sdkh5/weixin/route.html?game_id=h5_AXDpKLjJJoqQNAcn

引入SDK:在html中添加一个script标签就可以了
<script src="https://www.fenyouwangluo.com/h5_sdk_v1.js"></script>
 */
import { gameMethod } from "../common/gameMethod";
import { PlayerLogin, PlayerLoginPram } from "../common/Xyc";
import { SevBack } from "../common/Xys";
import Config from "../Config";
import { LoadEvent } from "../data/const/EventConst";
import { 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 PlatformBase from "./PlatformBase";

export default class PlatformZhilingH5 extends PlatformBase {
    isGoPay: boolean
    constructor() {
        super()
        cc.game.on(cc.game.EVENT_SHOW, () => {
            if (this.isGoPay) {
                this.isGoPay = false
                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)
            }
        })
    }
    private getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substring(1).match(reg);
        if (r != null) {
            return decodeURIComponent(r[2]);
        } else {
            return null;
        }
    }
    showLoginView(): void {
        console.log("初始化&登录SDK")
        EventMng.emit(LoadEvent.LOAD_PROGRESS, 0.25, "login")

        try {
            zlGame.initSdk().then(res => {
                console.log("初始化成功", res)
                EventMng.emit(LoadEvent.LOAD_PROGRESS, 0.75, "login")
                let user_id = this.getQueryString("user_id")
                let user_token = this.getQueryString("user_token")
                let game_id = this.getQueryString("game_id")

                let params: PlayerLoginPram = {
                    pid: Config.pid,
                    param: [user_id, user_token, game_id]
                }
                GameController.network.send(PlayerLogin.url, params, (result: SevBack) => {
                    console.log("后段验证返回")
                    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) {
                        this.openId = result.platBack.openid
                        this.loginParams = [result.platBack.token]
                        console.log("后端验证成功", this.openId)
                        this.login()
                    } else {
                        console.log("PlayerLogin.url 返回数据异常")
                        if (gameMethod.isEmpty(result.win) || gameMethod.isEmpty(result.win.msgOut)) {
                            GameDataCenter.plat.showErrDialog(GameErrCode.errGamePlatLogin)
                        }
                    }
                })
            })
        } catch (error) {
            UIHelp.ShowTips("----" + error)
            UIHelp.ShowDialog({ content: error })
        }
    }

    pay(orderId: string, order10cs: string, param: PayProductParam): void {
        console.log("===调起zhiling支付")
        // UIHelp.ShowTips("调起zhiling支付")

        let req: { role_id: string, role_name: string, zone_id: string, server_id: string, server_name: string, role_level: number, role_vip: number, game_trade_no: string, product_id: string, product_name: string, amount: number, extra_params: string } = {
            role_id: GameDataCenter.sevBack.userInfo?.a?.uuid,
            role_name: GameDataCenter.sevBack.userInfo?.a?.name,
            zone_id: GameDataCenter.sevBack.userInfo?.a?.sid,
            server_id: GameDataCenter.sevBack.userInfo?.a?.sid,
            server_name: GameDataCenter.sevBack.qufuList[GameDataCenter.sevBack.userInfo?.a?.sid].name,
            role_level: GameDataCenter.sevBack.userInfo?.a?.level,
            role_vip: 1,
            game_trade_no: orderId,
            product_id: param.productId,
            product_name: param.productName,
            amount: param.price * 100,
            extra_params: orderId
        }
        if (Config.pid == "1310") {
            req.amount = req.amount / 100;
        }

        zlGame.startPay(req).then(res => {
            console.log("创建订单成功:", res)
        })
    }
    // 上报角色数据
    reportRole(event: ReportRoleType): void {
        // • 初始化成功后调⽤. • 进⼊区服、创⻆、⻆⾊等级更新、战⼒更新时调⽤.
        if (GameDataCenter.sevBack == null) { return }
        if (GameDataCenter.sevBack.userInfo == null) { return }
        console.log("上报角色数据:", event)
        let onlineTime = 0
        if (GameDataCenter.time.onlineTime > 0) {
            onlineTime = GameDataCenter.time.sevTime - GameDataCenter.time.onlineTime
        }
        let eventName = ""
        switch (event) {
            case ReportRoleType.create:
                eventName = "create_role"
                break;
            // case ReportRoleType.delete:
            //     eventName = "delete_user"
            //     break;
            case ReportRoleType.levelup:
                eventName = "role_upgrade"
                break;
            case ReportRoleType.offline:
                eventName = "role_upgrade"
                break;
            case ReportRoleType.online:
                eventName = "role_upgrade"
                break;
            case ReportRoleType.other:
                eventName = "role_upgrade"
                break;
            default:
                break;
        }
        let req = {
            role_id: GameDataCenter.sevBack.userInfo?.a?.uuid,
            role_name: GameDataCenter.sevBack.userInfo?.a?.name,
            zone_id: GameDataCenter.sevBack.userInfo?.a?.sid,
            server_id: GameDataCenter.sevBack.userInfo?.a?.sid,
            server_name: GameDataCenter.sevBack.qufuList[GameDataCenter.sevBack.userInfo?.a?.sid].name,
            role_level: GameDataCenter.sevBack.userInfo?.a?.level,
            role_vip: 1,
            avatar: GameDataCenter.sevBack.userInfo?.a?.head,
            nickname: GameDataCenter.sevBack.userInfo?.a?.name
        }

        // set_userinfo 设置⻆⾊基本信息，如头像、昵称
        zlGame.reportRoleInfo(eventName, req).then(res => {
            console.log("数据上报成功:", event, eventName, res)
            UIHelp.ShowTips("数据上报成功:" + eventName)
        })
    }
    openService() {
        zlGame.showCustomerService().then(res => {
            console.log("展⽰客服界⾯成功", res)
        })
    }
}