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

export default class GetCardPanel extends Panel {
    public get skinKey() { return 'GetCardPanel' }
    public get groupName() { return 'card' }


    private mData: any;
    public getGroup: eui.Group;
    public title1: eui.Image;
    public title2: eui.Image;
    public closeBtn: eui.Button;
    public turnlab: eui.Image;
    public cardGroup: eui.Group;
    public shareBtn: eui.Button;
    public cardImg: eui.Image;
    public turnBtn: eui.Button;
    public turn2Btn: eui.Button;
    public jinli2: eui.Group;
    public cardjinImg: eui.Image;
    public shopBtn: eui.Button;
    public erweima: eui.Image;
    public turn3Btn: eui.Group;
    public clickBG: eui.Rect;
    public shareimg: eui.Image;
    public light: eui.Image;

    private turnExposure: any;
    private shareExposure: any;
    private turnjin1Exposure: any;
    private turnjin2Exposure: any;
    private cardstatus = 1;
    constructor(data?) {
        super(data);
        this.mData = data;
        // this.startList.height = 1080/window.innerWidth*window.innerHeight;
    }

    initEvents() {
        // super.initEvents();
        this.shareBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.shareUrl, this);
        if (this.mData.prizeId == "egg_card") {

        } else {

        }

    }

    removeEvents() {
        super.removeEvents();
    }

    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();
        // this.initBuried()
    }
    initBuried() {
        this.turnExposure = GetBuriedData(14)
        NetManager.ins.showLog(this.turnExposure);
        this.shareExposure = GetBuriedData(13)
        NetManager.ins.showLog(this.shareExposure);
    }

    initUI() {
        this.shareimg.visible = false;
        this.jinli2.visible = false;
        this.turnlab.visible = false;
        this.showAni()
        if (this.mData.prizeId == "egg_card") {
            this.turnjin1Exposure = GetBuriedData(15)
            NetManager.ins.showLog(this.turnjin1Exposure);
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.checkCard, this);
            this.cardImg.source = "cardlucky_png"
            //this.shareBtn.visible = false;
            this.turnlab.visible = true;
            this.turnBtn.visible = true;
            this.turn2Btn.visible = false;
            this.title1.source = "cardluckylab_png"
            this.title2.source = "jinlilab_png"
            this.cardImg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);
            this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);
            this.clickBG.addEventListener(egret.TouchEvent.TOUCH_TAP, this.checkCard, this);
        } else {
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closePanel, this);
            this.turnExposure = GetBuriedData(14)
            NetManager.ins.showLog(this.turnExposure);
            this.shareExposure = GetBuriedData(13)
            NetManager.ins.showLog(this.shareExposure);
            this.cardGroup.visible = true;
            this.jinli2.visible = false;
            this.turn2Btn.visible = false;
            this.title2.source = "cardlab_png"
            this.clickBG.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closePanel, this);
            if (this.mData.prizeId == "card_1") {
                this.title1.source = "cardpeachlab_png"
                this.cardImg.source = "cardpeach_png"
                this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);


            } else if (this.mData.prizeId == "card_2") {
                this.title1.source = "cardfinalab_png"
                this.cardImg.source = "cardfina_png"
                this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);

            } else if (this.mData.prizeId == "card_3") {
                this.title1.source = "cardcaselab_png"
                this.cardImg.source = "cardcase_png"
                this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);

            } else if (this.mData.prizeId == "card_4") {
                this.title1.source = "cardhapplab_png"
                this.cardImg.source = "cardhapp_png"
                this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);

            } else if (this.mData.prizeId == "card_5") {
                this.title1.source = "cardheallab_png"
                this.cardImg.source = "cardheal_png"
                this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);

            }
        }
    }

    turnCard() {
        PanelCtrl.instance.btnDelay(this.turnBtn);
        PanelCtrl.instance.btnDelay(this.turn2Btn);
        PanelCtrl.instance.btnDelay(this.turn3Btn);
        if (this.cardstatus == 1) {

            //转过去
            this.cardstatus = 2;
            if (this.mData.prizeId == "egg_card") {
                NetManager.ins.clickLog(this.turnjin1Exposure)
                this.turnjin2Exposure = GetBuriedData(16)
                NetManager.ins.showLog(this.turnjin2Exposure);
                this.cardGroup.visible = false;
                this.jinli2.visible = true;
                this.turnlab.visible = false;
                //二维码显示
                this.geterweima();

                //门店显示
                this.shopBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showShop, this);
                this.turn3Btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);
                this.cardjinImg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);

            } else if (this.mData.prizeId == "card_1") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardpeach2_png"
                this.turnBtn.visible = false;
                this.turn2Btn.visible = true;
                this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);
            } else if (this.mData.prizeId == "card_2") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardfina2_png"
                this.turnBtn.visible = false;
                this.turn2Btn.visible = true;
                this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);
            } else if (this.mData.prizeId == "card_3") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardcase2_png"
                this.turnBtn.visible = false;
                this.turn2Btn.visible = true;
                this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);
            } else if (this.mData.prizeId == "card_4") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardhapp2_png"
                this.turnBtn.visible = false;
                this.turn2Btn.visible = true;
                this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);
            } else if (this.mData.prizeId == "card_5") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardheal2_png"
                this.turnBtn.visible = false;
                this.turn2Btn.visible = true;
                this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.turnAni, this);
            }
        } else {
            //转回来
            this.cardstatus = 1;
            if (this.mData.prizeId == "egg_card") {
                NetManager.ins.clickLog(this.turnjin2Exposure)
                this.cardGroup.visible = true;
                this.jinli2.visible = false;
                this.turnlab.visible = true;

            } else if (this.mData.prizeId == "card_1") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardpeach_png"
                this.turnBtn.visible = true;
                this.turn2Btn.visible = false;
                // this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnCard,this);
            } else if (this.mData.prizeId == "card_2") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardfina_png"
                this.turnBtn.visible = true;
                this.turn2Btn.visible = false;
                // this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnCard,this);
            } else if (this.mData.prizeId == "card_3") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardcase_png"
                this.turnBtn.visible = true;
                this.turn2Btn.visible = false;
                // this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnCard,this);
            } else if (this.mData.prizeId == "card_4") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardhapp_png"
                this.turnBtn.visible = true;
                this.turn2Btn.visible = false;
                // this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnCard,this);
            } else if (this.mData.prizeId == "card_5") {
                NetManager.ins.clickLog(this.turnExposure)
                this.cardImg.source = "cardheal_png"
                this.turnBtn.visible = true;
                this.turn2Btn.visible = false;
                // this.turn2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnCard,this);
            }
        }

    }

    showShop() {
        console.log('查看门店')
        PanelCtrl.instance.btnDelay(this.shopBtn);
        PanelCtrl.instance.show(ModuleTypes.STORE_PANEL);
    }

    shareUrl() {
        NetManager.ins.clickLog(this.shareExposure)
        PanelCtrl.instance.btnDelay(this.shareBtn);
        let iswx = 1;
        if (GFun.getIsWxClient()) {
            iswx = 2;
        } else {
            iswx = 1;
        }
        // NetManager.ins.genInvite((success,result)=>{
        //     if(!success){
        //         PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL,{msg:"网络异常，请重试~"});
        //         return
        //     }
        //     window['getlink'](result.data);


        // })
        if (iswx == 2) {
            //微信走二次分享 蒙层分享
            //this.getGroup.visible = false;
            //this.shareimg.visible = true;
            //PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL,{msg:'点击右上角，喊好友来集卡吧～'});
            this.clickBG.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.closePanel, this);
            this.clickBG.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeshare, this);
        } else {
            PanelCtrl.instance.show(ModuleTypes.WXSHARE_PANEL);
        }

    }

    closeshare() {
        this.getGroup.visible = true;
        this.shareimg.visible = false;
        this.clickBG.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.closeshare, this);
        this.clickBG.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closePanel, this);
    }

    geterweima() {
        NetManager.ins.getQrCodeUrl((success, result) => {
            if (!success) {
                PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '网络异常，请重试~' });
                return
            }
            NetManager.ins.getQrcode((success2, result2) => {
                if (!success2) {
                    PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '网络异常，请重试~' });
                    return
                }
                this.erweima.source = result2.data;
            }, result.data, 186, 186)
        })
    }

    checkCard() {
        if (StartScene.instance.mData.baseinfo.eggCardStatus == 2) {
            NetManager.ins.checkEggCard((success, result) => {
                if (!success) {
                    PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: "网络异常，请重试~" });
                    this.closePanel();
                    return
                }
                if (result.data.usedOffline) {
                    this.openPrize();
                } else {
                    this.closePanel();
                }

            })
        } else {
            this.closePanel()
        }

    }

    private openPrize() {
        console.log("开奖")
        if (GFun.getIsWxClient()) {
            NetManager.ins.saveOpenId((success, result) => {
                if (!success) {
                    PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '该微信账号已绑定过OPPO+账号啦，\n一个微信号只能绑定一个OPPO+账号哦，\n换回之前的OPPO+账号去领取红包吧～' });
                    return
                }
                NetManager.ins.getPrize((success2, result2) => {
                    if (!success2) {
                        // PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '网络异常，请重试~' });
                        this.closePanel()
                        return
                    }
                    let data = { prizedata: result2.data, type: 2 }
                    PanelCtrl.instance.show(ModuleTypes.REDLUCKY_PANEL, data);
                    this.hidePanel();

                }, "2", "2")
            })
        } else {
            NetManager.ins.getPrize((success, result) => {
                if (!success) {
                    // PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '网络异常，请重试~' });
                    this.closePanel()
                    return
                }
                let data = { prizedata: result.data, type: 2 }
                PanelCtrl.instance.show(ModuleTypes.REDLUCKY_PANEL, data);
                this.hidePanel();
            }, "2", "1")
        }
    }

    closePanel() {
        PanelCtrl.instance.btnDelay(this.closeBtn);
        PanelCtrl.instance.btnDelay(this.clickBG);

        if (this.cardstatus == 2) {
            this.turnAni();
            setTimeout(() => {
                this.moveAni();
            }, 500)
            setTimeout(() => {
                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;
                        })

                    })
                })

            }, 1200)
        } else {
            this.moveAni()
            setTimeout(() => {
                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;
                        })

                    })
                })

            }, 700)
        }

    }

    turnAni() {
        if (this.mData.prizeId == "egg_card") {
            if (this.cardstatus == 1) {
                egret.Tween.get(this.cardGroup)
                    .to({ scaleX: 0 }, 200)
                    .call(() => {
                        egret.Tween.get(this.jinli2)
                            .set({ scaleX: 0 })
                            .to({ scaleX: 1 }, 200)
                        setTimeout(() => {
                            this.turnCard();
                        }, 10)

                    })
            } else {
                egret.Tween.get(this.jinli2)
                    .to({ scaleX: 0 }, 200)
                    .call(() => {
                        egret.Tween.get(this.cardGroup)
                            .set({ scaleX: 0 })
                            .to({ scaleX: 1 }, 200)
                        this.turnCard();
                    })
            }

        } else {
            egret.Tween.get(this.cardGroup)
                .to({ scaleX: 0 }, 200)
                .call(() => {
                    this.turnCard();
                    egret.Tween.get(this.cardGroup)
                        .to({ scaleX: 1 }, 200)
                })
        }

    }

    moveAni() {
        this.pMask.visible = false;
        this.title1.visible = false;
        this.title2.visible = false;
        this.closeBtn.visible = false;
        this.turnlab.visible = false;
        //this.shareBtn.visible = false;
        if (this.mData.prizeId == "egg_card") {
            egret.Tween.get(this.cardGroup)
                .to({
                    x: StartScene.instance.jinGroup.x + StartScene.instance.jinGroup.width / 2 - 218,
                    y: StartScene.instance.jinGroup.y + StartScene.instance.jinGroup.height / 2 - StartScene.instance.startList.viewport.scrollV - this.getGroup.y,
                    scaleX: 0.58, scaleY: 0.59
                }, 500)
        } else if (this.mData.prizeId == "card_1") {
            egret.Tween.get(this.cardGroup)
                .to({
                    x: StartScene.instance.card1Group.x + StartScene.instance.card1Group.width / 2 - 218,
                    y: StartScene.instance.card1Group.y + StartScene.instance.card1Group.height / 2 - StartScene.instance.startList.viewport.scrollV - this.getGroup.y,
                    scaleX: 0.58, scaleY: 0.59
                }, 500)
        } else if (this.mData.prizeId == "card_2") {
            egret.Tween.get(this.cardGroup)
                .to({
                    x: StartScene.instance.card2Group.x + StartScene.instance.card2Group.width / 2 - 218,
                    y: StartScene.instance.card2Group.y + StartScene.instance.card2Group.height / 2 - StartScene.instance.startList.viewport.scrollV - this.getGroup.y,
                    scaleX: 0.58, scaleY: 0.59
                }, 500)
        } else if (this.mData.prizeId == "card_3") {
            egret.Tween.get(this.cardGroup)
                .to({
                    x: StartScene.instance.card3Group.x + StartScene.instance.card3Group.width / 2 - 218,
                    y: StartScene.instance.card3Group.y + StartScene.instance.card3Group.height / 2 - StartScene.instance.startList.viewport.scrollV - this.getGroup.y,
                    scaleX: 0.58, scaleY: 0.59
                }, 500)
        } else if (this.mData.prizeId == "card_4") {
            egret.Tween.get(this.cardGroup)
                .to({
                    x: StartScene.instance.card4Group.x + StartScene.instance.card4Group.width / 2 - 218,
                    y: StartScene.instance.card4Group.y + StartScene.instance.card4Group.height / 2 - StartScene.instance.startList.viewport.scrollV - this.getGroup.y,
                    scaleX: 0.58, scaleY: 0.59
                }, 500)
        } else if (this.mData.prizeId == "card_5") {
            egret.Tween.get(this.cardGroup)
                .to({
                    x: StartScene.instance.card5Group.x + StartScene.instance.card5Group.width / 2 - 218,
                    y: StartScene.instance.card5Group.y + StartScene.instance.card5Group.height / 2 - StartScene.instance.startList.viewport.scrollV - this.getGroup.y,
                    scaleX: 0.58, scaleY: 0.59
                }, 500)
        }
    }

    showAni() {
        egret.Tween.get(this.light)
            .set({ scaleX: 0, scaleY: 0, alpha: 1 })
            .to({ scaleX: 1.5, scaleY: 1.5 }, 300)
            .call(() => {
                egret.Tween.get(this.light)
                    .to({ scaleX: 0.8, scaleY: 0.8, alpha: 0 }, 200)

            })

        egret.Tween.get(this.cardGroup)
            .set({ scaleX: 0.7, scaleY: 0.7, alpha: 0 })
            .wait(150)
            .to({ scaleX: 1.04, scaleY: 1.04, alpha: 1 }, 100)
            .to({ scaleX: 1, scaleY: 1 }, 50)

    }

}