import Scene from "../views/Scene";
import { GCache } from "../../libs/tc/util/GCache";
import PanelCtrl from "../ctrls/panelCtrl";
import { ModuleTypes } from "../types/sceneTypes";
import { yzwNet } from "../NetConst";
import ToastCtrl from "../ctrls/toastCtrl";
import { DataManager } from "../../libs/tw/manager/DataManager";
import SceneCtrl from "../ctrls/sceneCtrl";
import Waiting from "../waiting/Waiting";
import GameConst, { wait } from "../GameConst";
import { Buried } from "../../libs/tw/util/Buried";
import { NetManager } from "../../libs/tw/manager/NetManager";
import instance from "../views/layers";
import { loadSvga } from "../loadSvga";
import { getResPath } from "../utils";
import { playBg, playSound } from "../ctrls/soundCtrl";
import { GFun } from "../../libs/tc/util/GFun";
import {Howl, Howler} from 'howler';
import iswx from "../iswx";
import { GDispatcher } from "../../libs/tc/util/GDispatcher";

export default class StartScene extends Scene {
    public scroll:eui.Scroller;
    public scrollGroup:eui.Group;

    public walkcow:eui.Group
    public cowlabGroup:eui.Group;
    public cowlab:eui.Image;

    public one:eui.Group;
    public oneLab:eui.Label;
    public onecow:eui.Group;
    public cowMask:eui.Rect;

    public twosun:eui.Image;
    public twowing:eui.Image;
    public twobucket:eui.Image;
    public twoflow1:eui.Image;
    public twoflow2:eui.Image;
    public twoflow3:eui.Image;
    public twoflow4:eui.Image;
    public twobired:eui.Group;
    public twosit:eui.Group;
    public twostand:eui.Group;

    public threesit:eui.Group;
    public threestand:eui.Group;
    public lab1group:eui.Group;
    public lab2group:eui.Group;

    public fourdoc:eui.Group;
    public fourswing:eui.Group;
    public fourstand:eui.Group;
    public fourdoc2:eui.Group;

    public fivebed:eui.Group;
    public fivemu:eui.Group;
    public fiveturn:eui.Group;

    public sixfan:eui.Image;
    public window1:eui.Image;
    public window2:eui.Image;
    public dream:eui.Group;
    public lastscene:eui.Group;
    public lastlab:eui.Label;


    public lab1:eui.Image;
    public lab2:eui.Image;


    public cowwalk:any;
    public cowone:any;
    public biredtwo:any;
    public sittwo:any;
    public standtwo:any;

    public sitthree:any;
    public standthree:any;

    public docfour:any;
    public swingfour:any;
    public standfour:any;
    public doc2four:any;

    public bedfive:any;

    public turnfive:any;
    public dream2:any;
    public bgMusic:any;
    public lab1music:any;
    public lab2music:any;
    public lab3music:any;
    public lab4music:any;
    public lab5music:any;
    public lab6music:any;
    public lab7music:any;
    public lab8music:any;
    public lab9music:any;
    public lab10music:any;
    public lab11music:any;
    public lab12music:any;
    public lab13music:any;
    public lab14music:any;
    public lab15music:any;
    public lab16music:any;
    
    public bg1music:any;
    public bg2music:any;
    public bg3music:any;
    public bg4music:any;
    public bg5music:any;
    public bg6music:any;
    public bg7music:any;
    public bg8music:any;

    public bg1channel:any;
    public bg2channel:any;
    public bg3channel:any;
    public bg4channel:any;
    public bg5channel:any;
    public bg6channel:any;
    public bg7channel:any;
    public bg8channel:any;

    public musiconBtn:eui.Button;
    public musicoffBtn:eui.Button;
    public slide:eui.Image;
    public xfbtn:eui.Image;
    public lastani:any;

    public onex;
    public oney;
    public isFirst:boolean;
    public iswalkTween:boolean = false;
    public isGo:boolean = true
    public isend:boolean = true;
    public lastend:boolean = false;

    public startExposure:any;
    public slideExposure:any;
    public xfExposure:any;
    public cowPoint = [{x:265,y:2075},{x:122,y:2250},{x:630,y:2638},
        {x:203,y:3239},{x:658,y:3525},{x:95,y:4585},
        {x:695,y:4910},{x:80,y:5704},{x:465,y:5955},
        {x:90,y:6573},{x:475,y:6862},{x:275,y:7106}]
    protected get skinKey() { return 'Start' }

    public static instance:StartScene;


    addoffsety:number = 10;
    addflag:Array<number> = new Array(25);
    finani:boolean = true;
    finaframe:number = -1;
    constructor(data){
        super();
        // Waiting.instance.hide();
        StartScene.instance = this;
        
        // this.onecow.visible = false;
        
        this.initUI(data)
        this.initBuried();
        this.isFirst = true;
        // window['setBgm']("bgmMusic",false);
        window['setBgm']("bg1",true);
        window['setBgm']("bg1",false);
        window['setBgm']("bg2",false);
        window['setBgm']("bg3",false);
        window['setBgm']("bg4",false);
        window['setBgm']("bg5",false);
        window['setBgm']("bg6",false);
        window['setBgm']("bg7",false);
        window['setBgm']("bg8",false);
        
        window['setBgm']("lab1music",false);
        window['setBgm']("lab2music",false);
        window['setBgm']("lab3music",false);
        window['setBgm']("lab4music",false);
        window['setBgm']("lab5music",false);
        window['setBgm']("lab6music",false);
        window['setBgm']("lab7music",false);
        window['setBgm']("lab8music",false);
        window['setBgm']("lab9music",false);
        window['setBgm']("lab10music",false);
        window['setBgm']("lab11music",false);
        window['setBgm']("lab12music",false);
        window['setBgm']("lab13music",false);
        window['setBgm']("lab14music",false);
        window['setBgm']("lab15music",false);
        window['setBgm']("lab16music",false);
        
    //    this.loadMusic();
       window['setBgm']("bgmMusic",true);
       
        
    }

    onSkinComplete() {
        super.onSkinComplete();
        
        // this.initUI();
    }

    onEnterFrame(){
        this.sixfan.rotation+=0.5
        // this.MoveScreen();
        // console.log("lalalala",this.walkcow.y)
        
        if(this.startMove){
            if(this.scroll.viewport.scrollV<1206){
                this.scroll.viewport.scrollV = 1206;
                this.endY = this.scroll.viewport.scrollV 
            }
            window['setBgm']("lab1music",false);
        }
        if(this.endY>=1200){
            window['setBgm']("bgmMusic",true);
            if(this.walkcow.y+10>=this.endY+this.scroll.height){
                egret.Tween.removeTweens(this.walkcow);
                this.iswalkTween = false
                this.isGo = true
            }else{
                if(this.isGo){
                    this.isGo = false;
                    for(let n =this.cowPoint.length-1;n>1;n--){
                        if(this.walkcow.y>this.cowPoint[n-1].y-1){
                            if(n%2 == 0){
                                this.walkcow.scaleX = -1
                                this.cowlabGroup.scaleX = -1
                            }else{
                                this.walkcow.scaleX = 1
                                this.cowlabGroup.scaleX = 1
                            }
                            this.walkCowMove(this.cowPoint[n].x,this.cowPoint[n].y)
                            this.iswalkTween = true
                            break
                        }
                    }
                }
                
            }
            if(this.iswalkTween){
           
                if(this.walkcow.y >2099 && this.walkcow.y<(2101+this.addoffsety)&&this.addflag[0]!=1){
                    this.addflag[0]=1;
                    this.cowlabAni(2);
                }
                if(this.walkcow.y >2299 && this.walkcow.y<(2301+this.addoffsety)&&this.addflag[1]!=1){
                    this.addflag[1]=1;
                    this.cowlab.source = "cowlab2_png"
                    this.cowlabAni(1);
                    window['setBgm']("lab3music",true,this.SoundComplete);
                }
                if(this.walkcow.y > 2478 && this.walkcow.y<(2480+this.addoffsety)&&this.addflag[2]!=1){
                    this.addflag[2]=1;
                    this.cowlabAni(2);
                }
                // if(this.walkcow.y > 2590 && this.walkcow.y<2592){
                //     this.cowlab.source = "cowlab3_png"
                //     this.cowlabAni(1);
                //     window['setBgm']("lab4music",true);
                //     window['setBgm']("bg1",false);
                //     window['setBgm']("bg2",false);
                //     window['setBgm']("bg3",true);
                //     window['setBgm']("bg4",true);
                    
                // }
                if(this.walkcow.y > 2670 && this.walkcow.y<(2672+this.addoffsety)&&this.addflag[3]!=1){
                    this.addflag[3] = 1;
                    this.cowlab.source = "cowlab4_png"
                    this.cowlabAni(1);
                    window['setBgm']("lab5music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",true);
                    window['setBgm']("bg4",true);
                }
                if(this.walkcow.y > 2954 && this.walkcow.y<(2956+this.addoffsety)&&this.addflag[4]!=1){
                    this.addflag[4]=1;
                    this.cowlabAni(2);
                }
                if(this.walkcow.y > 3031 && this.walkcow.y<(3033+this.addoffsety)&&this.addflag[5]!=1){
                    this.addflag[5] = 1;
                    this.cowlab.source = "cowlab5_png"
                    this.cowlabAni(1);
                    window['setBgm']("lab6music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",true);
                    window['setBgm']("bg4",true);
                }
                if(this.walkcow.y > 3298 && this.walkcow.y<(3300+this.addoffsety)&&this.addflag[6]!=1){
                    this.addflag[6] = 1;
                    this.cowlabAni(2);
                }
                if(this.walkcow.y > 3340 && this.walkcow.y<(3342+this.addoffsety)&&this.addflag[7]!=1){
                    this.addflag[7] = 1;
                    this.cowlab.source = "cowlab6_png"
                    this.cowlabAni(1);
                    // console.log("fhesdoihsedoifbsdu",this.scrollGroup)
                    this.scrollGroup.setChildIndex(this.walkcow,20)
                    window['setBgm']("lab7music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",true);
                    window['setBgm']("bg4",true);
                }
                if(this.walkcow.y > 3438 && this.walkcow.y<(3440+this.addoffsety)&&this.addflag[8]!=1){
                    this.addflag[8] = 1;
                    this.cowlabAni(2);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",true);
                    window['setBgm']("bg4",true);
                }
                if(this.walkcow.y > 3715 && this.walkcow.y<(3717+this.addoffsety)&&this.addflag[9]!=1){
                    this.addflag[9] = 1;
                    this.cowlab.source = "cowlab7_png"
                    this.cowlabAni(1);
                    window['setBgm']("lab8music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",true);
                }
                if(this.walkcow.y > 4043 && this.walkcow.y<(4045+this.addoffsety)&&this.addflag[10]!=1){
                    this.addflag[10] = 1;
                    this.cowlab.source = "cowlab8_png"
                    window['setBgm']("lab9music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",true);
                }
                if(this.walkcow.y > 4432 && this.walkcow.y<(4434+this.addoffsety)&&this.addflag[11]!=1){
                    this.addflag[11]=1;
                    this.cowlabAni(2);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",true);
                }
                if(this.walkcow.y > 4690 && this.walkcow.y<(4692+this.addoffsety)&&this.addflag[12]!=1){
                    this.addflag[12]=1;
                    this.cowlab.source = "cowlab9_png"
                    this.cowlabAni(1);
                    window['setBgm']("lab10music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",true);
                }
                if(this.walkcow.y > 4835 && this.walkcow.y<(4837+this.addoffsety)&&this.addflag[13]!=1){
                    this.addflag[13]=1;
                    this.cowlabAni(2);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",true);
                }
                if(this.walkcow.y > 5266 && this.walkcow.y<(5268+this.addoffsety)&&this.addflag[14]!=1){
                    this.addflag[14]=1;
                    this.cowlab.source = "cowlab10_png"
                    this.cowlabAni(1);
                    window['setBgm']("lab11music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",true);
                }
                if(this.walkcow.y > 5653 && this.walkcow.y<(5655+this.addoffsety)&&this.addflag[15]!=1){
                    this.addflag[15] = 1;
                    this.cowlabAni(2);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",true);
                }
                if(this.walkcow.y > 5774 && this.walkcow.y<(5776+this.addoffsety)&&this.addflag[16]!=1){
                    this.addflag[16] = 1;
                    this.cowlab.source = "cowlab11_png"
                    this.cowlabAni(1);
                    window['setBgm']("lab12music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",false);
                }
                if(this.walkcow.y > 5904 && this.walkcow.y<(5906+this.addoffsety)&&this.addflag[17]!=1){
                    this.addflag[17] = 1;
                    this.cowlabAni(2);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",false);
                }
                if(this.walkcow.y > 6079 && this.walkcow.y<(6081+this.addoffsety)&&this.addflag[18]!=1){
                    this.addflag[18]=1;
                    this.cowlab.source = "cowlab12_png"
                    this.cowlabAni(1);
                    window['setBgm']("lab13music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",false);
                }
                if(this.walkcow.y > 6474 && this.walkcow.y<(6476+this.addoffsety)&&this.addflag[19]!=1){
                    this.addflag[19]=1;
                    this.cowlabAni(2);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",false);
                }
                if(this.walkcow.y > 6667 && this.walkcow.y<(6669+this.addoffsety)&&this.addflag[20]!=1){
                    this.addflag[20] = 1;
                    this.cowlab.source = "cowlab13_png"
                    this.cowlabAni(1);
                    window['setBgm']("lab14music",true);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",false);
                    window['setBgm']("bg7",true);
                }
                if(this.walkcow.y > 6894 && this.walkcow.y<(6896+this.addoffsety)&&this.addflag[21]!=1){
                    this.addflag[21] = 1;
                    this.cowlabAni(2);
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",false);
                    window['setBgm']("bg7",true);
                }
                if(this.walkcow.y>=7105){
                    if(this.isend){
                        this.isend = false;
                        window['setBgm']("bg1",false);
                        window['setBgm']("bg2",false);
                        window['setBgm']("bg3",false);
                        window['setBgm']("bg4",false);
                        window['setBgm']("bg5",false);
                        window['setBgm']("bg6",false);
                        window['setBgm']("bg7",false);
                        // window['setBgm']("bg8",true);
                        this.window1.visible = false;
                        this.window2.visible = false;

                        
                        this.loadDream();
                    }
                    
                    // this.iswalkTween = false
                }
                
            }
            if(this.lastend){
                if(this.scroll.viewport.scrollV+this.scroll.height>7800){
                    this.lastend = false
                    window['setBgm']("bg1",false);
                    window['setBgm']("bg2",false);
                    window['setBgm']("bg3",false);
                    window['setBgm']("bg4",false);
                    window['setBgm']("bg5",false);
                    window['setBgm']("bg6",false);
                    window['setBgm']("bg7",false);
                    window['setBgm']("bg8",false);
                    
                    
                    // wait(800).then(async()=>{
                    //     this.lastani = await loadSvga(getResPath() + 'resource/assets/svgas/lastani.svga',this.lastscene) as SVGA.EgretMovieClip;
                    //     this.lastani.lockStep = true;
                    //     this.lastani.y = 270
                    // })
                    // // console.log(this.scroll)
                    // // egret.Tween.get(this.scroll.viewport)
                    // // .to({scrollV:this.scroll.viewport.percentHeight-this.scroll.height})
                    // wait(1500).then(()=>{
                    //     this.typerEffect(this.lastlab,"没错，我们就是五好奶牛\n快来“认养”我们\n每天都喝好牛奶\n买牛奶，不如认养一头牛！",170)
                    
                    // })
                    
                }
            }
            
            

            if(this.walkcow.y > 3715){// && this.walkcow.y<3722){
                this.scrollGroup.setChildIndex(this.walkcow,26)
            }
            if(this.walkcow.y > 4405){ //&& this.walkcow.y<4412){
                this.scrollGroup.setChildIndex(this.walkcow,27)
            }
            if(this.walkcow.y > 5135 ){//&& this.walkcow.y<5140){
                this.scrollGroup.setChildIndex(this.walkcow,38)
            }
            if(this.walkcow.y > 6115){ //&& this.walkcow.y<6122){
                this.scrollGroup.setChildIndex(this.walkcow,45)
            }
            
        }
        if(this.musicoffBtn.visible){
            if(GFun.isIOS){
                window['setBgm']("bgmMusic",false);
                window['setBgm']("bg1",false);
                window['setBgm']("bg2",false);
                window['setBgm']("bg3",false);
                window['setBgm']("bg4",false);
                window['setBgm']("bg5",false);
                window['setBgm']("bg6",false);
                window['setBgm']("bg7",false);
                window['setBgm']("bg8",false);
                
                window['setBgm']("lab1music",false);
                window['setBgm']("lab2music",false);
                window['setBgm']("lab3music",false);
                window['setBgm']("lab4music",false);
                window['setBgm']("lab5music",false);
                window['setBgm']("lab6music",false);
                window['setBgm']("lab7music",false);
                window['setBgm']("lab8music",false);
                window['setBgm']("lab9music",false);
                window['setBgm']("lab10music",false);
                window['setBgm']("lab11music",false);
                window['setBgm']("lab12music",false);
                window['setBgm']("lab13music",false);
                window['setBgm']("lab14music",false);
                window['setBgm']("lab15music",false);
                window['setBgm']("lab16music",false);

            }
            
        }
        
    }
    initBuried(){
        let num ;
        let shareid;
        if(GameConst.getQueryString("isTaobao")){
            num = 1
            shareid = 0

            let xfDpmIn = Buried.connectDpm(110, 3, 1); // 点击埋点   
            let xfDcmIn = Buried.connectDcm(202, 0, 0); // 曝光埋点
            let xfDomIn = Buried.connectDom(num,shareid)
            this.xfExposure = Buried.createExposure(xfDpmIn, xfDcmIn,xfDomIn);
            NetManager.ins.showLog(this.xfExposure);
        }else{
            if(iswx()){
                num = 2
                if(GameConst.getQueryString("isShare")){
                    shareid = 2
                }else{
                    shareid = 1
                }
            }
        }
        let startDpmIn = Buried.connectDpm(110, 1, 1); // 点击埋点   
        let startDcmIn = Buried.connectDcm(202, 0, 0); // 曝光埋点
        let startDomIn = Buried.connectDom(num,shareid)
        this.startExposure = Buried.createExposure(startDpmIn, startDcmIn,startDomIn);
        NetManager.ins.showLog(this.startExposure);
        let slideDpmIn = Buried.connectDpm(110, 2, 1); // 点击埋点   
        let slideDcmIn = Buried.connectDcm(202, 0, 0); // 曝光埋点
        let slideDomIn = Buried.connectDom(num,shareid)
        this.slideExposure = Buried.createExposure(slideDpmIn, slideDcmIn,slideDomIn);
        NetManager.ins.showLog(this.slideExposure);
    }
    labchannel:any;
    async initUI(data) {
        
        
        // this.onecow.addChild(this.cowone)
        this.onecow.mask = this.cowMask;
        this.scroll.height = 750/window.innerWidth * window.innerHeight
        this.scroll.viewport.scrollV = 92;
        this.onex = this.one.x;
        this.oney = this.one.y;
        
        egret.Tween.get(this.slide,{loop:true})
        .set({alpha:1})
        .to({alpha:0},1000)
        .to({alpha:1},1000)
        this.onComplete();
        
        if(iswx()){
            this.xfbtn.visible = false;
        }else{
            this.xfbtn.visible = true;
            this.xfbtn.y = 750/window.innerWidth * window.innerHeight - 450;
        }
        
        this.walkCowAni();
        this.loadAni();
        this.loadSVGAother();
        
        wait(200).then(async()=>{
            this.lastani = await loadSvga(getResPath() + 'resource/assets/svgas/lastani.svga',this.lastscene) as SVGA.EgretMovieClip;
            this.lastani.lockStep = true;
            this.lastani.y = 270
        })
                    // console.log(this.scroll)
                    // egret.Tween.get(this.scroll.viewport)
                    // .to({scrollV:this.scroll.viewport.percentHeight-this.scroll.height})
        // wait(1500).then(()=>{
            this.typerEffect(this.lastlab,"没错，我们就是五好奶牛\n快来“认养”我们\n每天都喝好牛奶\n买牛奶，不如认养一头牛！",170)
        //     window['setBgm']("lab16music",true);
        // })
        if(GFun.isIOS){
            this.musicoffBtn.visible = true;
            this.musiconBtn.visible = false;
            window["mute"] = true;
        }else{
            this.musicoffBtn.visible = false;
            this.musiconBtn.visible = true;
            window["mute"] = false;
        }
    }
    initEvents(){
        super.initEvents();
        this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
        this.scroll.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.tapBuried,this)
        this.scroll.addEventListener(egret.Event.CHANGE,this.MoveScreen,this);
        this.scroll.addEventListener(eui.UIEvent.CHANGE_END,this.onScaleMove,this)
        this.scroll.addEventListener(eui.UIEvent.CHANGE_END,this.stopMove,this)
        // this.scroll.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,this.stopMove,this)
        this.musiconBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.stopMusic,this)
        this.musicoffBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.startMusic,this)
        this.xfbtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.clickXfbtn,this);
        // GDispatcher.addEvent("addpause",this.addpause,this);
        GDispatcher.addEvent("addresume",this.addresume,this);
    }
    tapBuried(){
        NetManager.ins.clickLog(this.slideExposure)
    }
    isaddOpenMusic:boolean = false;
    addpause(){
        this.isaddOpenMusic = this.musiconBtn.visible;
        // console.log("停止音乐");
        this.stopMusic();
    }
    addresume(){
        if(this.isaddOpenMusic){
            this.startMusic();
        }
    }


    isMusicOn = false;
    startMusic(){
        console.log("播放音乐");
        // alert(GFun.isIOS)
        this.musicoffBtn.visible = false;
        this.musiconBtn.visible = true;
        window["mute"] = false;
        window['setVolumeon']();
        window['setBgm']("bgmMusic",true);
        if(GFun.isIOS){
            if(!this.isMusicOn){
                this.isMusicOn = true;
                window['setBgm']("bgmMusic",true);
                window['setBgm']("lab1music",true);
                let now = new Date()
                window['setlab1start'](Math.floor((now.getTime()-this.currenttime)/1000))
                window['setBgm']("bg1",true);
                window['setBgm']("bg2",true);
                window['setBgm']("bg3",true);
                window['setBgm']("bg4",true);
                window['setBgm']("bg5",true);
                window['setBgm']("bg6",true);
                window['setBgm']("bg7",true);
                window['setBgm']("bg8",true);
                
                
                window['setBgm']("lab2music",true);
                window['setBgm']("lab3music",true);
                window['setBgm']("lab4music",true);
                window['setBgm']("lab5music",true);
                window['setBgm']("lab6music",true);
                window['setBgm']("lab7music",true);
                window['setBgm']("lab8music",true);
                window['setBgm']("lab9music",true);
                window['setBgm']("lab10music",true);
                window['setBgm']("lab11music",true);
                window['setBgm']("lab12music",true);
                window['setBgm']("lab13music",true);
                window['setBgm']("lab14music",true);
                window['setBgm']("lab15music",true);
                window['setBgm']("lab16music",true);
                window['setBgm']("bg1",false);
                window['setBgm']("bg2",false);
                window['setBgm']("bg3",false);
                window['setBgm']("bg4",false);
                window['setBgm']("bg5",false);
                window['setBgm']("bg6",false);
                window['setBgm']("bg7",false);
                window['setBgm']("bg8",false);
                
                
                window['setBgm']("lab2music",false);
                window['setBgm']("lab3music",false);
                window['setBgm']("lab4music",false);
                window['setBgm']("lab5music",false);
                window['setBgm']("lab6music",false);
                window['setBgm']("lab7music",false);
                window['setBgm']("lab8music",false);
                window['setBgm']("lab9music",false);
                window['setBgm']("lab10music",false);
                window['setBgm']("lab11music",false);
                window['setBgm']("lab12music",false);
                window['setBgm']("lab13music",false);
                window['setBgm']("lab14music",false);
                window['setBgm']("lab15music",false);
                window['setBgm']("lab16music",false);
                if(!this.isFirstMove){
                    window['setBgm']("lab1music",false);
                }
            }            
        }
    }   
    stopMusic(){
        this.musicoffBtn.visible = true;
        this.musiconBtn.visible = false;
        window["mute"] = true;
        window['setVolumeoff']();
    }
    startMove=false; 
    onScaleMove(){
        if(this.isFirstMove){
            this.isFirstMove = false
            this.startMove = true;
            egret.Tween.get(this.scroll.viewport)
            .to({scrollV:1210},500)
            egret.Tween.get(this.one)
            .to({x:320,y:1810,scaleX:0,scaleY:0},500)
            .call(()=>{
                // this.endY = this.scroll.viewport.scrollV;
                this.MoveScreen()
            })
        }
        
    }
    startY = 0;
    endY;
    isNoone = false;
    isFirstMove = true;
    MoveScreen(){
        this.endY = this.scroll.viewport.scrollV;
        
        if(this.endY>0){
            this.slide.visible = false
            if(this.isFirstMove){
                if(this.one.scaleX>0){
                    this.one.scaleX =1- this.endY/(1810-this.scroll.height/2)
                    this.one.scaleY =1- this.endY/(1810-this.scroll.height/2)
                }
                this.one.x = this.onex-55*this.endY/(1810-this.scroll.height/2)
                this.one.y = this.oney+1045*this.endY/(1810-this.scroll.height/2)
            }
            // console.log(Math.floor(1810-this.scroll.height/2))

            if(Math.floor(this.endY)>=1150){
                if(this.isFirst){
                    this.isFirst = false;
                    this.one.visible = false;
                    this.loadSunAni();
                    this.walkCowMove(this.cowPoint[1].x,this.cowPoint[1].y)
                    this.cowlabAni(1);
                    // this.cowlabGroup.alpha = 0
                    this.cowlab.source = "cowlab1_png"
                    
                    window['setBgm']("lab2music",true);
                    window['setBgm']("lab1music",false);
                    
                    window['setBgm']("bg1",true);
                    window['setBgm']("bg2",true);
                }
                if(this.endY>1206){
                    
                }else{
                    this.scroll.viewport.scrollV = 1206
                        
                }
                
            }
            

        }
    }
    SoundComplete(){
        this.cowlabAni(2);
    }

    stopMove(){
        
        if(this.walkcow.y>this.scroll.viewport.scrollV+this.scroll.height/2){
            window['setBgm']("bg1",false);
            window['setBgm']("bg2",false);
            window['setBgm']("bg3",false);
            window['setBgm']("bg4",false);
            window['setBgm']("bg5",false);
            window['setBgm']("bg6",false);
            window['setBgm']("bg7",false);
            window['setBgm']("bg8",false);
            
            let balance = this.scroll.height/2+this.scroll.viewport.scrollV;
            if(this.scroll.viewport.scrollV>=1206){
                if(balance<2550){
                    window['setBgm']("bg1",true);
                    window['setBgm']("bg2",true);
                }else if(balance<3425){
                    window['setBgm']("bg3",true);
                    window['setBgm']("bg4",true);
                }else if(balance<4700){
                    window['setBgm']("bg5",true);
                }else if(balance<6120){
                    window['setBgm']("bg6",true);
                }else if(balance<8200){
                    window['setBgm']("bg7",true);
                    // window['setBgm']("bg8",true);
                }else{

                }
            }
         }
            
    }


    removeEvents(){
        super.removeEvents();
    }
    async walkCowAni(){
        this.cowwalk = await loadSvga(getResPath() + 'resource/assets/svgas/walkcow.svga',this.walkcow) as SVGA.EgretMovieClip;
        this.cowwalk.lockStep = true;
        this.walkcow.setChildIndex(this.cowlabGroup,this.walkcow.numChildren-1)
    }
    
    walkCowMove(cowx,cowy){
        let speed = 0

        if(cowy>4580){
            // alert("?")
            speed = 8
        }else{
            speed = 13
        }
        let time = Math.sqrt((cowx-this.walkcow.x)*(cowx-this.walkcow.x)+(cowy-this.walkcow.y)*(cowy-this.walkcow.y))*speed
        egret.Tween.get(this.walkcow)
        .to({x:cowx,y:cowy},time)
        .call(()=>{
            this.isGo = true
        })
    }
    cowlabAni(type){
        let cowscale = this.walkcow.scaleX;
        if(cowscale>0){
            cowscale = 1
        }else if(cowscale<0){
            cowscale = -1
        }
        // this.cowlabGroup.alpha = 1
        if(type == 1){
            egret.Tween.get(this.cowlabGroup)
            .set({scaleX:0,scaleY:0})
            .to({scaleX:1.06*cowscale,scaleY:1.06},450)
            .to({scaleX:0.96*cowscale,scaleY:0.96},100)
            .to({scaleX:1*cowscale,scaleY:1},50)
        }else{
            if(this.cowlabGroup.scaleX != 0){
                egret.Tween.get(this.cowlabGroup)
                .set({scaleX:1*cowscale,scaleY:1})
                .to({scaleX:0.96*cowscale,scaleY:0.96},50)
                .to({scaleX:1.06*cowscale,scaleY:1.06},150)
                .to({scaleX:0,scaleY:0},200)
            }
            
            this.cowlab.source = ""
        }
        wait(200).then(()=>{
            this.cowlabGroup.visible = true;
        })
        
        
    }
    async loadDream(){
        // console.log("loadDream1111111111111");
        window['setBgm']("bg8",true);
        this.walkcow.visible = false
        try{
            this.dream2 = await loadSvga(getResPath() + 'resource/assets/svgas/dream.svga',this.dream) as SVGA.EgretMovieClip;
            // this.dream.addChild(this.dream2);
            this.dream2.gotoAndStop(1);
        }
        catch(e){
            console.log('svga error:', e)
            return this.loadDream();
        }
        
        this.dream2.lockStep = true;
        
        let totalframe = this.dream2.totalFrames;
        this.dream2.gotoAndPlay(1,true);
        // console.log("总帧数",totalframe);
        
        let compfunc = ()=>{
            if(this.finani){
                this.finani = false;
                this.dream2.gotoAndStop(totalframe);
                this.lastend = true;
                egret.Tween.get(this.lastscene)
                .to({y:this.lastscene.y-700},1000)
                window['setBgm']("lab16music",true);

                this.dream2.removeEventListener(egret.Event.COMPLETE,compfunc,this);
            }
        }
        // this.dream2.startAniRange(1,totalframe,1,compfunc);

        let framefunc = ()=>{
            var curframe = this.dream2.currentFrame;
            // console.log("测试",curframe,this.finaframe);
            if(curframe>=this.finaframe){
                this.finaframe = curframe;
                if(curframe==totalframe-1){
                    this.dream2.removeEventListener(egret.Event.ENTER_FRAME,framefunc,this);
                    compfunc();
                }
            }else{
                this.dream2.removeEventListener(egret.Event.ENTER_FRAME,framefunc,this);
                compfunc();
            }
        }
        this.dream2.addEventListener(egret.Event.ENTER_FRAME,framefunc,this);

        // this.dream2.once(egret.Event.COMPLETE, compfunc, this);

        wait(1800).then(()=>{
            window['setBgm']("lab15music",true);
            window['setBgm']("bg8",false);
        })
    }

    loadSunAni(){
        egret.Tween.get(this.twosun)
        .set({x:207,y:1525})
        .to({x:377,y:1165},2000)
        .call(()=>{
            this.twosun.visible = false
        })
    }
    loadAni(){
        egret.Tween.get(this.twowing,{loop:true})
        .set({alpha:0})
        .to({alpha:1},200)
        .wait(800)
        .to({alpha:0},200);  
        egret.Tween.get(this.twowing,{loop:true})
        .set({scaleX:1,scaleY:1})
        .to({scaleX:0.5,scaleY:0.5},1200)
        egret.Tween.get(this.twobucket,{loop:true})
        .set({rotation:0})
        .to({rotation:1},600)
        .to({rotation:0},600)
        for(let i =1;i<5;i++){
            egret.Tween.get(this["twoflow"+i],{loop:true})
            .set({rotation:0})
            .to({rotation:4},600)
            .to({rotation:0},600)
        }

        setInterval(()=>{
            egret.Tween.get(this.lab1group)
            .set({alpha:1})
            .to({alpha:0},100)
            .call(()=>{
                if(this.lab1.source == "lab1_png"){
                    this.lab1.source = "lab2_png";
                }else{
                    this.lab1.source = "lab1_png";
                }
                egret.Tween.get(this.lab1group)
                .to({alpha:1},100)
            })
            egret.Tween.get(this.lab2group)
            .set({alpha:1})
            .to({alpha:0},100)
            .call(()=>{
                if(this.lab2.source == "lab3_png"){
                    this.lab2.source = "lab4_png";
                }else{
                    this.lab2.source = "lab3_png";
                }
                egret.Tween.get(this.lab2group)
                .to({alpha:1},100)
            })
            
            
        },3000)

    }


    async loadSVGAother(){
        this.biredtwo = await loadSvga(getResPath() + 'resource/assets/svgas/twobired.svga',this.twobired) as SVGA.EgretMovieClip;
        this.biredtwo.lockStep = true;

        this.sittwo = await loadSvga(getResPath() + 'resource/assets/svgas/twosit.svga',this.twosit) as SVGA.EgretMovieClip;
        this.sittwo.lockStep = true;

        this.standtwo = await loadSvga(getResPath() + 'resource/assets/svgas/twostand.svga',this.twostand) as SVGA.EgretMovieClip;
        this.standtwo.lockStep = true;

        this.sitthree = await loadSvga(getResPath() + 'resource/assets/svgas/threesit.svga',this.threesit) as SVGA.EgretMovieClip;
        this.sitthree.lockStep = true;

        this.standthree = await loadSvga(getResPath() + 'resource/assets/svgas/threestand.svga',this.threestand) as SVGA.EgretMovieClip;
        this.standthree.lockStep = true;

        this.docfour = await loadSvga(getResPath() + 'resource/assets/svgas/fourdoc.svga',this.fourdoc) as SVGA.EgretMovieClip;
        this.docfour.lockStep = true;

        this.swingfour = await loadSvga(getResPath() + 'resource/assets/svgas/fourswing.svga',this.fourswing) as SVGA.EgretMovieClip;
        this.swingfour.lockStep = true;

        this.standfour = await loadSvga(getResPath() + 'resource/assets/svgas/fourstand.svga',this.fourstand) as SVGA.EgretMovieClip;
        this.standfour.lockStep = true;

        this.doc2four = await loadSvga(getResPath() + 'resource/assets/svgas/fourdoc2.svga',this.fourdoc2) as SVGA.EgretMovieClip;
        this.doc2four.lockStep = true;

        this.bedfive = await loadSvga(getResPath() + 'resource/assets/svgas/fivebed.svga',this.fivebed) as SVGA.EgretMovieClip;
        this.bedfive.lockStep = true;
        this.bedfive = await loadSvga(getResPath() + 'resource/assets/svgas/musiclogo.svga',this.fivemu) as SVGA.EgretMovieClip;
        this.bedfive.lockStep = true;

        this.turnfive = await loadSvga(getResPath() + 'resource/assets/svgas/fiveturn.svga',this.fiveturn) as SVGA.EgretMovieClip;
        this.turnfive.lockStep = true;
        this.turnfive = await loadSvga(getResPath() + 'resource/assets/svgas/musiclogo.svga',this.fiveturn) as SVGA.EgretMovieClip;
        this.turnfive.lockStep = true;
        this.turnfive.scaleX = -1
        this.turnfive.y = 150

        


    }
    async loadMusic(){
        // const soundHowl = new Howl({
        //     src:['bgMusic_mp3','lab1_mp3','01_mp3','lab2_mp3']
        // })
        // soundHowl.play()

        // this.bgMusic = await RES.getResAsync('bgMusic_mp3');
        // console.log("-=-=-=-=-=-=--")
        // this.lab1music = await RES.getResAsync('lab1_mp3');
        // this.lab2music = await RES.getResAsync('lab2_mp3');
        // this.bg1music = await RES.getResAsync('01_mp3');
        // this.bg2music = await RES.getResAsync('02_mp3');
        // this.lab3music = await RES.getResAsync('lab3_mp3');
        // this.lab4music = await RES.getResAsync('lab4_mp3');
        // this.lab5music = await RES.getResAsync('lab5_mp3');
        // this.lab6music = await RES.getResAsync('lab6_mp3',this.onComplete,this);

        // this.bg3music = await RES.getResAsync('03_mp3');
        // this.bg4music = await RES.getResAsync('04_mp3');
        // this.bg5music = await RES.getResAsync('05_mp3');
        // this.bg6music = await RES.getResAsync('06_mp3');
        // this.bg7music = await RES.getResAsync('07_mp3');
        // this.bg8music = await RES.getResAsync('08_mp3');

        
        
        // this.lab7music = await RES.getResAsync('lab7_mp3');
        // this.lab8music = await RES.getResAsync('lab8_mp3');
        // this.lab9music = await RES.getResAsync('lab9_mp3');
        // this.lab10music = await RES.getResAsync('lab10_mp3');
        // this.lab11music = await RES.getResAsync('lab11_mp3');
        // this.lab12music = await RES.getResAsync('lab12_mp3');
        // this.lab13music = await RES.getResAsync('lab13_mp3');
        // this.lab14music = await RES.getResAsync('lab14_mp3');
        // this.lab15music = await RES.getResAsync('lab15_mp3');
        // this.lab16music = await RES.getResAsync('lab16_mp3');
        
        
    }
    currenttime = 0;
    async onComplete(){
        Waiting.instance.hide();
        
        
        window['setBgm']("bgmMusic",true);
        this.cowone = await loadSvga(getResPath() + 'resource/assets/svgas/cow.svga') as SVGA.EgretMovieClip;
        this.cowone.visible = false;
        this.cowone.lockStep = true;
        wait(500).then(()=>{
        
            this.onecow.addChild(this.cowone)
            this.onecow.visible = true;
            this.cowone.visible = true;
           
        })
        wait(1800).then(()=>{
            window['setBgm']("lab1music",true)
        })
        wait(1600).then(async()=>{
            // this.typerEffect(this.oneLab,"大家好哦，我是认养一头牛牧场的小奶牛一头，我的编号是19061，这是我的第一支vlog，带大家看看我的生活~",170)
            this.oneLab.textAlign = "center"
            this.oneLab.text = "大家好"
            window['setBgm']("lab1music",true)
            let now = new Date()
            this.currenttime = now.getTime()
            wait(1000).then(()=>{
                this.oneLab.text = "我是认养一头牛牧场的小奶牛“一头”"
                wait(2800).then(()=>{
                    this.oneLab.text = "我的编号是19061"
                    wait(2100).then(()=>{
                        this.oneLab.text = "这是我的第一支vlog"
                        wait(1800).then(()=>{
                            this.oneLab.text = "带大家看看我的生活~"
                        })
                    })
                })
            })
            
            
        })

        wait(2500).then(()=>{
            window['setBgm']("lab1music",true)
        })
        
        this.cowone.once(egret.Event.COMPLETE, () => {
            this.cowone.stop();
        }, this);
    }

    setColor(group) {
        let colorMatrix = [
            0.3, 0.3, 0.3, 0, 0,
            0.3, 0.3, 0.3, 0, 0,
            0.3, 0.3, 0.3, 0, 0,
            0, 0, 0, 1, 0
        ];
        let colorFilter = new egret.ColorMatrixFilter(colorMatrix);

        group.filters = [colorFilter];

    }

    private typerEffect(obj,content:string = "",interval:number = 200,backFun:Function = null):void{
        var strArr:Array<any> = content.split("");
        var len:number = strArr.length;
        for (var i = 0; i < len; i++){
            egret.setTimeout(function () {              
                obj.appendText(strArr[Number(this)]);
                if ((Number(this) >= len - 1) && (backFun != null)) {
                    backFun();
                }
            }, i, interval*i);              
        }
    }
    
    private clickXfbtn(){
        if(window["nxfactivityurl"]){
            if(!this.xfExposure){
                let xfDpmIn = Buried.connectDpm(110, 3, 1); // 点击埋点   
                let xfDcmIn = Buried.connectDcm(202, 0, 0); // 曝光埋点
                let xfDomIn = Buried.connectDom(1,0);
                this.xfExposure = Buried.createExposure(xfDpmIn, xfDcmIn,xfDomIn);
            }
            
            this.addpause();
            NetManager.ins.clickLog(this.xfExposure);
            window.location.href = window["nxfactivityurl"];
        }
    }
}