import Panel from "../views/Panel";
import { DataManager } from "../../libs/tw/manager/DataManager";
import getOptionImgSize from "./data/getOptionImgSize";
import ImgSizeType from "./data/ImgSizeType";
import { LotteryType } from "../../libs/tw/enum/LotteryType";
import { NetManager } from "../../libs/tw/manager/NetManager";
import showLog from "./ctrl/showLog";
import SceneCtrl from "../ctrls/sceneCtrl";
import { ModuleTypes } from "../types/sceneTypes";
import PanelCtrl from "../ctrls/panelCtrl";

import { Buried } from "../../libs/tw/util/Buried";

export default class PrizePanel extends Panel {

    protected get skinKey() { return 'Prize' }

    public closeBtn: eui.Button;
    public useBtn: eui.Button;
    public img: eui.Image;
    public prizeName: eui.Label;
    public scoreTxt: eui.Label;
    public maxScoreTxt: eui.Label;
    public costTxt: eui.Label;

    //埋点
    private exposure;

    // 开始
    start(data) {
        // const lottery = DataManager.ins.gameGetSubmitResultData.lottery;
        const lottery = DataManager.ins.getPlugOrderStatusData.lottery;
        this.data = lottery;

        const sizeType = getOptionImgSize(lottery);
        if (ImgSizeType.BIG === sizeType) {
            this.img.width = this.picBigWidth;
            this.img.height = this.picBigWidth / this.bigPicRatio;
        } else {
            this.img.width = this.img.height = this.picWidth;
        }
        this.img.y = 550;
        this.prizeName.y = 550 + 15 + this.img.height;

        showLog(lottery);

        if (lottery.type == 'collectGoods') {
            this.useBtn.visible = false;
        }

        this.initBuired();
    }

    get picWidth() { return 240 };
    get picBigWidth() { return 510 };
    get bigPicRatio() { return 640 / 300 }

    initBuired() {
        var dpm = Buried.connectDpm(40, 8, 1);
        var num;
        switch (window["currentPigData"].identifier) {
            case "wastonA":
                num = 1;
                break;
            case "wastonB":
                num = 2;
                break;
            case "wastonC":
                num = 3;
                break;
        }
        var dcm = Buried.connectDcm(230, window['skinID'], num);
        this.exposure = Buried.createExposure(dpm, dcm);

        //曝光
        NetManager.ins.showLog(this.exposure);
    }

    initEvents() {
        super.initEvents();
        this.img.once(egret.TouchEvent.TOUCH_TAP, this.onUse, this);
        this.useBtn.once(egret.TouchEvent.TOUCH_TAP, this.onUse, this);
        this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_CloseBtn, this)
    }

    onTap_CloseBtn(e: egret.TouchEvent) {
        this.hidePanel();
    }


    onUse() {
        //点击埋点
        NetManager.ins.clickLog(this.exposure);

        setTimeout(() => {
            const lottery = DataManager.ins.getPlugOrderStatusData.lottery;
            // if (lottery.type == LotteryType.COUPON || lottery.type == LotteryType.LUCKY) {
            if (lottery.type == LotteryType.LUCKY) {
                window["downloadAppConfig"] =
                    {
                        openUrl: lottery.openUrl,
                        iosDownloadUrl: lottery.iosDownloadUrl,
                        androidDownloadUrl: lottery.androidDownloadUrl,
                        confirm: lottery.confirm ? lottery.confirm : false
                    };
                window["downloadApp"]();
                if (lottery.type == "lucky") {
                    NetManager.ins.spmclick(DataManager.ins.gameGetSubmitResultData.exposure);
                }
            }
            else {
                console.log(`领奖链接：${lottery.link}`)
                window.location.href = lottery.link;
            }
        }, 1500)
    }
}