
import { NetManager } from './../../libs/tw/manager/NetManager';
import { Buried } from './../../libs/tw/util/Buried';
import Scene from "../views/Scene";
import PeopleConfig from './PeopleConfig';
import PanelCtrl from "../ctrls/panelCtrl";
import { ModuleTypes } from "../types/sceneTypes";
import isIPhoneX from '../../libs/tc/utils/isIPhoneX';
export default class EndScene extends Scene {
    protected get skinKey() { return 'End' }

    public bg: eui.Image;
    public star1Glow: eui.Image;
    public star1: eui.Image;
    public star2Glow: eui.Image;
    public star2: eui.Image;
    public star3Glow: eui.Image;
    public star3: eui.Image;
    public star4Glow: eui.Image;
    public star4: eui.Image;
    public star6Glow: eui.Image;
    public star6: eui.Image;
    public star6F: eui.Image;
    public star5Glow: eui.Image;
    public star5: eui.Image;
    public planet: eui.Image;
    public tips: eui.Image;
    public darkMask: eui.Image;
    public guideTxt: eui.Image;
    public nickNameTxt: eui.Label;
    public peopleName: eui.Image;
    public peopleHead: eui.Image;
    public endTxtPlane: eui.Component;
    public shareGuide: eui.Image;
    public toastGuideBg: eui.Rect;
    public toastGUideTxt: eui.Label;


    public nickName: string;
    public img: any;
    public domShareBtn: any;
    public domReChooseBtn: any;
    public isInApp: boolean;
    public starExposure: any;
    public reChooseExposure: any;
    public longSaveExposure: any;
    public shareExposure: any;
    public downloadExposure: any;
    public currStartTimeStamp: number;
    public currPeople: any;
    public currWordIndex: number;
    public currBid: string;
    public currserialNum: string;
    public saveImg: any;
    public bookImg: any;

    start(data?) {
        let mainCanvas = window['$']('.egret-player').find("canvas")[0];
        mainCanvas.style.display = "block";
        egret.Tween.removeAllTweens();

        // window['isInApp'] = true;
        this.nickName = data ? data : null;

        this.nickNameTxt.text = `${this.nickName}：`;
        this.percentWidth = 100;
        this.percentHeight = 100;


        this.domShareBtn = document.getElementById('shareBtn');
        this.domReChooseBtn = document.getElementById('reChooseBtn');
        this.saveImg = document.getElementById('saveImg');
        this.bookImg = document.getElementById('bookImg');
        this.domReChooseBtn.addEventListener('click', this.onClick_ReChooseBtn.bind(this));
        this.domShareBtn.addEventListener('click', this.onClick_ShareBtn.bind(this));
        this.saveImg.addEventListener('touchstart', this.onTouchStart_saveImg.bind(this));
        this.saveImg.addEventListener('touchcancel', this.onTouchCancel_saveImg.bind(this));
        // this.saveImg.addEventListener('touchmove', this.onTouchCancel_saveImg.bind(this));
        this.saveImg.addEventListener('touchend', this.onTouchCancel_saveImg.bind(this));
        this.bookImg.addEventListener('click', this.onClick_bookImg.bind(this));
        window.addEventListener('resize', this.onWindowResize.bind(this));


        this.initBuired();
        this.initSprite();
        this.initTipsAni();
        this.initStarCloudAni();
        this.initStarAni();
        this.fixPosition();


    }

    // 初始化埋点信息
    initBuired() {
        Buried.init();
        if (window['isInApp']) {
            // 端内
            // 星星埋点信息
            let starDpmIn = Buried.connectDpm(110, 3, 1); // 点击埋点   
            let starDcmIn = Buried.connectDcm(202, 0, 0); // 曝光埋点
            this.starExposure = Buried.createExposure(starDpmIn, starDcmIn);

            // 重选埋点信息
            let reChooseDpmIn = Buried.connectDpm(110, 3, 2); // 点击埋点   
            let reChooseDcmIn = Buried.connectDcm(202, 0, 0); // 曝光埋点
            this.reChooseExposure = Buried.createExposure(reChooseDpmIn, reChooseDcmIn);

            // 长按埋点信息
            let longSaveDpmIn = Buried.connectDpm(110, 3, 3); // 点击埋点   
            let longSaveDcmIn = Buried.connectDcm(202, 0, 0); // 曝光埋点
            this.longSaveExposure = Buried.createExposure(longSaveDpmIn, longSaveDcmIn);

            // 端内分享埋点信息 !! 埋点信息不同
            let shareDpmIn = Buried.connectDpm(110, 3, 4); // 点击埋点   
            let shareDcmIn = 305 + '.' + 0 + '.' + 1 + '.' + Buried['oaId']; // Buried.connectDcm(202, 0, 0); // 曝光埋点!! 埋点信息不同 305.0.1.oaid
            this.shareExposure = Buried.createExposure(shareDpmIn, shareDcmIn);

            // 下载埋点信息 !! 埋点信息不同
            let downloadDpmIn = Buried.connectDpm(110, 4, 5); // 点击埋点   
            let downloadDcmIn = Buried.connectDcm(202, 0, 0); // 曝光埋点
            this.downloadExposure = Buried.createExposure(downloadDpmIn, downloadDcmIn);
        } else {
            // 端外
            // 星星埋点信息
            let starDpmOut = Buried.connectDpm(110, 13, 1); // 点击埋点   
            let starDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
            this.starExposure = Buried.createExposure(starDpmOut, starDcmOut);

            // 重选埋点信息
            let reChooseDpmOut = Buried.connectDpm(110, 13, 2); // 点击埋点   
            let reChooseDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
            this.reChooseExposure = Buried.createExposure(reChooseDpmOut, reChooseDcmOut);

            // 长按埋点信息
            let longSaveDpmOut = Buried.connectDpm(110, 13, 3); // 点击埋点   
            let longSaveDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
            this.longSaveExposure = Buried.createExposure(longSaveDpmOut, longSaveDcmOut);

            // 端外分享埋点信息 !! 埋点信息不同
            let shareDpmOut = Buried.connectDpm(110, 13, 4); // 点击埋点   
            let shareDcmOut = 305 + '.' + 0 + '.' + 1 + '.' + Buried['oaId']; // Buried.connectDcm(202, 0, 0); // 曝光埋点!! 埋点信息不同 305.0.1.oaid
            this.shareExposure = Buried.createExposure(shareDpmOut, shareDcmOut);

            // 分享埋点信息 !! 埋点信息不同
            let downloadDpmOut = Buried.connectDpm(110, 14, 5); // 点击埋点   
            let downloadDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
            this.downloadExposure = Buried.createExposure(downloadDpmOut, downloadDcmOut);
        }
        // 星星曝光埋点
        NetManager.ins.showLog(this.starExposure);
    }

    // 初始化所有事件
    initEvents() {
        this.star1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star4.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star5.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star6.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.shareGuide.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_ShareGuide, this);
        this.endTxtPlane['book'].addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Book, this);
        this.endTxtPlane['reChooseBtn'].addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_ReChooseBtn, this);
        this.endTxtPlane['shareBtn'].addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_ShareBtn, this);

    }

    // 移除所有事件
    removeEvents() {
        this.star1.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star2.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star3.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star4.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star5.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.star6.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Star, this);
        this.shareGuide.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_ShareGuide, this);
        this.endTxtPlane['book'].removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_Book, this);
        this.endTxtPlane['reChooseBtn'].removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_ReChooseBtn, this);
        this.endTxtPlane['shareBtn'].removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_ShareBtn, this);

        this.domReChooseBtn.removeEventListener('click', this.onClick_ReChooseBtn.bind(this));
        this.domShareBtn.removeEventListener('click', this.onClick_ShareBtn.bind(this));
        this.saveImg.removeEventListener('touchstart', this.onTouchStart_saveImg.bind(this));
        this.saveImg.removeEventListener('touchcancel', this.onTouchCancel_saveImg.bind(this));
        // this.saveImg.removeEventListener('touchmove', this.onTouchCancel_saveImg.bind(this));
        this.saveImg.removeEventListener('touchend', this.onTouchCancel_saveImg.bind(this));
        this.bookImg.removeEventListener('click', this.onClick_bookImg.bind(this));
        window.removeEventListener('resize', this.onWindowResize.bind(this));
    }

    onTap_Star(e) {
        let scope = this;
        NetManager.ins.clickLog(scope.starExposure);

        NetManager.ins.showLog(scope.reChooseExposure);
        NetManager.ins.showLog(scope.shareExposure);
        NetManager.ins.showLog(scope.longSaveExposure);
        NetManager.ins.showLog(scope.downloadExposure);
        // if (true) {
        if (window['isInApp']) {

            // 在app内
            scope.darkMask.visible = true;
            scope.nickNameTxt.visible = true;
            scope.guideTxt.visible = true;
            let keyName = ['la', 'hj', 'stl', 'mks', 'lyh', 'jy'][`${Math.round(Math.random() * 5)}`];
            scope.currPeople = new PeopleConfig(keyName).currPeople;
            scope.peopleHead.source = `${scope.currPeople.name}Head_png`;
            scope.peopleHead.visible = true;
            scope.peopleName.source = `${scope.currPeople.name}_png`;
            scope.peopleName.visible = true;
            scope.endTxtPlane['sex'].source = `${scope.currPeople.sex}_png`;
            scope.currWordIndex = Math.round(Math.random() * (scope.currPeople.wordsNum - 1));
            scope.endTxtPlane['words'].source = scope.currPeople.words[scope.currWordIndex];
            // scope.endTxtPlane['book'].source = `//yun.duiba.com.cn/db_games/activity/endStockTaking1.0/books/${scope.currPeople.books[scope.currWordIndex]}.png`;
            scope.endTxtPlane['book'].source = `${scope.currPeople.books[scope.currWordIndex]}_png`;
            scope.endTxtPlane['longSaveBtn'].visible = false;
            scope.endTxtPlane['reChooseBtn'].x = 375 - 60 - 205 / 2;
            scope.endTxtPlane['shareBtn'].x = 375 + 30 + 205 / 2;
            scope.endTxtPlane.visible = true;

            scope.star2.visible = false;
            scope.star2Glow.visible = false;
            scope.tips.visible = false;

            scope.nickNameTxt.x = -500;
            scope.guideTxt.x = -500;
            scope.peopleName.x = -500;
            scope.peopleHead.x = 750 + 500;
            scope.endTxtPlane.x = 750 + 500;
            let nickNameTW = egret.Tween.get(scope.nickNameTxt);
            nickNameTW.to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.nickNameTxt.width / 2 }, 200);
            let guideTW = egret.Tween.get(scope.guideTxt);
            guideTW.to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.guideTxt.width / 2 }, 200);
            let peopleNameTW = egret.Tween.get(scope.peopleName);
            peopleNameTW.to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.peopleName.width / 2 }, 200);
            let peopleHeadTW = egret.Tween.get(scope.peopleHead);
            peopleHeadTW.wait(100).to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.peopleHead.width / 2 }, 200);
            let endTxtTW = egret.Tween.get(scope.endTxtPlane);
            endTxtTW.wait(100).to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.endTxtPlane.width / 2 }, 200).wait(500).call(() => {
                if (!window['isToastGuide']) {
                    window['isToastGuide'] = true;
                    // let toastGuide = document.createElement('div');
                    // toastGuide.innerHTML = "分享好友可以长按保存图片哦~";
                    // toastGuide.id = "toastGuide"
                    // toastGuide.style.background = "rgb(0, 0, 0, 0.7)";
                    // toastGuide.style.width = "250px";
                    // toastGuide.style.height = "4rem";
                    // toastGuide.style.borderRadius = "5px";
                    // toastGuide.style.textAlign = "center";
                    // toastGuide.style.lineHeight = "4rem";
                    // toastGuide.style.color = "#fff";
                    // toastGuide.style.left = `${document.body.clientWidth / 2 - 125}px`;
                    // toastGuide.style.top = `30%`;
                    // toastGuide.style.position = 'absolute';
                    // document.body.append(toastGuide);
                    this.addChild(this.toastGuideBg);
                    this.addChild(this.toastGUideTxt);
                    this.toastGUideTxt.visible = true;
                    this.toastGuideBg.visible = true;
                    setTimeout(() => {
                        this.toastGUideTxt.visible = false;
                        this.toastGuideBg.visible = false;
                        // toastGuide.remove();
                    }, 2500)
                }
            })
        } else {
            // 在app外
            scope.darkMask.visible = true;
            scope.nickNameTxt.visible = true;
            scope.guideTxt.visible = true;
            let keyName = ['la', 'hj', 'stl', 'mks', 'lyh', 'jy'][`${Math.round(Math.random() * 5)}`];
            scope.currPeople = new PeopleConfig(keyName).currPeople;
            scope.peopleHead.source = `${scope.currPeople.name}Head_png`;
            scope.peopleHead.visible = true;
            scope.peopleName.source = `${scope.currPeople.name}_png`;
            scope.peopleName.visible = true;
            scope.endTxtPlane['sex'].source = `${scope.currPeople.sex}_png`;
            scope.currWordIndex = Math.round(Math.random() * (scope.currPeople.wordsNum - 1));
            scope.endTxtPlane['words'].source = scope.currPeople.words[scope.currWordIndex];
            scope.endTxtPlane['book'].source = `${scope.currPeople.books[scope.currWordIndex]}_png`;

            scope.bookImg.src = `//yun.duiba.com.cn/db_games/activity/endStockTaking1.0/books/${scope.currPeople.books[scope.currWordIndex]}.png`;
            let bookImgWidthP = 134.58 / egret.MainContext.instance.stage.stageWidth;
            scope.bookImg.style.width = `${bookImgWidthP * 100}%`;
            let bookImgHeightP = 118 / egret.MainContext.instance.stage.stageHeight;
            scope.bookImg.style.height = `${bookImgHeightP * 100}%`;
            let bookImgLeftP = 133.34 / egret.MainContext.instance.stage.stageWidth;
            scope.bookImg.style.left = `${bookImgLeftP * 100}%`;
            let bookImgTopP = (536.7 + 365) / egret.MainContext.instance.stage.stageHeight;
            scope.bookImg.style.top = `${bookImgTopP * 100}%`;
            scope.bookImg.style.position = 'absolute';
            scope.bookImg.style.display = 'none';

            scope.endTxtPlane.visible = true;

            scope.star2.visible = false;
            scope.star2Glow.visible = false;
            scope.tips.visible = false;

            scope.nickNameTxt.x = -500;
            scope.guideTxt.x = -500;
            scope.peopleName.x = -500;
            scope.peopleHead.x = 750 + 500;
            scope.endTxtPlane.x = 750 + 500;
            let nickNameTW = egret.Tween.get(scope.nickNameTxt);
            nickNameTW.to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.nickNameTxt.width / 2 }, 200);
            let guideTW = egret.Tween.get(scope.guideTxt);
            guideTW.to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.guideTxt.width / 2 }, 200);
            let peopleNameTW = egret.Tween.get(scope.peopleName);
            peopleNameTW.to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.peopleName.width / 2 }, 200);
            let peopleHeadTW = egret.Tween.get(scope.peopleHead);
            peopleHeadTW.wait(100).to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.peopleHead.width / 2 }, 200);
            let endTxtTW = egret.Tween.get(scope.endTxtPlane);
            endTxtTW.wait(100).to({ x: egret.MainContext.instance.stage.stageWidth / 2 - scope.endTxtPlane.width / 2 }, 200).wait(100).call(() => {
                scope.bookImg.style.display = 'block';
                scope.domReChooseBtn.style.display = 'block';
                scope.domShareBtn.style.display = 'block';
                scope.saveImg.style.display = 'block';

                // 生成图
                scope.endTxtPlane['reChooseBtn'].visible = false;
                scope.endTxtPlane['shareBtn'].visible = false;
                scope.endTxtPlane['longSaveBtn'].visible = false;
                let renderTexture: egret.RenderTexture = new egret.RenderTexture();
                renderTexture.drawToTexture(scope, new egret.Rectangle(0, 0, 750, egret.MainContext.instance.stage.stageHeight));
                // egret.updateAllScreens();

                let imageData = renderTexture.toDataURL("image/png");
                scope.saveImg.style.position = 'absolute';
                let widthP = 750 / egret.MainContext.instance.stage.stageWidth;
                scope.saveImg.style.width = `${widthP * 100}%`;
                let leftP = 0;
                scope.saveImg.style.left = `${leftP * 100}%`;
                let topP = 0;
                scope.saveImg.style.top = `${topP * 100}%`;
                scope.saveImg.style.opacity = 0;
                scope.saveImg['src'] = imageData;
                scope.saveImg.onload = () => {
                    // if (!window['isToastGuide']) {
                    //     window['isToastGuide'] = true;
                    //     this.addChild(this.toastGuideBg);
                    //     this.addChild(this.toastGUideTxt);
                    //     this.toastGUideTxt.visible = true;
                    //     this.toastGuideBg.visible = true;
                    //     setTimeout(() => {
                    //         this.toastGUideTxt.visible = false;
                    //         this.toastGuideBg.visible = false;
                    //         // toastGuide.remove();
                    //     }, 1500)
                    // }
                }

                scope.endTxtPlane['longSaveBtn'].visible = true;
                // scope.domReChooseBtn.style.background = `url(//yun.duiba.com.cn/db_games/activity/endStockTaking1.0/reChooseBtn.png) 100% 100%`
                // scope.domReChooseBtn.style['background-size'] = `cover`;
                scope.domReChooseBtn.style.position = 'absolute';
                let reChooseWidthP = 205 / egret.MainContext.instance.stage.stageWidth;
                scope.domReChooseBtn.style.width = `${reChooseWidthP * 100}%`;
                let reChooseHeightP = 87 / egret.MainContext.instance.stage.stageHeight;
                scope.domReChooseBtn.style.height = `${reChooseHeightP * 100}%`;
                let reChooseLeftP = 73.5 / egret.MainContext.instance.stage.stageWidth;
                scope.domReChooseBtn.style.left = `${reChooseLeftP * 100}%`;
                let reChooseTopP = (536.7 + 507.73) / egret.MainContext.instance.stage.stageHeight;
                scope.domReChooseBtn.style.top = `${reChooseTopP * 100}%`;

                // scope.domShareBtn.style.background = `url(//yun.duiba.com.cn/db_games/activity/endStockTaking1.0/shareBtn.png) 100% 100%`
                // scope.domShareBtn.style['background-size'] = `cover`;
                scope.domShareBtn.style.position = 'absolute';
                let shareWidthP = 205 / egret.MainContext.instance.stage.stageWidth;
                scope.domShareBtn.style.width = `${shareWidthP * 100}%`;
                let shareHeightP = 87 / egret.MainContext.instance.stage.stageHeight;
                scope.domShareBtn.style.height = `${shareHeightP * 100}%`;
                let shareLeftP = 472.5 / egret.MainContext.instance.stage.stageWidth;
                scope.domShareBtn.style.left = `${shareLeftP * 100}%`;
                let shareTopP = (536.7 + 505.73) / egret.MainContext.instance.stage.stageHeight;
                scope.domShareBtn.style.top = `${shareTopP * 100}%`;
            });


            window['shareMsg'] = {
                shareTitle: `${scope.nickName} 邀你进入2018文学世界时光之旅`,
                shareDesc: "看看哪个关键句，将会代表你的2019年？",
                shareLink: encodeURI(`${window['baseShareUrl']}&share=true&nickname=${scope.nickName ? encodeURIComponent(scope.nickName) : ''}&people=${scope.currPeople.name}&wordindex=${scope.currWordIndex}`),
                shareImg: "https://yun.duiba.com.cn/db_games/activity/endStockTaking1.0/shareImg.png",
            }

            this.initShareFunc();
        }
        window['shareUrl'] = encodeURI(`${window['baseShareUrl']}&share=true&nickname=${scope.nickName ? encodeURIComponent(scope.nickName) : ''}&people=${scope.currPeople.name}&wordindex=${scope.currWordIndex}`);
        console.log(window['shareUrl']);
    }
    public initShareFunc = () => {
        let scope = this;
        window['wx'].ready(() => {
            let shareTitle = window['shareMsg'].shareTitle;  //【微信分享好友的标题
            let shareWord = window['shareMsg'].shareDesc;  //【微信分享好友的描述
            let shareTitle2 = window['shareMsg'].shareTitle; //【微信分享朋友圈的标题
            let link = `${window['baseShareUrl']}&share=true&nickname=${scope.nickName ? encodeURIComponent(scope.nickName) : ''}&people=${scope.currPeople.name}&wordindex=${scope.currWordIndex}`;  //【微信分享出去的链接地址
            let imgUrl = window['shareMsg'].shareImg;  //【微信分享图标路径
            console.log('wxShareUrl:' + link);
            //监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
            window['wx'].onMenuShareTimeline({
                title: shareTitle2,
                desc: shareTitle2,
                link: link,
                imgUrl: imgUrl,
                success: function (res) {
                }
            });
            //监听“分享给朋友”按钮点击、自定义分享内容及分享结果接口
            window['wx'].onMenuShareAppMessage({
                title: shareTitle,
                desc: shareWord,
                link: link,
                imgUrl: imgUrl,
                success: function (res) {
                }
            });
        });
        window['wx'].error(function (res) {
            // alert(JSON.stringify(res))
        });
    };
    onWindowResize() {
        if (!window['isInApp']) {
            // 生成图
            if (!this.endTxtPlane['reChooseBtn'] || !this.endTxtPlane['shareBtn'] || !this.endTxtPlane['longSaveBtn'] || !this.endTxtPlane['book']) {
                return
            }
            // this.endTxtPlane['reChooseBtn'].visible = false;
            // this.endTxtPlane['shareBtn'].visible = false;
            // this.endTxtPlane['longSaveBtn'].visible = false;
            // this.img = document.getElementById('saveImg');
            // let renderTexture: egret.RenderTexture = new egret.RenderTexture();
            // renderTexture.drawToTexture(this, new egret.Rectangle(0, 0, 750, egret.MainContext.instance.stage.stageHeight));
            // egret.updateAllScreens();

            // let imageData = renderTexture.toDataURL("image/png");
            this.saveImg.style.position = 'absolute';
            let widthP = 750 / egret.MainContext.instance.stage.stageWidth;
            this.saveImg.style.width = `${widthP * 100}%`;
            let leftP = 0;
            this.saveImg.style.left = `${leftP * 100}%`;
            let topP = 0;
            this.saveImg.style.top = `${topP * 100}%`;
            this.saveImg.style.opacity = 0;
            // this.saveImg['src'] = imageData;
            // this.saveImg.onload = () => {
            //     this.endTxtPlane['longSaveBtn'].visible = true;
            // }

            this.domReChooseBtn.style.position = 'absolute';
            let reChooseWidthP = 205 / egret.MainContext.instance.stage.stageWidth;
            this.domReChooseBtn.style.width = `${reChooseWidthP * 100}%`;
            let reChooseHeightP = 87 / egret.MainContext.instance.stage.stageHeight;
            this.domReChooseBtn.style.height = `${reChooseHeightP * 100}%`;
            let reChooseLeftP = 73.5 / egret.MainContext.instance.stage.stageWidth;
            this.domReChooseBtn.style.left = `${reChooseLeftP * 100}%`;
            let reChooseTopP = (536.7 + 507.73) / egret.MainContext.instance.stage.stageHeight;
            this.domReChooseBtn.style.top = `${reChooseTopP * 100}%`;

            this.domShareBtn.style.position = 'absolute';
            let shareWidthP = 205 / egret.MainContext.instance.stage.stageWidth;
            this.domShareBtn.style.width = `${shareWidthP * 100}%`;
            let shareHeightP = 87 / egret.MainContext.instance.stage.stageHeight;
            this.domShareBtn.style.height = `${shareHeightP * 100}%`;
            let shareLeftP = 472.5 / egret.MainContext.instance.stage.stageWidth;
            this.domShareBtn.style.left = `${shareLeftP * 100}%`;
            let shareTopP = (536.7 + 505.73) / egret.MainContext.instance.stage.stageHeight;
            this.domShareBtn.style.top = `${shareTopP * 100}%`;

            this.bookImg.style.position = 'absolute';
            let bookImgWidthP = 134.58 / egret.MainContext.instance.stage.stageWidth;
            this.bookImg.style.width = `${bookImgWidthP * 100}%`;
            let bookImgHeightP = 118 / egret.MainContext.instance.stage.stageHeight;
            this.bookImg.style.height = `${bookImgHeightP * 100}%`;
            let bookImgLeftP = 133.34 / egret.MainContext.instance.stage.stageWidth;
            this.bookImg.style.left = `${bookImgLeftP * 100}%`;
            let bookImgTopP = (536.7 + 365) / egret.MainContext.instance.stage.stageHeight;
            this.bookImg.style.top = `${bookImgTopP * 100}%`;
        }
    }

    // 重选
    onTap_ReChooseBtn() {
        NetManager.ins.clickLog(this.reChooseExposure);

        this.darkMask.visible = false;
        this.star2.visible = true;
        this.star2Glow.visible = true;
        this.tips.visible = true;

        this.endTxtPlane['reChooseBtn'].visible = true;
        this.endTxtPlane['shareBtn'].visible = true;

        let nickNameTW = egret.Tween.get(this.nickNameTxt);
        nickNameTW.to({ x: -500 }, 200);
        let guideTW = egret.Tween.get(this.guideTxt);
        guideTW.to({ x: -500 }, 200);
        let peopleNameTW = egret.Tween.get(this.peopleName);
        peopleNameTW.to({ x: -500 }, 200);
        let peopleHeadTW = egret.Tween.get(this.peopleHead);
        peopleHeadTW.wait(100).to({ x: 750 + 500 }, 200);
        let endTxtTW = egret.Tween.get(this.endTxtPlane);
        endTxtTW.wait(100).to({ x: 750 + 500 }, 200);
    }

    // 端外重选按钮
    onClick_ReChooseBtn() {
        this.onTap_ReChooseBtn()
        this.bookImg.style.display = 'none';
        this.domReChooseBtn.style.display = 'none';
        this.domShareBtn.style.display = 'none';
        this.saveImg.style.display = 'none';
    }

    // 分享
    onTap_ShareBtn() {

        this.endTxtPlane['shareBtn'].touchEnabled = false;
        NetManager.ins.clickLog(this.shareExposure);
        let tout = setTimeout(() => {
            clearTimeout(tout);
            this.endTxtPlane['shareBtn'].touchEnabled = true;
        }, 1000)

        // if (true) {
        if (window['isInApp']) {
            // 端内调用端内分享方法
            // let data = {
            //     shareUrl: window['shareUrl'] ? window['shareUrl'] : `${window['baseShareUrl']}&share=true&nickname=${this.nickName}&people=${this.currPeople.name}&wordindex=${this.currWordIndex}`,//链接
            //     shareTitle: window['shareTitle'] ? window['shareTitle'] : `${this.nickName} 邀你进入2018文学世界时光之旅`,//标题
            //     shareContent: window['shareContent'] ? window['shareContent'] : "看看哪个关键句，将会代表你的2019年？",//描述
            //     imgUrl: window['imgUrl'] ? window['imgUrl'] : "//yun.duiba.com.cn/db_games/activity/endStockTaking1.0/shareImg.png" //图片
            // }
            let data = {
                shareUrl: window['shareUrl'] ? window['shareUrl'] : `${window['baseShareUrl']}&share=true&nickname=${this.nickName}&people=${this.currPeople.name}&wordindex=${this.currWordIndex}`,//链接
                shareTitle: window['shareTitle'] ? window['shareTitle'] : `${this.nickName} 邀你进入2018文学世界时光之旅`,//标题
                shareContent: window['shareContent'] ? window['shareContent'] : "看看哪个关键句，将会代表你的2019年？",//描述
                imgUrl: window['imgUrl'] ? window['imgUrl'] : "//yun.duiba.com.cn/db_games/activity/endStockTaking1.0/shareImg.png" //图片
            }
            console.log("data.shareUrl=" + data.shareUrl)
            try {
                window['anShuqiForSqWebJS'].openAppSendShareData(JSON.stringify(data));
            } catch{
                PanelCtrl.instance.show(ModuleTypes.MSG_PANEL, { msg: '分享失败', type: 'tips' });
                return
            }
        } else {
            // 端外展示分享蒙层
            this.addChild(this.shareGuide);
            this.shareGuide.visible = true;
            this.endTxtPlane['reChooseBtn'].visible = true;
            this.endTxtPlane['shareBtn'].visible = true;
            this.bookImg.style.display = 'none';
            this.saveImg.style.display = 'none';
            this.domReChooseBtn.style.display = 'none';
            this.domShareBtn.style.display = 'none';
        }
    }

    // 分享蒙层
    onTap_ShareGuide() {
        this.shareGuide.visible = false;
        this.endTxtPlane['reChooseBtn'].visible = false;
        this.endTxtPlane['shareBtn'].visible = false;
        this.bookImg.style.display = 'block';
        this.saveImg.style.display = 'block';
        this.domReChooseBtn.style.display = 'block';
        this.domShareBtn.style.display = 'block';
    }

    // 点canvas书跳转至书的位置
    onTap_Book() {
        NetManager.ins.clickLog(this.downloadExposure);
        console.log(`bookId:${this.currPeople.bids[this.currWordIndex]}`);
        //调用
        try {
            window['nativeApi']("openAppBookCover", {
                "bookId": this.currPeople.bids[this.currWordIndex] //书籍id
            });
        } catch{
            PanelCtrl.instance.show(ModuleTypes.MSG_PANEL, { msg: '跳转链接失败', type: 'tips' })
        }

    }

    // 点dom书跳转链接下载
    onClick_bookImg() {
        NetManager.ins.clickLog(this.downloadExposure);
        window.location.href = window['downloadAppUrl'] ? window['downloadAppUrl'] : encodeURIComponent("http://t.shuqi.com/route.php?shuqi_h5=102_209_6#/ac/in/ct/download");
    }

    // 端外分享按钮
    onClick_ShareBtn() {
        this.onTap_ShareBtn();
    }

    // 长按保存图片开始
    onTouchStart_saveImg() {
        this.currStartTimeStamp = Date.now();
    }
    // 长按保存图片结束
    onTouchCancel_saveImg() {
        let endTimeStamp = Date.now();
        if (endTimeStamp - this.currStartTimeStamp > 400) {
            NetManager.ins.clickLog(this.longSaveExposure);
        }
        this.currStartTimeStamp = endTimeStamp;
    }

    // 初始化粒子效果
    initSprite() {
        for (let i = 0; i < 25; i++) {
            let sprite = new eui.Image();
            sprite.source = `upSprite${[1, 2][Math.round(Math.random())]}_png`;
            sprite.anchorOffsetX = sprite.width / 2;
            sprite.anchorOffsetY = sprite.height / 2;
            sprite.rotation = 360 * Math.random();
            let scale = Math.max(Math.random() * 0.8, 0.5);
            sprite.scaleX = scale;
            sprite.scaleY = scale;
            sprite.x = Math.max(Math.min(750 * Math.random(), 700), 50 + [1, -1][Math.round(Math.random())] * 20 * Math.random());
            sprite.y = 150 + 800 * Math.random();
            let targetY = Math.max(Math.abs(sprite.y - 500 * Math.random()), 150);
            this.addChild(sprite);
            let spriteTW = egret.Tween.get(sprite);
            spriteTW.to({ y: targetY }, Math.max(Math.random() * 10000, 5000)).call(() => {
                this.removeChild(sprite);
            })
        }
        for (let i = 0; i < 80; i++) {
            let sprite = new eui.Image();
            sprite.source = `upSprite${[1, 2][Math.round(Math.random())]}_png`;
            sprite.anchorOffsetX = sprite.width / 2;
            sprite.anchorOffsetY = sprite.height / 2;
            sprite.rotation = 360 * Math.random();
            let scale = Math.max(Math.random() * 0.8, 0.5);
            sprite.scaleX = scale;
            sprite.scaleY = scale;
            sprite.x = Math.max(Math.min(750 * Math.random(), 675), 75);
            sprite.y = 150 + egret.MainContext.instance.stage.stageHeight * 3 / 4 * Math.random();
            sprite.alpha = 0;
            let targetY = Math.max(Math.abs(sprite.y - 500 * Math.random()), 150 + 50 * Math.random());
            this.addChild(sprite);
            let spriteTW = egret.Tween.get(sprite, { loop: true });
            spriteTW.wait(7000 * Math.random()).to({ alpha: 1 }, 1).to({ y: targetY }, Math.max(Math.random() * 10000, 5000))
        }
    }

    // 初始化选择提示动画
    initTipsAni() {
        let tw = egret.Tween.get(this.tips, { loop: true });
        tw.to({ alpha: 0.6 }, 1500).to({ alpha: 1 }, 1500);
    }

    // 初始化星星动画
    initStarAni() {
        for (let i = 1; i <= 6; i++) {
            this.addChild(this[`star${i}`])
            let tempY = this[`star${i}`].y;
            let starTW = egret.Tween.get(this[`star${i}`], { loop: true, onChange: this.onStarMove.bind(this) });

            this.addChild(this[`star6F`]);
            starTW.wait(3000 * Math.random()).call(() => {
                let starTW2 = egret.Tween.get(this[`star${i}`], { loop: true, onChange: this.onStarMove.bind(this) });
                starTW2.to({ y: tempY - 10 }, 1200, egret.Ease.cubicOut).to({ y: tempY + 10 }, 2400, egret.Ease.cubicInOut).to({ y: tempY }, 1200, egret.Ease.cubicIn)
                let starGlowTW = egret.Tween.get(this[`star${i}Glow`], { loop: true });
                starGlowTW.to({ scaleX: 0.6, scaleY: 0.6 }, 2000).to({ scaleX: 1, scaleY: 1 }, 2000);

                if (i == 6) {
                    let tempY = this[`star6F`].y;
                    let starTW = egret.Tween.get(this[`star6F`], { loop: true });
                    starTW.to({ y: tempY - 10 }, 1200, egret.Ease.cubicOut).to({ y: tempY + 10 }, 2400, egret.Ease.cubicInOut).to({ y: tempY }, 1200, egret.Ease.cubicIn);
                }
            })
        }

        this.addChild(this.planet);
        let tempX = this.planet.x;
        let planetTW = egret.Tween.get(this.planet, { loop: true });
        planetTW.to({ x: tempX - 5 }, 1200).to({ x: tempX + 5 }, 2400).to({ x: tempX }, 1200);

    }

    // 初始化星云动画
    initStarCloudAni() {
        let starCloudP = new egret.Sprite();
        starCloudP.width = 600;
        starCloudP.height = 500;
        starCloudP.anchorOffsetX = 250;
        starCloudP.anchorOffsetY = 250;
        starCloudP.x = 150;
        starCloudP.y = 1350;
        starCloudP.scaleX = 0.6;
        starCloudP.scaleY = 1.5;
        starCloudP.rotation = -50;
        this.addChild(starCloudP);
        let starCloud = new eui.Image();
        starCloud.source = `starCloud1_png`;
        starCloud.width = 400;
        starCloud.height = 500;
        starCloud.scaleX = 1;
        starCloud.scaleY = 1;
        starCloud.anchorOffsetX = starCloud.width * 0.45;
        starCloud.anchorOffsetY = starCloud.height * 0.45;
        starCloud.x = 300;
        starCloud.y = 250;
        starCloud.alpha = 0.4;
        starCloudP.addChild(starCloud);
        let tw = egret.Tween.get(starCloud, { loop: true });
        tw.to({ rotation: -360 }, 120000);

        let starCloudP2 = new egret.Sprite();
        starCloudP2.width = 600;
        starCloudP2.height = 500;
        starCloudP2.anchorOffsetX = 250;
        starCloudP2.anchorOffsetY = 250;
        starCloudP2.x = 150;
        starCloudP2.y = 250;
        starCloudP2.scaleX = 0.8;
        starCloudP2.scaleY = 1.6;
        starCloudP2.rotation = 30;
        this.addChild(starCloudP2);
        let starCloud2 = new eui.Image();
        starCloud2.source = `starCloud2_png`;
        starCloud2.width = 600;
        starCloud2.height = 500;
        starCloud2.scaleX = 1;
        starCloud2.scaleY = 1;
        starCloud2.anchorOffsetX = starCloud2.width * 0.45;
        starCloud2.anchorOffsetY = starCloud2.height * 0.45;
        starCloud2.x = 300;
        starCloud2.y = 250;
        starCloud2.alpha = 0.8;
        starCloudP2.addChild(starCloud2);
        let tw2 = egret.Tween.get(starCloud2, { loop: true });
        tw2.to({ rotation: -360 }, 120000);
    }

    // 调整位置
    fixPosition() {
        this.addChild(this.darkMask);
        this.addChild(this.nickNameTxt);
        this.addChild(this.guideTxt);
        this.addChild(this.peopleName);
        this.addChild(this.peopleHead);

        this.addChild(this.endTxtPlane);
    }

    // 星星移动同步背景光亮移动
    onStarMove() {
        for (let i = 1; i <= 6; i++) {
            this[`star${i}Glow`].y = this[`star${i}`].y;
        }
    }
}