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

export default class CardPanel extends Panel {
    public get skinKey() { return 'CardPanel' }
    public get groupName() { return 'card' }
    private mData:any;

    public cardGroup:eui.Group;
    public cardImg:eui.Image;
    public turnBtn:eui.Button;
    public turn2Btn:eui.Button;

    public cardjinGroup:eui.Group;
    public shopBtn:eui.Image;
    public cardjinImg:eui.Image;
    public erweima:eui.Image;
    public turn3Btn:eui.Group;

    public closeBtn:eui.Button;
    public backBtn:eui.Button;
    public shareBtn:eui.Button;
    public clickBG:eui.Rect;
    public lab:eui.Image;
    
    private backExposure:any;
    private jinExposure:any;
    private cardstatus = 1;

    public turnlab:eui.Image;

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

    initEvents() {
        // super.initEvents();
        
        this.backBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.backPanel,this);
        this.shareBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.shareUrl,this);
       
    }

    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(){
        
    }

    initUI(){
        this.turnlab.visible = false;
        if(this.mData.cardid == 6){
            this.lab.source = "nocardjinlab_png"
            this.jinExposure = GetBuriedData(17)
            NetManager.ins.showLog(this.jinExposure);
            this.cardjinGroup.visible = false;
            if(this.mData.cardnum == 1){
                this.cardImg.source = "cardlucky3_png"
                //this.shareBtn.visible = false;
                this.backBtn.visible = true;
                this.turnBtn.visible = false;
                this.turn2Btn.visible = false;
                this.clickBG.addEventListener(egret.TouchEvent.TOUCH_TAP,this.closePanel,this);
                this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.closePanel,this);
            }else{
                this.cardGroup.visible = true;
                this.cardjinGroup.visible = false;
                this.cardImg.source = "cardlucky_png"
                this.turn2Btn.visible = false;
                this.lab.source = "jinlilab_png";
                this.turnlab.visible = true;
                //this.shareBtn.visible = true;
                this.backBtn.visible = false;
                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);
                this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.checkCard,this);
            }
        }else{
            this.cardGroup.visible = true;
            this.cardjinGroup.visible = false;
            this.turn2Btn.visible = false;
            
            this.lab.source = "nocardlab_png"
            this.clickBG.addEventListener(egret.TouchEvent.TOUCH_TAP,this.closePanel,this);
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.closePanel,this);
            if(this.mData.cardid == 1){
                if(this.mData.cardnum==0){
                    this.cardImg.source = "cardpeach3_png"
                    this.turnBtn.visible = false;
                    //this.shareBtn.visible = false;
                    this.backBtn.visible = true;
                }else{
                    this.cardImg.source = "cardpeach_png"
                    this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnAni,this);
                    //this.shareBtn.visible = true;
                    this.backBtn.visible = false;
                }
            }else if(this.mData.cardid == 2){
                if(this.mData.cardnum==0){
                    this.cardImg.source = "cardfina3_png"
                    this.turnBtn.visible = false;
                    //this.shareBtn.visible = false;
                    this.backBtn.visible = true;
                }else{
                    this.cardImg.source = "cardfina_png"
                    this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnAni,this);
                    //this.shareBtn.visible = true;
                    this.backBtn.visible = false;
                }
            }else if(this.mData.cardid == 3){
                if(this.mData.cardnum==0){
                    this.cardImg.source = "cardcase3_png"
                    this.turnBtn.visible = false;
                    //this.shareBtn.visible = false;
                    this.backBtn.visible = true;
                }else{
                    this.cardImg.source = "cardcase_png"
                    this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnAni,this);
                    //this.shareBtn.visible = true;
                    this.backBtn.visible = false;
                }
            }else if(this.mData.cardid == 4){
                if(this.mData.cardnum==0){
                    this.cardImg.source = "cardhapp3_png"
                    this.turnBtn.visible = false;
                    ///this.shareBtn.visible = false;
                    this.backBtn.visible = true;
                }else{
                    this.cardImg.source = "cardhapp_png"
                    this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnAni,this);
                    //this.shareBtn.visible = true;
                    this.backBtn.visible = false;
                }
            }else if(this.mData.cardid == 5){
                if(this.mData.cardnum==0){
                    this.cardImg.source = "cardheal3_png"
                    this.turnBtn.visible = false;
                    //this.shareBtn.visible = false;
                    this.backBtn.visible = true;
                }else{
                    this.cardImg.source = "cardheal_png"
                    this.turnBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.turnAni,this);
                    //this.shareBtn.visible = true;
                    this.backBtn.visible = false;
                }
            }
        }
        if(this.backBtn.visible == true){
            this.backExposure = GetBuriedData(18)
            NetManager.ins.showLog(this.backExposure);
        }
    }

    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.cardid == 6){
                this.cardGroup.visible = false;
                this.cardjinGroup.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.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.turnAni,this);

            }else if(this.mData.cardid == 1){
                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.cardid == 2){
                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.cardid == 3){
                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.cardid == 4){
                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.cardid == 5){
                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.cardid == 6){
                this.cardGroup.visible = true;
                this.cardjinGroup.visible = false;
                this.turnlab.visible = true;
            }else if(this.mData.cardid == 1){
                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.cardid == 2){
                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.cardid == 3){
                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.cardid == 4){
                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.cardid == 5){
                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.show(ModuleTypes.STORE_PANEL);
    }

    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()
        }
    }
    
    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)
        })
    }

    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+账号去领取红包吧～'});
                    this.hidePanel();
                    return
                }
                NetManager.ins.getPrize((success2,result2)=>{
                    if(!success2){
                        // PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL,{msg:'网络异常，请重试~'});
                        this.hidePanel()
                        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.hidePanel()
                    return
                }
                let data = {prizedata:result.data, type:2}
                PanelCtrl.instance.show(ModuleTypes.REDLUCKY_PANEL,data);
                this.hidePanel();
            },"2","1")
        }
    }
    closePanel(){

        PanelCtrl.instance.btnDelay(this.clickBG);
        PanelCtrl.instance.btnDelay(this.closeBtn);
        if(this.cardstatus == 2){
            this.turnAni();
            setTimeout(()=>{
                this.hidePanel();
            },500)
        }else{
            setTimeout(()=>{
                this.hidePanel();
            },100)
        }
        
        
    }
    backPanel(){
        NetManager.ins.clickLog(this.backExposure);

        PanelCtrl.instance.btnDelay(this.backBtn);
        if(this.cardstatus == 2){
            this.turnAni();
            setTimeout(()=>{
                this.hidePanel();
                //StartScene.instance.startList.viewport.scrollV = StartScene.instance.diheight - 1080 / window.innerWidth * window.innerHeight
            },500)
        }else{
            setTimeout(()=>{
                this.hidePanel();
                //StartScene.instance.startList.viewport.scrollV = StartScene.instance.diheight - 1080 / window.innerWidth * window.innerHeight
            },100)
        }
        
        
    }
    turnAni(){
        if(this.mData.cardid == 6){
            if(this.cardstatus == 1){
                egret.Tween.get(this.cardGroup)
                .to({scaleX:0},200)
                .call(()=>{
                    egret.Tween.get(this.cardjinGroup)
                    .set({scaleX:0})
                    .to({scaleX:1.2},200)
                    setTimeout(()=>{
                        this.turnCard();
                    },10)
                    
                })
            }else{
                egret.Tween.get(this.cardjinGroup)
                .to({scaleX:0},200)
                .call(()=>{
                    egret.Tween.get(this.cardGroup)
                    .set({scaleX:0})
                    .to({scaleX:1.2},200)
                    this.turnCard();
                })
            }
            
        }else{
            egret.Tween.get(this.cardGroup)
            .to({scaleX:0},200)
            .call(()=>{
                this.turnCard();
                egret.Tween.get(this.cardGroup)
                .to({scaleX:1.2},200)
            })
        }
        
    }

    shareUrl() {
        PanelCtrl.instance.btnDelay(this.shareBtn);
        let iswx = 1;
        if (GFun.getIsWxClient()) {
            iswx = 2;
        }
         else {
            iswx = 1;
        }
        
        if (iswx == 2) {
            //微信走二次分享 蒙层分享
            //PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL,{msg:'点击右上角，喊好友来集卡吧～'});
            
        } else {
            PanelCtrl.instance.show(ModuleTypes.WXSHARE_PANEL);
        }

    }
}