import { DataManager } from './../../libs/tw/manager/DataManager';
import { ModuleTypes } from './../types/sceneTypes';

import Scene from "../views/Scene";
import PanelCtrl from "../ctrls/panelCtrl";
import SceneCtrl from '../ctrls/sceneCtrl';
import { NetManager } from '../../libs/tw/manager/NetManager';
import Loading from '../loading/Loading';
import { ToolManager } from '../toolManager/ToolManager';
import { Buried } from '../../libs/tw/util/Buried';
import { GCache } from '../../libs/tc/util/GCache';
import { NetName } from '../../libs/tw/enum/NetName';
import { NetPromise } from '../NetPromise';
import { MConfigs } from '../MConfigs';
import { LotteryType } from '../../libs/tw/enum/LotteryType';
import TopLayerCtrl from '../ctrls/topLayerCtrl';
import { GDispatcher } from '../../libs/tc/util/GDispatcher';
import { NetBuried } from '../NetBuried';
export default class StartScene1 extends Scene {
    public mainbg:eui.Image;
    public petGroup:eui.Group;
    public petBodyGroup:eui.Group;
    public group:eui.Group;
    public petMsgText:eui.Label;
    public handIco:eui.Image;
    public handGroup:eui.Group;
    public joinBtn:eui.Button;


    

    // 用于控制全局音乐是否播放和音乐按钮状态
    public static isPlayMusic: boolean = true;

    public get skinKey() { return 'StartScene' }
    public get groupName() { return 'start_scene' }

    constructor(data?) {
        super(data);//95fcf3
    }

    initEvents() {
        super.initEvents();
        this.joinBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_joinBtn, this);
    }


    clearMusicTime

    removeEvents() {
        super.removeEvents();
        this.joinBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_joinBtn, this);
        clearInterval(this.clearMusicTime);
    }


   

    onTap_joinBtn(){
        NetBuried.ins.clickLog(31)
        window["kuwoApi"].navigateToApp('http://3000021.sit.kuwo-inc.com/db.html?pageType=2', '', '');
    }

    clearPetTips=[]

    /**
     * 更新宠物顶部消息框
     */
    updatePetTips(val?){
        clearInterval(this.clearPetTips[1])
        clearTimeout(this.clearPetTips[2])
        clearTimeout(this.clearPetTips[3])
        this["petMsgShow"].play(0)
        if(val){
            this.setPetMsgText(val)
        }else{
            this.setPetMsgText()
        }

        this.clearPetTips[3]=setTimeout(()=>{
            this["petMsgHide"].play(0)
        },5000)
        
        this.clearPetTips[1]=setInterval(()=>{
            this["petMsgShow"].play(0)
            this.setPetMsgText()
            this.clearPetTips[2]=setTimeout(()=>{
                this["petMsgHide"].play(0)
            },5000)
        },7000)
    }

    setPetMsgText(val?){
        if(val){
            this.petMsgText.text=val
        }else{
            this.petMsgText.text=MConfigs.msgsAuto[this.randomFrom(0,MConfigs.msgsAuto.length-1)]
        }
    }

    randomFrom(lowerValue,upperValue){
        return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
    }

   



    showToast(val) {
        //console.log(val)
        TopLayerCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: val });
    }

    

    start(data?) {
        super.start(data);
        this.percentHeight = 100;
    }

    private bgtexture: egret.Texture;
    protected async preLoadRes() {
        //加载背景并设置背景图片
        this.bgtexture = await RES.getResAsync('startBg_jpg');
        return new Promise(function (resolve, reject) {
            resolve();
        });
    }

    protected onSkinComplete(): void {
        super.onSkinComplete();

        this.mainbg.texture = this.bgtexture;

        this.once(egret.Event.ADDED_TO_STAGE, this.onLoad, this);
        if (this.stage) {
            this.onLoad();
        }
        console.log("test")

        this.doBuried()
    }

   
	private doBuried() {
		NetBuried.ins.showLog(31)
    }
    
  

    pets = []

    LoadPetClip(url) {
        return new Promise((resolev, reject) => {
            var parser = new window["SVGA"].Parser();
            parser.load(url, (videoItem) => {
                let pet = new window["SVGA"].EgretMovieClip(videoItem);
                pet.visible = false;
                this.petBodyGroup.addChild(pet);
                resolev(pet)
            }, (error) => {
                reject(error);
            })
        })
    }

    private async onLoad() {
        await this.initUI();
    }

    petData

    url = "//yun.duiba.com.cn/db_games/activity/kwyyyc_20190809/petAn"

    handAn

    shengjiAn

    petAn

    private async initUI() {
        Loading.instace.show()

        this["handLoop"].play(0)
        this["handLoop"].items.forEach((item) => {
            item.tween.loop = true;
        })

        //if(localStorage.handIco){
            this.handIco.visible=false
       // }

        this.petAn = await this.LoadPetClip(`${this.url}/pet_lv_5/auto.svga`);
        this.petAn.visible=true;
        this.petAn.y=60;
        this.petAn.gotoAndPlay(0, true)
        Loading.instace.hide()

        this.updatePetTips()
        
    }
}
