/*
17玩MiniGame(VIVO OPPO)
*/


import Config from "../Config";
import GameController from "../GameController";
import { PlayerLogin, PlayerLoginPram } from "../common/Xyc";
import { AD_VIDEO_KEY, GameErrCode, PayProductParam, ReportRoleType } from "../data/const/TypeConst";
import { SevBack } from "../common/Xys";
import PlatformBase from "./PlatformBase";
import { gameMethod } from "../common/gameMethod";
import GameDataCenter from "../data/GameDataCenter";
import EventMng from "../manager/EventMng";
import { LoadEvent } from "../data/const/EventConst";
import Gamecfg from "../common/gameCfg";
import UIHelp from "../logic/ui/UIHelp";
import Mint from "../utils/mintFilter/index";


export default class PlatformWan17MiniGame extends PlatformBase {

  mint: Mint;
  constructor() {
    super()

    //设置屏幕常亮
    qg.setKeepScreenOn({
      keepScreenOn: true,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    });

    qg.onShow((res) => {
      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)
    });

  }



  showLoginView(): void {
    console.log("初始化&登录SDK")
    qg.asdk.asdkInit((res) => {
      //执行登录授权
      if (res.code.toString() == "0") {
        qg.asdk.asdkLogin((loginRes) => {
          //console.log("登录结果" + JSON.stringify(loginRes))
          if (loginRes.code.toString() == "0") {
            let params: PlayerLoginPram = {
              pid: Config.pid,
              param: [loginRes.data.account.accountid, loginRes.data.account.sessionid]
            }
            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) {
                GameDataCenter.plat.instance.openId = result.platBack.openid
                GameDataCenter.plat.instance.loginParams = [result.platBack.token]
                console.log("后端验证成功", this.openId)
                GameDataCenter.plat.instance.login()
              } else {
                console.log("PlayerLogin.url 返回数据异常")
                if (gameMethod.isEmpty(result.win) || gameMethod.isEmpty(result.win.msgOut)) {
                  GameDataCenter.plat.showErrDialog(GameErrCode.errGamePlatLogin)
                }
              }
            })
          }
          else {
            UIHelp.ShowDialog({ content: loginRes.msg });
          }

        });
      }
    });

  }

  pay(orderId: string, order10cs: string, param: PayProductParam): void {

    let packageInfo = Gamecfg.packageInfo.getItem(Config.pid)
    let payUrl: string = packageInfo.wayhttp + "player/pay/" + Config.pid

    let orderData = {
      callbackurl: payUrl, // 游戏方充值回调地址
      paymoney: param.price.toString(),//游戏充值金额(单位为 元)
      custominfo: "",//游戏透传参数，回调时原样返回
      customorderid: orderId,//游戏透传参数，回调时原样返回
      productdesc: param.productName,//充值描述
      productfeecode: order10cs,
    };

    qg.asdk.asdkPay(orderData, function (res) {
      //console.log("pay return:"+JSON.stringify(res))
      if (res.code.toString() != "0") {
        //console.log("支付失败，失败原因："+res.msg);
      } else {
        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)
      }
    })

  }
  // 上报角色数据
  reportRole(event: ReportRoleType): void {

    let playerinfo = {
      playerid: GameDataCenter.user.uuid,  				//角色ID
      playername: GameDataCenter.user.nickName,				//角色名称
      playerlevel: GameDataCenter.sevBack.userInfo.a?.level?.toString(),						//角色等级
      serverid: GameDataCenter.sevBack.userInfo.a?.sid,					//区服ID
      servername: GameDataCenter.user.serverName				//区服名称
    };
    qg.asdk.asdkSendRole(playerinfo, function (res) {
      //console.log('角色上报返回：'+JSON.stringify(res));
    });
  }


  showRewardAd(adId: string, kid: string, order11Id: string): void {

    qg.asdk.asdkRewardedAd(adId, (res) => {
      //console.log('广告返回：'+JSON.stringify(res));
      if (res.code.toString() != "0") {
        //console.log("看广告失败，失败原因："+res.msg);
      } else {
        GameDataCenter.adVideo.onXiadanSucc() // 改成直接调用,不回调了
      }
    });
  }

  saveAppToDesktop(cb?: Function): void {
    qg.asdk.asdkCreateIcon(function (res) {
      //console.log('创建状态返回：'+JSON.stringify(res))
      if (res.code.toString() != "0") {
        //console.log("创建图标失败，失败原因："+res.msg);
      } else {
        if (cb) {
          cb();
        }
      }
    })
  }

  logout(): void {
    qg.asdk.asdkQuit();
  }

  setClipboard(data: string, cb?: Function) {
    qg.setClipboardData({
      data: data,
      success: function () {
        //console.log("setClipboardData success" );
        UIHelp.ShowTips("已复制到剪切板");
        if (cb) {
          cb();
        }
      },
      fail: function () {
        //console.log("setClipboardData fail");
      },
      complete: function () {
        //console.log("setClipboardData complete");
      }
    });
  }






}