//首页界面ui脚本
var TableReader =require("TableReader");

cc.Class({
    extends: cc.Component,

    properties: {
        lauchNodeUI: cc.Node,  //点击发射飞镖节点
        storeNode: cc.Node,  //商城节点
        knifeIcon: cc.Sprite,  //飞刀图片
        pumpNode: cc.Node,  //飞刀特效
        pumpNode1: cc.Node, //飞刀特效1
        labGold: cc.Label,  //用户的金币数
        rankUI: cc.Node,  //排行榜界面节点
        fankBtn: cc.Node,
        shiwanUI: cc.Node,  //试玩游戏界面节点
        moreGameUI: cc.Node, //更多游戏界面节点
        lingjinbiBtn: cc.Node,  //领取金币按钮节点
        libaoBtn: cc.Node,  //礼包按钮节点
        libaoUI: cc.Node,  //添加小程序指导界面节点
        goldStacksTipsUI: cc.Node,  //领取奖励节点
        guozhidianUI: cc.Node,  //果汁店界面节点
        guozhidianBtn: cc.Node,  //果汁店按钮节点
        leave_node: cc.Node,
        online_main: cc.Node,
        online_already: cc.Node,
        qiandaoBtn: cc.Node,  //签到按钮节点
        qiandaoUI: cc.Node,  //签到界面节点
        xiaohongdian: cc.Node,  //果汁店小红点
        timeActiveTipUI: cc.Node, //限时奖励弹框(万圣节奖励)
        shuru: cc.EditBox,
        shanguang: [cc.Node],  //奖励背后的闪光图节点 //-----更换签到面板上的奖励显示
        pic: [cc.Sprite],  //奖励的图片
        rewardSp: [cc.SpriteFrame],//奖励的金币图片
        labText: [cc.Label],   //对奖励的描述
        gongzonghaoUI: cc.Node,  //公众号界面节点
        fuliBtn: cc.Node,  //福利按钮
        //-----------
        _noticeContent: null,  //公告内容
        _sysid: null,  //公告id
        _cutTime: null,
    },

    // LIFE-CYCLE CALLBACKS:
    onLoad () {  
        //右上方摆动礼包
        var seq = cc.repeatForever(cc.sequence(cc.rotateTo(2,15),cc.rotateTo(2,-15)));
        seq.easing(cc.easeIn(3.0));
        this.libaoBtn.runAction(seq);
        if(!sdk.getConfig4().ipLimit){  //ip限制
            if(sdk.getConfig1().sns){
                //签到弹框
                this.onQianDaoCheck();
            }
        }
        var tmp_data = wx.getLaunchOptionsSync();
        console.log("开始场景值",tmp_data.scene);
        if(tmp_data.scene ==1035 || tmp_data.scene ==1074){
            this.onLiBao(tmp_data.scene);
        }
        wx.onShow((res) => {
            console.log("不杀进程领取添加小程序奖励.....",res);
            if(res.scene ==1035 || res.scene ==1074){
                cc.HQ.GameMgr.gzhAppid =sdk.getConfig2().gzhAppid;
            }
            //右上方摆动礼包
            this.onLiBao(res.scene);
        },this);

        //果汁店上面小红点是否显示
        var self =this;
        // sdk.Post(sdk.ip2+ sdk.notice,{},function(data){
        //     console.log("公告返回",data);
        //     if(data.c ==1){
        //         let noticeNum =cc.HQ.GameMgr.getNotice(String(data.d.sysid));
        //         console.log("noticeNum公告id",Number(noticeNum));
        //         self._noticeContent =data.d.content;
        //         self._sysid =data.d.sysid;
        //         if(data.d.sysid ==Number(noticeNum)){   //用户已看过公告
        //             //关闭小红点
        //             self.xiaohongdian.active =false;     
        //         }else{
        //             //显示小红点
        //             self.xiaohongdian.active =true;
        //         }
        //     }
            //离线收益
            self.onLiXianReward();
        // });
 
        //万圣节活动奖励弹框
        // var strTime ="2018-11-1 00:00:00";
        // var date =new Date(strTime.replace(/-/g,'/'));
        // console.log("时间",date.getTime(),Date.now());
        // let func =function(data){
        //     console.log("万圣节",data);
        //     if(!data && Date.now()<date){   //万圣节当天奖励领取
        //         self.timeActiveTipUI.active =true;
        //     }
        // }
        // sdk.getFromServer("timeActiveKey","timeActiveData",null,func);
    },

    onEnable: function(){ 
        let myArr =cc.HQ.GameMgr.getKnife();   //拥有的飞刀
        let baodaoTime =cc.HQ.GameMgr.getBaodaoTime();
        let curDate =Date.now();   //当天的当前时间
        // console.log("11111111111111",myArr,Number(curDate -Number(baodaoTime)));
        if(Number(curDate -Number(baodaoTime)) > 60000){   //使用时间就算用完了
            // console.log("22222222222222",myArr);
            if(typeof(myArr)=="string"){
                myArr =myArr.split(",");
            }
            if(myArr.indexOf(82) != -1){
                myArr.splice(myArr.indexOf(82),1);
                // console.log("屠龙刀使用时间3",myArr);
                //屠龙刀使用时间到后，默认装备经典皮肤   (去掉屠龙皮肤)
                cc.HQ.GameMgr.setKnife(myArr);
                cc.HQ.GameMgr.setCurKnife(1);
            }
        }

        //用户拥有的金币数
        this.labGold.string =cc.HQ.GameMgr.getGold();
        // let myselfKnife =cc.HQ.GameMgr.getKnife();   //拥有的飞刀
        console.log("用户拥有的飞刀",myArr);
        cc.HQ.Local.weaponSkin =0;
        let skin =null;
        let userCurWeapan =null;
        if(cc.HQ.Local.weaponSkin>0){  //皮肤体验的还有次数
            skin =TableReader.getTableRowByNum("knifeItems",cc.HQ.Local.knifeSkinId);
        }else{
            // userCurWeapan =cc.HQ.GameMgr.getCurKnife();  //当前装备的飞刀
            userCurWeapan =cc.HQ.GameMgr.onSetCurrSkin();  //显示当前装备的飞刀
            skin =TableReader.getTableRowByNum("knifeItems",userCurWeapan);
        }
        //更换飞刀图片
        cc.HQ.GameMgr.onReplaceKnife(this.knifeIcon,skin); 
        if(cc.HQ.GameMgr.onCheckStarand(userCurWeapan)){   //特定的飞刀就显示飞刀特效
            this.pumpNode.active =true;
            this.pumpNode1.active =true;
        }else{
            this.pumpNode.active =false;
            this.pumpNode1.active =false;  
        }

        //主页跳转盒子按钮开关
        if(!sdk.getConfig4().ipLimit){  //ip限制
            if(sdk.getConfig1().hz){
                //显示更多游戏面板
                this.moreGameUI.active =true;
                this.lingjinbiBtn.active =true;
                this.libaoBtn.active =true;
                this.guozhidianBtn.active =true;
                this.qiandaoBtn.active =true;
                //------
            }else{
                this.moreGameUI.active =false;
                this.lingjinbiBtn.active =false;
                this.libaoBtn.active =false;
                this.guozhidianBtn.active =false;
                this.qiandaoBtn.active =false;
            }

            if(sdk.getConfig1().kf){
                this.fuliBtn.active =true;
            }else{
                this.fuliBtn.active =false;  
            }

            if(sdk.getConfig1().sns){
                //显示投诉按钮
                cc.HQ.GameMgr.feedbackButtonShow();
                this.fankBtn.active =true;
            }else{
                this.fankBtn.active =false;
            }
        }

        //让继续游戏按钮可以点击
        if(cc.HQ.Local.continueState || cc.HQ.Local.continueState>15){
            cc.HQ.Local.continueState =0;
        }else{
            cc.HQ.Local.continueState =0;
        }
    },

    //开始游戏按钮
    onStartGame: function(){
        if(this.shuru.string){
            cc.HQ.Local.CurrentLevels =Number(this.shuru.string);
        }
        this.node.active =false;
        this.lauchNodeUI.active =true;
    },

    //设置功能按钮
    onKefu: function(){
        this._showdikuang =!this._showdikuang;
        if(this._showdikuang){
            this.shezhidikuang.active =true;
            this.shezhidikuang.runAction(cc.moveTo(0.1,cc.p(0,109)));
        }else{
            this.shezhidikuang.runAction(cc.sequence(cc.moveTo(0.1,cc.p(0,-50)),cc.callFunc(function(){
                this.shezhidikuang.active =false;
            }.bind(this))));
        }
    },

    //分享功能按钮
    onFengxiang: function(){
        sdk.shareAppMessage({type:1,query:""});
        // let shareFun =function(){
        //     console.log("homePageUIhomePageUIhomePageUI");
        // }
        // cc.HQ.GameMgr.onSimulateShareBack(shareFun);
        // cc.HQ.GameMgr.clickShare =true;
    },

    //排行榜功能
    onPaihangbang: function(){
        // cc.HQ.Global.showTips(2,"此功能正在开发中，敬请期待!",0.1);
        //排行榜数据请求
        cc.HQ.GameMgr.onFriendRankReq(1,cc.HQ.Local.CurrentLevels);
        //显示排行界面节点
        this.rankUI.active =true;
    },

    //更换武器按钮
    onSwitchWeapons: function(){
        // cc.HQ.Global.showTips(2,"此功能正在开发中，敬请期待!",0.1);
        //隐藏banner广告
        var bannerAd = sdk.createBannerAd({});
        bannerAd.hide();
        //隐藏投诉按钮
        cc.HQ.GameMgr.feedbackButtonHide();
        //显示商城界面节点
        this.storeNode.getComponent("storeUI").initBegin(0);
        this.storeNode.active =true;
        this.node.active =false;
    },

    //开启或关闭声音
    onShenyinBtn: function(){
        cc.HQ.suoundState = !cc.HQ.suoundState;
        cc.HQ.soundMgr.setAudio(cc.HQ.suoundState);
        cc.HQ.shakeState = !cc.HQ.shakeState;  
    },

    //跳转小程序  (已弃用)
    onJump: function(){
        var d = sdk.getButtonConfig('open_micro_game_left');
        sdk.navigateToMiniProgram(d);        
    },

    //试玩游戏领取金币
    onShiwan: function(){
        this.shiwanUI.active =true;
    },

    onLibaoBtn: function(){
        //隐藏投诉按钮
        cc.HQ.GameMgr.feedbackButtonHide();
        this.libaoUI.active =true; 
    },

    //右上方摆动礼包
    onLiBao: function(scene){
        var self = this;
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            // console.log("场景值",scene);
            if(scene == 1104){
                //从我的小程序进去
                sdk.getFromServer("MyProgram","Program",null,(d)=>{
                    // console.log("ddddddd",d);
                    if(d == undefined){
                        self.goldStacksTipsUI.getComponent("goldStacksTipsUI").init(self._data,2);
                        self.goldStacksTipsUI.active =true;
                        sdk.setToServer("MyProgram","Program",1,-1);
                    }
                });
            }else if((scene ==1074 || scene == 1035) && (cc.HQ.GameMgr.gzhAppid ==sdk.getConfig2().gzhAppid)){   //从公众号会话下发的文字链进去
                console.log("公众号跳转进来的");
                sdk.getFromServer("offAcountkey","offAcountData",null,(d)=>{
                    if(d == undefined){
                        self.goldStacksTipsUI.getComponent("goldStacksTipsUI").init(self._data,5,6666);
                        self.goldStacksTipsUI.active =true;
                        sdk.setToServer("offAcountkey","offAcountData",1,-1);
                    }
                });        
            }
        }    
    },

    //打开果汁店
    onOpenShop:function(){
        var self = this;
        // if(this.xiaohongdian.active){   //若用户公告未看过，点击果汁店优先让他看公告
        //     this.guozhidianUI.getComponent("guozhidianUI").init(this._noticeContent,this._sysid,1);
        // }else{
        //     this.guozhidianUI.getComponent("guozhidianUI").init(this._noticeContent,this._sysid,0);    
        // }
        this.guozhidianUI.active = true;
        this.leave_node.active = false;
        // console.log("果汁果汁",this.leave_node,this.leave_node.active);
        sdk.getServerTime(function(d){
            sdk.getFromServer("DoubleTime","DoubleTime",null,(data)=>{
                if(data && data - d.nowTime>0){
                    self.online_already.active = true;
                    self.online_already.getChildByName("rest_time").getComponent(cc.Label).string = Math.round((data - d.nowTime)/3600)+"小时内有效";
                    self.online_main.active = false;
                    // self.guozhidianUI.getChildByName("spine").active = true;
                }else{
                    self.online_already.active = false;
                    self.online_main.active = true;
                    // self.guozhidianUI.getChildByName("spine").active = false;
                }
            })
        });
    },

    //福利按钮
    onFuli: function(){
        //隐藏投诉按钮
        cc.HQ.GameMgr.feedbackButtonHide();
        this.gongzonghaoUI.active =true;
         //埋点
         sdk.setSceneEvent("主页福利","点击","福利按钮",{},null);
    },

    //离线收益
    onLiXianReward: function(){
        var self = this;
        self.time_interval_fn();
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            cc.game.on(cc.game.EVENT_HIDE, function(){
                console.log("pause.....");
                sdk.getServerTime(function(d){
                    sdk.setToServer("UserLeave","UserLeave",d.nowTime,-1);
                    // if(self.leave_node.active == false){ //判断用户之前时间段有没有领取
                    //     sdk.getFromServer("UserLeave","UserLeave",null,(leave)=>{
                    //         if(leave == undefined){
                    //             sdk.setToServer("UserLeave","UserLeave",d.nowTime,-1);
                    //         }else{
                    //             var time_interval = d.nowTime - leave;
                    //             if(time_interval>=600){ // 已弹出 已领取
                    //                 sdk.setToServer("UserLeave","UserLeave",d.nowTime,-1);
                    //             }
                    //         }
                    //     })
                    // }else{
                    //     sdk.getFromServer("UserLeave","UserLeave",null,(leave)=>{ //用户在水果店节点打开的时候离开  但从未设置过离开时间时
                    //         // console.log("用户离开的时间",d);
                    //         if(leave == undefined){
                    //             sdk.setToServer("UserLeave","UserLeave",d.nowTime,-1);
                    //         }
                    //     })
                    // } 
                });
            },this);

            cc.game.on(cc.game.EVENT_SHOW, function(){
                console.log("继续游戏.....");
                self.scheduleOnce(function(){
                    self.time_interval_fn();
                },3);
                
            },this);
        }
       
    },

    time_interval_fn:function(){
        var self = this;
        sdk.getFromServer("UserLeave","UserLeave",null,(leave)=>{
            console.log("用户离开的时间",leave);
            var last_time = leave;
            sdk.getServerTime(function(d){
                if(d){
                    if(last_time == undefined){console.log("离开时间",last_time);last_time = d.nowTime};
                    var time_now = d.nowTime;
                    var time_interval = d.nowTime - last_time;
                    if(time_interval>43200){time_interval = 43200;time_now = Number(last_time)+43200}; //大于12小时
                    
                    console.log("时间间隔",time_interval);
                    if(time_interval>=600){ //时间间隔要大于10分钟
                        if(!sdk.getConfig4().ipLimit){  //ip限制
                            if(sdk.getConfig1().sns){
                                sdk.getFromServer("DoubleTime","DoubleTime",null,(data)=>{
                                    if(data>=time_now){ //离开的时间段都在双倍时间内
                                        console.log("都在双倍时间");
                                        // self.guozhidianUI.getChildByName("spine").active = true;
                                        self.leave_node.getChildByName("green_box").getChildByName("much").getComponent(cc.Label).string = Math.round(time_interval/60)*2;
                                        self.leave_node.getChildByName("yellow_box").getChildByName("much").getComponent(cc.Label).string = Math.round(time_interval/60)*4;
                                    }else{
                                        if(data>last_time){
                                            var double_coin = Math.round((data - last_time)/60)*2;
                                            var simple_coin = Math.round((d.nowTime - data)/60)*1;
                                            var all_coin = Number(double_coin)+Number(simple_coin);
                                            console.log("双倍时间",double_coin,"普通时间",simple_coin);
                                            self.leave_node.getChildByName("green_box").getChildByName("much").getComponent(cc.Label).string = all_coin;
                                            self.leave_node.getChildByName("yellow_box").getChildByName("much").getComponent(cc.Label).string = all_coin*2;
                                        }else{ //离开的时间段都 不 在双倍时间内
                                            console.log("都不在双倍时间");
                                            self.leave_node.getChildByName("green_box").getChildByName("much").getComponent(cc.Label).string = Math.round(time_interval/60)*1;
                                            self.leave_node.getChildByName("yellow_box").getChildByName("much").getComponent(cc.Label).string = Math.round(time_interval/60)*2;
                                        }
                                        
                                    }
                                    // if(time_interval == 43200){sdk.setToServer("DoubleTime","DoubleTime",0,-1);} //12小时之后来领取
                                })

                                // if(self.xiaohongdian.active){   //若用户公告未看过，点击果汁店优先让他看公告
                                //     self.guozhidianUI.getComponent("guozhidianUI").init(self._noticeContent,self._sysid,1);
                                // }else{
                                //     self.guozhidianUI.getComponent("guozhidianUI").init(self._noticeContent,self._sysid,0); 
                                // }
                                self.guozhidianUI.active = true;
                                self.leave_node.active = true;
                                self.online_already.active = false;
                                self.online_main.active = false;
                            }
                        }
                    } 
                }
            })
        })
    },

    //点击打开签到面板
    onOpenQiandao:function(){
        this.onQianDaoCheck();
        this.qiandaoUI.active = true;
        //隐藏投诉按钮
        cc.HQ.GameMgr.feedbackButtonHide();
    },

    onQianDaoCheck: function(){
        var self = this;
        var arr=[
            [200,"31",800,"47",1500,3000,"60"],  //签到奖励 (第一周)  "31"  200
            [200,"27",800,"45",1500,3000,"68"],  //签到奖励 (第二周)  "27" 
            [200,1500,800,3000,1500,3000,6000]   //签到奖励 (第三周及三周以后)  1500
        ];
        sdk.Post(sdk.ip2+"/Sign/check",{"uid":sdk.getUser().uid},function(data){
        // sdk.Post(sdk.ip2+"/Sign/check?debugDate="+this.shuru.string,{"uid":sdk.getUser().uid},function(data){
            // self.qiandaoUI.active = true;
            console.log("签到信息",data);
            //动态改变签到奖励的显示
            let reward =self.onDynamicSP(arr,data);

            var signDay = data.d.signDay;
            for(let i = 0;i<7;i++){
                if(i<=signDay){
                    self.qiandaoUI.getChildByName("day_7").children[i].color = cc.color(255,255,255,255);
                }
                if(i<signDay){
                    self.qiandaoUI.getChildByName("day_7").children[i].getChildByName("today").active = false;
                    self.qiandaoUI.getChildByName("day_7").children[i].getChildByName("yiqiandao").active = true;
                }
                if(i>signDay){
                    self.qiandaoUI.getChildByName("day_7").children[i].color = cc.color(128,128,128,255);
                    self.qiandaoUI.getChildByName("day_7").children[i].getChildByName("today").active = false;
                    self.qiandaoUI.getChildByName("day_7").children[i].getChildByName("yiqiandao").active = false;
                }
            }
        
            if(data.d.signStatus == 0){
                self.qiandaoUI.active = true;
                self.qiandaoUI.getChildByName("day_7").children[signDay].getChildByName("today").active = true;
                self.qiandaoUI.getChildByName("day_7").children[signDay].getChildByName("yiqiandao").active = false;
                self.qiandaoUI.getChildByName("lingqu").active = true;
                self.qiandaoUI.getChildByName("xuanyao").active = true;
                self.qiandaoUI.getChildByName("sure").active = false;
                //显示领取对应的奖励
                self.qiandaoUI.getComponent("qinadaoUI").init(reward[signDay]);
            }else{
                self.qiandaoUI.getChildByName("sure").active = true;
                self.qiandaoUI.getChildByName("lingqu").active = false;
                self.qiandaoUI.getChildByName("xuanyao").active = false;
                self.qiandaoUI.getChildByName("day_7").children[signDay].getChildByName("today").active = true;
                self.qiandaoUI.getChildByName("day_7").children[signDay].getChildByName("yiqiandao").active = true;
            }
        });    
    },

    //动态改变签到奖励的显示
    onDynamicSP:function(arr,data){
        let reward =null;
        if(data.d.signWeek>=3){
            reward =arr[data.d.signWeek-1];
            let brr=[1500,3000,6000];
            for(let i=0;i<this.shanguang.length;i++){
                this.shanguang[i].active =false;
                this.pic[i].spriteFrame =this.rewardSp[i];
                this.labText[i].string =brr[i];
            }
        }else if(data.d.signWeek==2){
            reward =arr[data.d.signWeek-1];
            let brr=[27,45,68];
            for(let i in brr){
                let tempData =TableReader.getTableRowByNum("knifeItems",brr[i]);
                cc.HQ.GameMgr.onReplaceKnife(this.pic[i],tempData);
            }
        }else{
            reward =arr[data.d.signWeek-1]; 
            let brr=[31,47,60];
            for(let i in brr){
                let tempData =TableReader.getTableRowByNum("knifeItems",brr[i]);
                cc.HQ.GameMgr.onReplaceKnife(this.pic[i],tempData);
            }
        }
        return reward;
    },

    // update (dt) {
    // },
});
