
import { Data } from './../../libs/tw/data/Data';
import { GDispatcher } from '../../libs/tc/util/GDispatcher';
import { Buried } from './../../libs/tw/util/Buried';
import layers from "../../src/views/layers";
import { INetData } from './../../libs/tc/interface/INetData';
import { ModuleTypes } from './../types/sceneTypes';
import { DataManager } from './../../libs/tw/manager/DataManager';
import StartScene from './StartScene';
import PanelCtrl from '../ctrls/panelCtrl';
import SceneCtrl from '../ctrls/sceneCtrl';
import { NetManager } from '../../libs/tw/manager/NetManager';
import GameConst from '../GameConst';
import SceneTransitionAni from '../SceneTransitionAni';
export default class ExStartScene extends StartScene {

    public bg: eui.Image;
    public warningTips: eui.Label;
    public myCreditsTxt: eui.Label;
    public costTxt: eui.Label;
    public heart1: eui.Image;
    public heart2: eui.Image;
    public title: eui.Image;
    public dogBtn: eui.Button;
    public catBtn: eui.Button;
    public ruleBtn: eui.Button;
    public recordBtn: eui.Button;
    public finger: eui.Image;
    public vs: eui.Image;
    public vs2: eui.Image;
    public campTipsTxt: eui.Label;



    public video = document.getElementById('my_video');
    public titleSpritePool: any[] = []; // 标题粒子池
    public mainBgSpritePool: any[] = []; // 主场景粒子池
    public mainLightSpritePool: any[] = []; // 主光源喷射粒子池
    public shineSpritePool: any[] = []; // 闪烁粒子池
    public bubbleMoveCount: number = 0; // 泡泡移动计数
    public gameCount: number = 0; //  游戏计数
    public maxLevel: number = 1;
    public startExposure: any;
    public isCamp: boolean = false;
    public userCamp: number = null;

    start(data?) {
        super.start();

        this.isCamp = false;
        this.userCamp = null;

        egret.Tween.removeAllTweens();

        if (!DataManager.ins.getData('getValentineInfo')) {

        } else {
            this.initScene();
        }

        this.initBuired();

        // this.initWarningTips();

    }

    public initScene() {

        if (this.userCamp) {
            if (this.userCamp == 1) {
                // 情侣猫阵营
                this.bg.x = -750;
                this.catBtn.x = GameConst.stage.stageWidth / 2;
                this.dogBtn.visible = false;
            } else {
                // 单身汪阵营
                this.bg.x = 0;
                this.dogBtn.x = GameConst.stage.stageWidth / 2;
                this.catBtn.visible = false;
            }
        } else {
            if (DataManager.ins.getData('getValentineInfo').data && DataManager.ins.getData('getValentineInfo').data.camp) {
                // 已绑定阵营
                this.isCamp = true;
                this.userCamp = DataManager.ins.getData('getValentineInfo').data.camp;
                if (this.userCamp == 1) {
                    // 情侣猫阵营
                    this.bg.x = - 750;
                    this.catBtn.x = GameConst.stage.stageWidth / 2;
                    this.dogBtn.visible = false;

                } else {
                    // 单身汪阵营
                    this.bg.x = 0;
                    this.dogBtn.x = GameConst.stage.stageWidth / 2;
                    this.catBtn.visible = false;
                }
                this.costTxt.visible = true;
                this.myCreditsTxt.y = 930;
            } else {
                // 未绑定阵营
                this.isCamp = false;
                this.userCamp = null;
                this.costTxt.visible = false;
            }
        }

        this.initSceneAni();
    }

    initSceneAni() {
        console.log(`userCamp:${this.userCamp},isCamp:${this.isCamp}`)
        if (!this.isCamp) {
            // 未选择阵营动效
            this.title.scaleX = 0;
            this.title.scaleY = 0;
            this.vs.alpha = 0;
            this.vs.scaleX = 2.5;
            this.vs.scaleY = 2.5;
            this.dogBtn.scaleX = 0;
            this.dogBtn.scaleY = 0;
            this.catBtn.scaleX = 0;
            this.catBtn.scaleY = 0;
            this.costTxt.scaleX = 0;
            this.costTxt.scaleY = 0;
            this.myCreditsTxt.scaleX = 0;
            this.myCreditsTxt.scaleY = 0;

            let titleTW = egret.Tween.get(this.title);
            titleTW.to({ scaleX: 1.1, scaleY: 1.1 }, 300).to({ scaleX: 1, scaleY: 1 }, 200).call(() => {
                let vsTW = egret.Tween.get(this.vs);
                vsTW.to({ scaleX: 1, scaleY: 1, alpha: 1 }, 300).call(() => {
                    this.vs2.visible = true;
                    this.vs2.alpha = 0.5;

                    let vsTW2 = egret.Tween.get(this.vs2);
                    vsTW2.to({ scaleX: 1.6, scaleY: 1.6 }, 300).call(() => {
                        this.vs2.scaleX = 1;
                        this.vs2.scaleY = 1;
                        let vsTW3 = egret.Tween.get(this.vs2, { loop: true });
                        vsTW3.wait(800).to({ scaleX: 1.3, scaleY: 1.2, y: this.vs2.y - 10 }, 400);
                    });

                    let costTxtTW = egret.Tween.get(this.costTxt);
                    costTxtTW.to({ scaleX: 1, scaleY: 1 }, 500);

                    let myCreditsTW = egret.Tween.get(this.myCreditsTxt);
                    myCreditsTW.to({ scaleX: 1, scaleY: 1 }, 500);

                    let dogBtnTW = egret.Tween.get(this.dogBtn);
                    dogBtnTW.to({ scaleX: 1, scaleY: 1 }, 500);

                    let catBtnTW = egret.Tween.get(this.catBtn);
                    catBtnTW.to({ scaleX: 1, scaleY: 1 }, 500).call(() => {
                        this.finger.visible = true;
                        this.finger.x = 111;
                        let fingerTW = egret.Tween.get(this.finger, { loop: true });
                        fingerTW.to({ x: 520 }, 1200).to({ x: 111 }, 1200);

                        this.campTipsTxt.visible = true;
                        this.heart1.visible = true;
                        this.heart2.visible = true;
                        this.heart1.alpha = 0.5;
                        this.heart2.alpha = 0.5;
                        let heart1TW = egret.Tween.get(this.heart1, { loop: true });
                        heart1TW.to({ alpha: 1, x: this.heart1.x + 50, y: this.heart1.y - 30 }, 500).to({ alpha: 1, x: this.heart1.x + 40, y: this.heart1.y - 60 }, 500);
                        let heart2TW = egret.Tween.get(this.heart2);
                        heart2TW.wait(500).call(() => {
                            let heart2TW = egret.Tween.get(this.heart2, { loop: true });
                            heart2TW.to({ alpha: 1, x: this.heart2.x - 50, y: this.heart2.y - 40 }, 500).to({ alpha: 1, x: this.heart2.x - 40, y: this.heart2.y - 80 }, 500);
                        });
                    })
                })
            })
        } else {
            // 选择阵营动效
            if (this.userCamp == 1) {
                // 猫
                this.title.scaleX = 0;
                this.title.scaleY = 0;
                this.vs.alpha = 0;
                this.vs.scaleX = 2.5;
                this.vs.scaleY = 2.5;
                this.catBtn.scaleX = 0;
                this.catBtn.scaleY = 0;
                this.costTxt.scaleX = 0;
                this.costTxt.scaleY = 0;
                this.myCreditsTxt.scaleX = 0;
                this.myCreditsTxt.scaleY = 0;

                let titleTW = egret.Tween.get(this.title);
                titleTW.to({ scaleX: 1.1, scaleY: 1.1 }, 300).to({ scaleX: 1, scaleY: 1 }, 200).call(() => {
                    let vsTW = egret.Tween.get(this.vs);
                    vsTW.to({ scaleX: 1, scaleY: 1, alpha: 1 }, 300).call(() => {
                        this.vs2.visible = true;
                        this.vs2.alpha = 0.5;

                        let vsTW2 = egret.Tween.get(this.vs2);
                        vsTW2.to({ scaleX: 1.6, scaleY: 1.6 }, 300).call(() => {
                            this.vs2.scaleX = 1;
                            this.vs2.scaleY = 1;
                            let vsTW3 = egret.Tween.get(this.vs2, { loop: true });
                            vsTW3.wait(800).to({ scaleX: 1.3, scaleY: 1.2, y: this.vs2.y - 10 }, 400);
                        });

                        let costTxtTW = egret.Tween.get(this.costTxt);
                        costTxtTW.to({ scaleX: 1, scaleY: 1 }, 500);

                        let myCreditsTW = egret.Tween.get(this.myCreditsTxt);
                        myCreditsTW.to({ scaleX: 1, scaleY: 1 }, 500);

                        let catBtnTW = egret.Tween.get(this.catBtn);
                        catBtnTW.to({ scaleX: 1, scaleY: 1 }, 500).call(() => {
                            this.heart1.visible = true;
                            this.heart2.visible = true;
                            this.heart1.x = this.catBtn.x - (563 - 505);
                            this.heart2.x = this.catBtn.x + (535 - 505);
                            this.heart1.alpha = 0.5;
                            this.heart2.alpha = 0.5;
                            let heart1TW = egret.Tween.get(this.heart1, { loop: true });
                            heart1TW.to({ alpha: 1, x: this.heart1.x + 50, y: this.heart1.y - 30 }, 500).to({ alpha: 1, x: this.heart1.x + 40, y: this.heart1.y - 60 }, 500);
                            let heart2TW = egret.Tween.get(this.heart2);
                            heart2TW.wait(500).call(() => {
                                let heart2TW = egret.Tween.get(this.heart2, { loop: true });
                                heart2TW.to({ alpha: 1, x: this.heart2.x - 50, y: this.heart2.y - 40 }, 500).to({ alpha: 1, x: this.heart2.x - 40, y: this.heart2.y - 80 }, 500);
                            });
                        })
                    })
                })
            } else if (this.userCamp == 2) {
                // 狗
                this.title.scaleX = 0;
                this.title.scaleY = 0;
                this.vs.alpha = 0;
                this.vs.scaleX = 2.5;
                this.vs.scaleY = 2.5;
                this.dogBtn.scaleX = 0;
                this.dogBtn.scaleY = 0;
                this.costTxt.scaleX = 0;
                this.costTxt.scaleY = 0;
                this.myCreditsTxt.scaleX = 0;
                this.myCreditsTxt.scaleY = 0;

                let titleTW = egret.Tween.get(this.title);
                titleTW.to({ scaleX: 1.1, scaleY: 1.1 }, 300).to({ scaleX: 1, scaleY: 1 }, 200).call(() => {
                    let vsTW = egret.Tween.get(this.vs);
                    vsTW.to({ scaleX: 1, scaleY: 1, alpha: 1 }, 300).call(() => {
                        this.vs2.visible = true;
                        this.vs2.alpha = 0.5;

                        let vsTW2 = egret.Tween.get(this.vs2);
                        vsTW2.to({ scaleX: 1.6, scaleY: 1.6 }, 300).call(() => {
                            this.vs2.scaleX = 1;
                            this.vs2.scaleY = 1;
                            let vsTW3 = egret.Tween.get(this.vs2, { loop: true });
                            vsTW3.wait(800).to({ scaleX: 1.3, scaleY: 1.2, y: this.vs2.y - 10 }, 400);
                        });

                        let costTxtTW = egret.Tween.get(this.costTxt);
                        costTxtTW.to({ scaleX: 1, scaleY: 1 }, 500);

                        let myCreditsTW = egret.Tween.get(this.myCreditsTxt);
                        myCreditsTW.to({ scaleX: 1, scaleY: 1 }, 500);

                        let dogBtnTW = egret.Tween.get(this.dogBtn);
                        dogBtnTW.to({ scaleX: 1, scaleY: 1 }, 500);
                    })
                })
            }
        }
    }

    chooseAndStart(e) {
        if (this.userCamp) {
            if (this.userCamp == 1) {
                // 猫
                let bgTW = egret.Tween.get(this.bg);
                bgTW.to({ x: -750 }, 500);
                let catBtnTW = egret.Tween.get(this.catBtn);
                catBtnTW.to({ x: GameConst.stage.stageWidth / 2 }, 500);
                let dogBtnTW = egret.Tween.get(this.dogBtn);
                dogBtnTW.to({ x: 0, alpha: 0 }, 500).call(() => {
                    this.onTap_startBtn(e);
                });
            } else {
                // 狗
                let bgTW = egret.Tween.get(this.bg);
                bgTW.to({ x: 0 }, 500);
                let dogBtnTW = egret.Tween.get(this.dogBtn);
                dogBtnTW.to({ x: GameConst.stage.stageWidth / 2 }, 500);
                let catBtnTW = egret.Tween.get(this.catBtn);
                catBtnTW.to({ x: 750, alpha: 0 }, 500).call(() => {
                    this.onTap_startBtn(e);
                });
            }
            this.heart1.visible = false;
            this.heart2.visible = false;
            this.finger.visible = false;
            this.campTipsTxt.visible = false;
        } else {

        }

    }


    // 初始化埋点信息
    initBuired() {
        Buried.init();

        // 开始按钮埋点信息
        let startDpmOut = Buried.connectDpm(110, 1, 1); // 点击埋点   
        let startDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
        this.startExposure = Buried.createExposure(startDpmOut, startDcmOut);


        NetManager.ins.showLog(this.startExposure);
    }


    initEvents() {
        super.initEvents();
        this.dogBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_dogBtn, this);
        this.catBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_catBtn, this);
    }

    removeEvents() {
        super.removeEvents();

        this.dogBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_dogBtn, this);
        this.catBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_catBtn, this);
    }

    onTap_dogBtn(e) {
        this.dogBtn.touchEnabled = false;
        setTimeout(() => {
            this.dogBtn.touchEnabled = true;
        }, 1000);
        if (this.isCamp) {
            // 已选择阵营,开始游戏
            this.onTap_startBtn(e)
        } else {
            // 未选择阵营,去选择阵营
            let data = { camp: 2, scene: this };
            PanelCtrl.instance.show(ModuleTypes.CAMP_PANEL, data);
        }

    }

    onTap_catBtn(e) {
        this.catBtn.touchEnabled = false;
        setTimeout(() => {
            this.catBtn.touchEnabled = true;
        }, 1000);
        if (this.isCamp) {
            // 已选择阵营
            this.onTap_startBtn(e)
        } else {
            // 未选择阵营
            let data = { camp: 1, scene: this };
            PanelCtrl.instance.show(ModuleTypes.CAMP_PANEL, data);
        }
    }

    // // 免责声明
    // initWarningTips() {
    //     console.log(this.warningTips, this.myCreditsTxt, this.dogBtn)
    //     if (this.warningTips) {
    //         if (GameConst.isIOS()) {
    //             this.warningTips.visible = true;
    //         } else {
    //             this.warningTips.visible = false;
    //         }
    //     } else {
    //         setTimeout(() => {
    //             if (GameConst.isIOS()) {
    //                 this.warningTips.visible = true;
    //             } else {
    //                 this.warningTips.visible = false;
    //             }
    //         }, 20)
    //     }

    // }



    onTap_startBtn(e: egret.Event) {
        super.onTap_startBtn(e);
        NetManager.ins.clickLog(this.startExposure);
    }

    /**
     *  开始游戏的回调
     */
    doStartResult(success: boolean, data) {
        if (!success) {
            // 轮询失败
            if (data && data.code == 0) {
                SceneCtrl.instance.change(ModuleTypes.PLAY_SCENE);
                this.startBtn.touchEnabled = true;
            } else {
                let msgData = { msg: '网络开了个小差，请稍后再试~', type: "err", title: "网络异常" }
                PanelCtrl.instance.show(ModuleTypes.MSG_PANEL, msgData)
            }
            return;
        }
        let sceneData = { camp: this.userCamp };

        // this.startBtn.touchEnabled = true;

        let nBg = new eui.Image();
        nBg.source = `startBg_png`;
        if (this.userCamp == 1) {
            nBg.x = -750
        } else {
            nBg.x = 0;
        }
        layers.sceneLayer.addChildAt(nBg, 0);
        SceneTransitionAni.close(() => {
            layers.sceneLayer.removeChild(nBg);
            SceneCtrl.instance.change(ModuleTypes.PLAY_SCENE, sceneData);
        }, this);
    }
}