import { Buried } from './../../libs/tw/util/Buried';
import { LotteryType } from './../../libs/tw/enum/LotteryType';
import Panel from "../views/Panel";
import SceneCtrl from "../ctrls/sceneCtrl";
import { ModuleTypes } from "../types/sceneTypes";
import { DataManager } from "../../libs/tw/manager/DataManager";
import { NetManager } from "../../libs/tw/manager/NetManager";
import PanelCtrl from "../ctrls/panelCtrl";
import Loading from "../loading/Loading";
import Layers from "../views/layers"
export default class RevivePanel extends Panel {

    public panelGroup: eui.Group;
    public bg: eui.Image;
    public titleImg: eui.Image;
    public continueBtn: eui.Button;
    public shareBtn: eui.Button;
    public closeBtn: eui.Button;
    public msgImg: eui.Image;
    public shareGuide: eui.Image;
    public inShareCloser: eui.Rect;
    public inShareGroup: eui.Group;
    public shareBg: eui.Rect;
    public pyqBtn: eui.Button;
    public wxBtn: eui.Button;


    private shareIMG: any;
    private mdata: any;
    private scene: any;

    public get skinKey() { return 'Revive' }
    public get groupName() { return 'revive_panel' }

    constructor(data: any) {
        super(data);
        this.mdata = data;
        this.scene = data.scene;
    }

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

        // 复活按钮埋点
        let reviveDpmOut = Buried.connectDpm(110, 4, 1); // 点击埋点   
        let reviveDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
        let reviveExposure = Buried.createExposure(reviveDpmOut, reviveDcmOut);

        NetManager.ins.showLog(reviveExposure);
    }

    start(): void {
        this.percentHeight = 100;

        this.shareIMG = '[encoded data, 303872 characters]'
    }

    initEvents(): void {
        super.initEvents();
        this.shareBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_shareBtn, this);
        this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_closeBtn, this);
        this.continueBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_continueBtn, this);
        this.shareGuide.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_shareGuide, this);
        this.inShareCloser.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_inShareCloser, this);
        this.wxBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_wxBtn, this);
        this.pyqBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_pyqBtn, this);
    }

    removeEvents(): void {
        super.removeEvents();
        this.shareBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_shareBtn, this);
        this.closeBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_closeBtn, this);
        this.continueBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_continueBtn, this);
        this.shareGuide.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_shareGuide, this);
        this.inShareCloser.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_inShareCloser, this);
        this.wxBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_wxBtn, this);
        this.pyqBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_pyqBtn, this);
    }

    public isShared = false;
    public isShareTimeOut = false;
    // 马上复活按钮（分享）
    onTap_shareBtn() {
        PanelCtrl.instance.btnDelay(this.shareBtn);

        // 复活按钮埋点
        let reviveDpmOut = Buried.connectDpm(110, 4, 1); // 点击埋点   
        let reviveDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
        let reviveExposure = Buried.createExposure(reviveDpmOut, reviveDcmOut);

        NetManager.ins.clickLog(reviveExposure);


        const isMPBank = this.isMPBank();
        if (isMPBank) {
            // 端内分享
            this.inShareGroup.y = this.stage.stageHeight;
            egret.Tween.get(this.inShareGroup).to({ y: this.stage.stageHeight - 280 }, 300)
            this.inShareGroup.visible = true;
            this.inShareCloser.visible = true;

            // Loading.instace.show();
            //'weixinsession';   //weixinsession:微信会话分享
            //data:image/png;base64,


        } else {
            // 端外分享
            this.shareGuide.visible = true;

            setTimeout(() => {
                this.isShareTimeOut = true;
            }, 5000)
        }
    }

    onTap_wxBtn() {
        PanelCtrl.instance.btnDelay(this.wxBtn);
        Loading.instace.show();

        let sharetype = 'weixinsession'
        let sharePosterImg = encodeURIComponent(this.shareIMG);
        let cmd = 'http://cmbls/socialshare?id=ZPsocialshare&channel=' + sharetype + '&type=image&imageData=' + sharePosterImg;
        setTimeout(() => {
            this.cmblsJSExecutor(cmd);
        }, 500)

        window['CMBLS'].socialShare.successCallback = (id, message) => {
            // alert("CMBLS socialshare调用成功，id:" + id + ",message: " + message);
            // alert('成功');
            if (!this.isShared) {
                this.isShared = true;
                console.log("CMBLS socialshare调用成功，id: " + id + ", message: " + message);
                Loading.instace.hide();
                this.shareBtn.visible = false;
                this.shareGuide.visible = false;
                this.closeBtn.visible = false;

                this.inShareGroup.visible = false;
                this.inShareCloser.visible = false;
                this.continueBtn.visible = true;
                this.msgImg.source = `reviveMsg2_png`;

                // 继续游戏按钮埋点
                let continueDpmOut = Buried.connectDpm(110, 5, 1); // 点击埋点   
                let continueDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
                let continueExposure = Buried.createExposure(continueDpmOut, continueDcmOut);

                NetManager.ins.showLog(continueExposure);
            }

        };
        window['CMBLS'].socialShare.failCallback = (id, message) => {
            // alert("CMBLS socialshare调用失败，id:" + id + ",message: " + message);
            // alert('失败');
            if (!this.isShared) {
                this.isShared = true;
                Loading.instace.hide();
                PanelCtrl.instance.showMsg('okBtn', 8, null, null, () => {
                    // SceneCtrl.instance.change(ModuleTypes.GAME_SCENE)
                    window.location.href = window['appIndexLink'];
                }, () => {
                    // SceneCtrl.instance.change(ModuleTypes.GAME_SCENE)
                    window.location.href = window['appIndexLink'];
                }, '分享失败');
            }

        };
    }

    onTap_pyqBtn() {
        PanelCtrl.instance.btnDelay(this.pyqBtn);
        Loading.instace.show();
        // 朋友圈
        let sharetype = 'weixintimeline'
        let sharePosterImg = encodeURIComponent(this.shareIMG);
        let cmd = 'http://cmbls/socialshare?id=ZPsocialshare&channel=' + sharetype + '&type=image&imageData=' + sharePosterImg;
        setTimeout(() => {
            this.cmblsJSExecutor(cmd);
        }, 500)



        window['CMBLS'].socialShare.successCallback = (id, message) => {
            // alert("CMBLS socialshare调用成功，id:" + id + ",message: " + message);
            // alert('成功');
            if (!this.isShared) {
                this.isShared = true;
                console.log("CMBLS socialshare调用成功，id: " + id + ", message: " + message);
                Loading.instace.hide();
                this.shareBtn.visible = false;
                this.shareGuide.visible = false;
                this.closeBtn.visible = false;
                this.continueBtn.visible = true;

                this.inShareGroup.visible = false;
                this.inShareCloser.visible = false;
                this.msgImg.source = `reviveMsg2_png`;

                // 继续游戏按钮埋点
                let continueDpmOut = Buried.connectDpm(110, 5, 1); // 点击埋点   
                let continueDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
                let continueExposure = Buried.createExposure(continueDpmOut, continueDcmOut);

                NetManager.ins.showLog(continueExposure);
            }

        };
        window['CMBLS'].socialShare.failCallback = (id, message) => {
            // alert("CMBLS socialshare调用失败，id:" + id + ",message: " + message);
            // alert('失败');
            if (!this.isShared) {
                this.isShared = true;
                Loading.instace.hide();
                PanelCtrl.instance.showMsg('okBtn', 8, null, null, () => {
                    // SceneCtrl.instance.change(ModuleTypes.GAME_SCENE)
                    window.location.href = window['appIndexLink'];
                }, () => {
                    // SceneCtrl.instance.change(ModuleTypes.GAME_SCENE)
                    window.location.href = window['appIndexLink'];
                }, '分享失败');
            }

        };
    }

    onTap_inShareCloser() {
        this.inShareCloser.visible = false;
        egret.Tween.get(this.inShareGroup).to({ y: this.stage.stageHeight }, 300).call(() => {
            this.inShareGroup.visible = false;
        })

    }

    onTap_closeBtn() {
        // SceneCtrl.instance.change(ModuleTypes.GAME_SCENE);
        if (this.isMPBank()) {
            // 端内 活动首页
            window.location.href = window['appIndexLink'];
        } else {
            // 端外 活动首页
            window.location.href = window['outIndexLink'];
        }
    }

    onTap_continueBtn() {
        this.hidePanel();

        // 继续游戏按钮埋点
        let continueDpmOut = Buried.connectDpm(110, 5, 1); // 点击埋点   
        let continueDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
        let continueExposure = Buried.createExposure(continueDpmOut, continueDcmOut);

        NetManager.ins.clickLog(continueExposure);

        this.scene.revive();
    }

    onTap_shareGuide() {
        if (this.isShareTimeOut) {
            this.shareBtn.visible = false;
            this.shareGuide.visible = false;
            this.closeBtn.visible = false;
            this.continueBtn.visible = true;
            this.msgImg.source = `reviveMsg2_png`;

            // 继续游戏按钮埋点
            let continueDpmOut = Buried.connectDpm(110, 5, 1); // 点击埋点   
            let continueDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
            let continueExposure = Buried.createExposure(continueDpmOut, continueDcmOut);

            NetManager.ins.clickLog(continueExposure);
        } else {
            let tips = new eui.Label()
            tips.text = `只有分享后才可以复活哦～`;
            tips.x = this.stage.stageWidth / 2 - tips.width / 2
            tips.y = 650;
            // tips.size = ;
            Layers.topLayer.addChild(tips)
            Layers.topLayer.visible = true;
            setTimeout(() => {
                Layers.topLayer.visible = false;
                Layers.topLayer.removeChild(tips)
            }, 1000);
        }
    }

    private XMLGetFirstValue(e, t) {
        var a = "",
            n = "<" + t + ">",
            r = "</" + t + ">",
            l = e.indexOf(n);
        if (0 > l) return a;
        var c = l + n.length,
            u = e.indexOf(r, c);
        return 0 > u ? a : a = e.substring(c, u)
    };

    private shareStatus: boolean;
    private cmblsJSExecutor(cmblsCommand) {
        const self = this;
        self.shareStatus = true;
        if (window['cmblsExecutor']) {
            var cmblsExecutor = window['cmblsExecutor'] || {};
            window['cmblsExecutor'].executeCmbls("1.0", cmblsCommand);
            setTimeout(() => {
                if (!this.isShared) {
                    this.isShared = true;
                    // console.log("CMBLS socialshare调用成功，id: " + id + ", message: " + message);
                    Loading.instace.hide();
                    this.shareBtn.visible = false;
                    this.shareGuide.visible = false;
                    this.closeBtn.visible = false;

                    this.inShareGroup.visible = false;
                    this.inShareCloser.visible = false;
                    this.continueBtn.visible = true;
                    this.msgImg.source = `reviveMsg2_png`;

                    // 继续游戏按钮埋点
                    let continueDpmOut = Buried.connectDpm(110, 5, 1); // 点击埋点   
                    let continueDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
                    let continueExposure = Buried.createExposure(continueDpmOut, continueDcmOut);

                    NetManager.ins.showLog(continueExposure);
                }
            }, 6000);
        } else {
            document.addEventListener('CMBLSExecutorReady', () => {
                var cmblsExecutor = window['cmblsExecutor'] || {};
                window['cmblsExecutor'].executeCmbls("1.0", cmblsCommand);
            }, false);

            setTimeout(() => {
                if (!window['cmblsExecutor'] || !this.isShared) {
                    this.isShared = true;
                    // console.log("CMBLS socialshare调用成功，id: " + id + ", message: " + message);
                    Loading.instace.hide();
                    this.shareBtn.visible = false;
                    this.shareGuide.visible = false;
                    this.closeBtn.visible = false;

                    this.inShareGroup.visible = false;
                    this.inShareCloser.visible = false;
                    this.continueBtn.visible = true;
                    this.msgImg.source = `reviveMsg2_png`;

                    // 继续游戏按钮埋点
                    let continueDpmOut = Buried.connectDpm(110, 5, 1); // 点击埋点   
                    let continueDcmOut = Buried.connectDcm(202, 0, 0); // 曝光埋点
                    let continueExposure = Buried.createExposure(continueDpmOut, continueDcmOut);

                    NetManager.ins.showLog(continueExposure);
                }
            }, 6000);
        }
    };

    private isMPBank() {
        var ua = navigator.userAgent;
        // var ua = 'MPBank'
        if (ua.indexOf('MPBank') > -1)
            return true;
        else
            return false;
    }
}