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

import Scene from "../views/Scene";
import PanelCtrl from "../ctrls/panelCtrl";
import { NetManager } from '../../libs/tw/manager/NetManager';
import Loading from '../loading/Loading';
import { Buried } from '../../libs/tw/util/Buried';
import { VideoEntity } from '../../libs/SVGA';
import SceneCtrl from '../ctrls/sceneCtrl';
import { playSound, stopAllSound } from '../ctrls/soundCtrl';
import StartScene1 from '../startScene/StartScene';
export default class Scene2 extends Scene {
    private tidx:number;
    public textBg:eui.Image;
    public mainGroup:eui.Group;
    public soundBtn:eui.Button;
    private arrow:SVGA.EgretMovieClip;
    private sunshine:SVGA.EgretMovieClip;
    private s2Role:SVGA.EgretMovieClip;
    private s2Kid:SVGA.EgretMovieClip;
    private wuqi0:SVGA.EgretMovieClip;
    private wuqi1:SVGA.EgretMovieClip;
    private wuqi2:SVGA.EgretMovieClip;

    private tTxtTimes:any[];

    public get skinKey() { return 'Scene2' }
    public get groupName() { return 'scene2' }

    constructor(data?) {
        super(data);

    }

    initEvents() {
        super.initEvents();

        this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTap_this, this);
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onBegin_this, this);
        this.soundBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_soundBtn, this);
    }

    removeEvents() {
        super.removeEvents();

        this.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTap_this, this);
        this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onBegin_this, this);
        this.soundBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_soundBtn, this);
    }

    private bLocalX:number = 0;
    private onTap_this(e:egret.TouchEvent):void{
        console.log('e.localX',e.localX);
        console.log('value', e.localX - this.bLocalX);
        let val = e.localX - this.bLocalX;
        if(Math.abs(val) > window['space']){
            clearTimeout(this.settimeIdx);
            if(val < 0){//向左
                SceneCtrl.instance.change(ModuleTypes.SCENE3);
                this.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTap_this, this);
                this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onBegin_this, this);

            // stopAllSound();
                    var music:any = document.getElementById('music');
                    music.pause();
            }else{//向右
                // SceneCtrl.instance.change(ModuleTypes.START_SCENE);
            }

            clearTimeout(this.tidx);
        }
    }

    private onBegin_this(e:egret.TouchEvent):void{
        console.log('e.localX',e.localX);
        this.bLocalX = e.localX;
        
        if(window['isapp']){//端内
            let startDpmOut = Buried.connectDpm(110, 5, 1); // 点击埋点   
            let startDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
            let exposure = Buried.createExposure(startDpmOut, startDcmOut);
    
            NetManager.ins.clickLog(exposure);
        }else{
            let startDpmOut = Buried.connectDpm(110, 14, 1); // 点击埋点   
            let startDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
            let exposure = Buried.createExposure(startDpmOut, startDcmOut);
    
            NetManager.ins.clickLog(exposure);

        }

    }

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

    private bgtexture: egret.Texture;
    protected async preLoadRes() {
        //加载背景并设置背景图片
        // this.bgtexture = await RES.getResAsync('startBg_jpg');
        this.sunshine = await PanelCtrl.instance.getSvga('resource/assets/svga/scene2/sunshine.svga') as SVGA.EgretMovieClip;
        this.s2Role = await PanelCtrl.instance.getSvga('resource/assets/svga/scene2/s2Role.svga') as SVGA.EgretMovieClip;
        this.s2Kid = await PanelCtrl.instance.getSvga('resource/assets/svga/scene2/s2Kid.svga') as SVGA.EgretMovieClip;
        this.wuqi0 = await PanelCtrl.instance.getSvga('resource/assets/svga/scene2/wuqi0.svga') as SVGA.EgretMovieClip;
        this.wuqi1 = await PanelCtrl.instance.getSvga('resource/assets/svga/scene2/wuqi1.svga') as SVGA.EgretMovieClip;
        this.wuqi2 = await PanelCtrl.instance.getSvga('resource/assets/svga/scene2/wuqi2.svga') as SVGA.EgretMovieClip;
        this.arrow = await PanelCtrl.instance.getSvga('resource/assets/svga/start/arrow.svga') as SVGA.EgretMovieClip;

        // await RES.getResAsync('sound2_mp3');

        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();
        }
    }

    private onLoad(): void {
        
        if(window['isapp']){//端内
            let startDpmOut = Buried.connectDpm(110, 5, 1); // 点击埋点   
            let startDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
            let exposure = Buried.createExposure(startDpmOut, startDcmOut);
    
            NetManager.ins.showLog(exposure);
        }else{
            let startDpmOut = Buried.connectDpm(110, 14, 1); // 点击埋点   
            let startDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
            let exposure = Buried.createExposure(startDpmOut, startDcmOut);
    
            NetManager.ins.showLog(exposure);

        }

        this.initSoundBtn();
        this.initData();
        this.initUI();

        if(StartScene1.isPlayMusic){
            this.tidx = setTimeout(() => {
                var music:any = document.getElementById('music');
                music.play();
    
            }, this.tTxtTimes[0].s * 1000);
        }
    }

    private initData():void{
        this.tTxtTimes = [
            {s:3, e:5, h:335},
            {s:5.5, e:6.83, h:175},
            {s:7.33, e:9, h:230}
        ]
    }

    private initUI(): void {
        this.initAct();
        this.initSvga();

    }

    private settimeIdx:any;
    private initAct():void{
        this.textBg.alpha = 0;
        //文字背景动效
        egret.Tween.get(this.textBg).wait(2000).to({alpha:1}, 500);

        for(let i = 0; i < 3; i++){
            this.setTxtAct(this.tTxtTimes[i].s, this.tTxtTimes[i].e, this.tTxtTimes[i].h, this['text' + i]);
        }
        
        this.settimeIdx = setTimeout(() => {
            //切换场景3
            console.log('切换场景3');
            SceneCtrl.instance.change(ModuleTypes.SCENE3);
            this.removeEvents();
        }, this.tTxtTimes[2].e * 1000 + 1000);

        if(!window['toutArr']){
            window['toutArr'] = [];
        }

        window['toutArr'].push(this.settimeIdx);
    }

    private setTxtAct(stime:number, etime:number, h:number, target:eui.Component):void{
        let shap = new egret.Shape();
        shap.graphics.beginFill(0);
        shap.graphics.drawRect(0, 0, 37, h);
        shap.x = target.x;
        shap.y = target.y - h;
        this.mainGroup.addChild(shap);

        target.mask  = shap;

        setTimeout(() => {
            egret.Tween.get(shap).to({y:target.y}, (etime - stime) * 1000);
        }, stime * 1000);
    }

    private initSvga():void{

        //初始化常态蜻蜓
        this.sunshine.play();
        this.mainGroup.addChild(this.sunshine);

        this.s2Role.play();
        this.mainGroup.addChild(this.s2Role);

        this.s2Kid.play();
        this.mainGroup.addChild(this.s2Kid);

        this.wuqiact();

        this.mainGroup.addChild(this.soundBtn);
        // setTimeout(() => {
        //     this.startBtn.play();
        // }, 330);
        // this.startBtn.touchEnabled = true;
        // this.startBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_start, this);
        // this.mainGroup.addChild(this.startBtn);

        setTimeout(() => {
            this.mainGroup.addChild(this.arrow);
            this.arrow.x = 50;
            this.arrow.y = 100;

            setTimeout(() => {
                this.arrow.stop();
                this.arrow.visible = false;
            }, window['disTime'] ? window['disTime'] : 1000);
        }, 1000);
    }

    private isstop:boolean;
    private wuqiact():void{
        // this.wuqi0.play();
        this.wuqi0.startAniRange(1, this.wuqi0.totalFrames, 1);
        this.mainGroup.addChild(this.wuqi0);

        setTimeout(() => {
            this.wuqi1.startAniRange(1, this.wuqi1.totalFrames, 1);
            this.mainGroup.addChild(this.wuqi1);
        }, 500);

        setTimeout(() => {
            // this.wuqi2.play();
            this.wuqi2.startAniRange(1, this.wuqi2.totalFrames, 1, ()=>{
                if(!this.isstop){
                    this.wuqiact();
                }
            });
            this.mainGroup.addChild(this.wuqi2);
        }, 1000);
    }

    private initSoundBtn(){
        if(!StartScene1.isPlayMusic){
            this.soundBtn.currentState = 'disabled';
            window['musicPlay'](false);
        }else{
            this.soundBtn.currentState = 'up';
            window['musicPlay'](true);
        }
    }
    
    private onTap_soundBtn(){
        PanelCtrl.instance.btnDelay(this.soundBtn);

        let startDpmOut = Buried.connectDpm(110, 3, 1); // 点击埋点   
        let startDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
        let exposure = Buried.createExposure(startDpmOut, startDcmOut);

        NetManager.ins.clickLog(exposure);
        if(StartScene1.isPlayMusic){
            StartScene1.isPlayMusic = false;
            this.soundBtn.currentState = 'disabled';
            window['musicPlay'](false);
            
            var music:any = document.getElementById('music');
            if(music){
                music.pause();
            }
        }else{
            StartScene1.isPlayMusic = true;
            this.soundBtn.currentState = 'up';
            window['musicPlay'](true);
        }
    }

}
