import Panel from "../views/Panel";
import { GFun } from "../../libs/tc/util/GFun";
import PanelCtrl from "../ctrls/panelCtrl";
import { GetBuriedData } from "../ctrls/GetBuriedData";
import { NetManager } from "../../libs/tw/manager/NetManager";
import Loading from "../loading/Loading";
import { ModuleTypes } from "../types/sceneTypes";
import StartScene from "../startScene/StartScene";

export default class RedLuckyPanel extends Panel {
    public get skinKey() { return 'RedLuckyPanel' }
    public get groupName() { return 'pop' }
    private mData:any;
    public redlab1:eui.Image;
    public redlab2:eui.Image;
    public rednum:eui.Label;
    public closeBtn:eui.Button;
    public zfbGroup:eui.Group;
    public zfbnum:eui.Label;
    public zfbBtn:eui.Button;

    public redlab:eui.Image;
    public wxGroup:eui.Group;
    public wxnum:eui.Label;

    private jinzfbExposure:any;
    private cardzfbExposure:any;
    private jinwxExposure:any;
    private cardwxExposure:any;
    private prizedata:any;

    public clickBG:eui.Rect;

    constructor(data?) {
        super(data);
        this.mData = data;
        this.prizedata = data.prizedata;
        // this.startList.height = 1080/window.innerWidth*window.innerHeight;
    }

    initEvents() {
        // super.initEvents();
       this.zfbBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickZFBbtn,this);
       this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.closePanel,this);

       this.clickBG.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closePanel, this);
    }

    removeEvents() {
        // super.removeEvents();
        this.zfbBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickZFBbtn,this);
        this.closeBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.closePanel,this);

        this.clickBG.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closePanel, this);
    }

    start() {
        super.start();
        this.percentHeight = 100;
        
    }
    private cityJson;
    protected async preLoadRes() {
        //加载背景并设置背景图片

        return new Promise(function (resolve, reject) {
            resolve();
        });
        
    }

    protected onSkinComplete(): void {
        super.onSkinComplete();
        this.once(egret.Event.ADDED_TO_STAGE, this.onLoad, this);
        if (this.stage) {
            this.onLoad();
        }
    }

    private onLoad(): void {
        this.initUI();
    }

    initUI(){
        if(this.mData.type == 1){
            this.redlab.source = "redlab3_png"
        }else{
            this.redlab.source = "redlab1_png"
        }
        this.rednum.text = this.prizedata.prizeName;
        if(GFun.getIsWxClient()){
            this.wxGroup.visible = true;
            this.zfbGroup.visible = false;
            this.wxnum.text = this.prizedata.prizeName + "元";
            if(this.mData.type == 1){
                this.cardwxExposure = GetBuriedData(23)
                NetManager.ins.showLog(this.cardwxExposure);
            }else{
                this.jinwxExposure = GetBuriedData(22)
                NetManager.ins.showLog(this.jinwxExposure);
            }

        }else{
            if(this.mData.type == 1){
                this.cardzfbExposure = GetBuriedData(21)
                NetManager.ins.showLog(this.cardzfbExposure);
            }else{
                this.jinzfbExposure = GetBuriedData(20)
                NetManager.ins.showLog(this.jinzfbExposure);
            }
            this.wxGroup.visible = false;
            this.zfbGroup.visible = true;
            this.zfbnum.text = this.prizedata.prizeName + "元";
        }
    }
    onClickZFBbtn(){
        if(this.mData.type == 1){
            NetManager.ins.clickLog(this.cardzfbExposure);
        }else{
            NetManager.ins.clickLog(this.jinzfbExposure);
        }
        PanelCtrl.instance.btnDelay(this.zfbBtn);
        if(this.prizedata && this.prizedata.url){
            window.location.href = this.prizedata.url;
        }
    }

    closePanel(){
        Loading.instace.show();
        NetManager.ins.queryTaskList((success, result) => {
            if (!success) {
                PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: result.message })
            }
            NetManager.ins.getBaseInfo((success2, result2) => {
                if (!success2) {
                    PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: result2.message })
                }
                NetManager.ins.getSubscibeState((success3, result3) => {
                    Loading.instace.hide();
                    if (!success3) {
                        PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: result2.message })
                    }
                    let data = { baseinfo: result2.data, taskinfo: result.data, pushstatus: result3.data.status };
                    // SceneCtrl.instance.change(ModuleTypes.START_SCENE,data);
                    StartScene.instance.mData = data;
                    StartScene.instance.initUI();
                    this.hidePanel();
                    // StartScene.instance.startList.viewport.scrollV = this.listscroV;
                })

            })
        })
    }
}