
cc.Class({
    extends: cc.Component,

    properties: {
        GotBTN:cc.Node,
    },
    start()
    {
        // this.EveryDaySignView = this.getNode("EveryDaySignView");
        this.EveryDaySignContainer = this.node.parent.parent;
        this.EveryDaySignView = this.node.parent;
        this.ManageUI = cc.find("GameNode/ManageUI");


        //每日签到
        let myDate = new Date();
        //'2019/05/01'
        let Today = myDate.toLocaleDateString();
        let LastTimeEveryDayRewardsDate = GlobalD.GameData.GetLastTimeEveryDayRewardsDate();
        // let Today = '2019/05/03';
        // let LastTimeEveryDayRewardsDate = '2019/05/04';
        // cc.log('Today='+Today);
        // cc.log('LastTimeEveryDayRewardsDate='+LastTimeEveryDayRewardsDate);

        var dateStart = new Date(Today);
        var dateEnd = new Date(LastTimeEveryDayRewardsDate);
        var difValue = ((dateEnd - dateStart) / (1000 * 60 * 60 * 24));
        // cc.log('difValue='+difValue);
        // cc.log('LastTimeEveryDayRewardsDate='+LastTimeEveryDayRewardsDate);
        // if(difValue == 0 && LastTimeEveryDayRewardsDate!='0000/00/00')
        // {
        //     this.GotBTN.active=false;
        // }
        // else
        // {
        //     //过了一天
        //     GlobalD.GameData.SetLotteryTimes(10);
        // }
        console.log("today",Today);
        console.log("LastTimeEveryDayRewardsDate",LastTimeEveryDayRewardsDate);


        if(Today == LastTimeEveryDayRewardsDate && LastTimeEveryDayRewardsDate!='0000/00/00')
        {
            this.GotBTN.active=false;
        }
        else
        {
            //过了一天
            // GlobalD.GameData.SetLotteryTimes(10);
        }

        //Show Got Rewards Item to BG Black
        let EveryDayRewardsArray = GlobalD.GameData.GetEveryDayRewardsArray();
        let GotRewarsNum = 0;
        // cc.log('111111111111='+EveryDayRewardsArray);
        // return;
        for(let i=0;i<EveryDayRewardsArray.length;i++)
        {
            if(EveryDayRewardsArray[i]==1)
            {
                GotRewarsNum++;
            }
        }
        if(GotRewarsNum==7)
        {
            GlobalD.GameData.InitEveryDayRewardsArrayFirst();
        }
        for(let i=0;i<EveryDayRewardsArray.length;i++)
        {
            if(EveryDayRewardsArray[i]==1)
            {
                let TodayNode = null;
                //0 is monday
                switch (i) {
                    case 0:
                        TodayNode= this.node.getChildByName('0');
                        break;
                    case 1:
                        TodayNode= this.node.getChildByName('1');
                        break;
                    case 2:
                        TodayNode= this.node.getChildByName('2');
                        break;
                    case 3:
                        TodayNode= this.node.getChildByName('3');
                        break;
                    case 4:
                        TodayNode= this.node.getChildByName('4');
                        break;
                    case 5:
                        TodayNode= this.node.getChildByName('5');
                        break;
                    case 6:
                        TodayNode= this.node.getChildByName('6');
                        break;
                }

                let Achieved = TodayNode.getChildByName('Achieved');
                Achieved.active = true;
            }
        }
    },
    //点击了 点击领取
    //每日签到 领取按钮
    GetRewards()
    {
        let EveryDayRewardsArray = GlobalD.GameData.GetEveryDayRewardsArray();
        for(let i=0;i<EveryDayRewardsArray.length;i++)
        {
            if(EveryDayRewardsArray[i]==0)
            {
                let TodayNode = null;
                //0 is monday
                switch (i) {
                    case 0:
                        TodayNode= this.node.getChildByName('0');
                        break;
                    case 1:
                        TodayNode= this.node.getChildByName('1');
                        break;
                    case 2:
                        TodayNode= this.node.getChildByName('2');
                        break;
                    case 3:
                        TodayNode= this.node.getChildByName('3');
                        break;
                    case 4:
                        TodayNode= this.node.getChildByName('4');
                        break;
                    case 5:
                        TodayNode= this.node.getChildByName('5');
                        break;
                    case 6:
                        TodayNode= this.node.getChildByName('6');
                        break;
                }
                this.TodayNode = TodayNode;
                let Achieved = TodayNode.getChildByName('Achieved');
                Achieved.active = true;
                EveryDayRewardsArray[i] = 1;
                GlobalD.GameData.SetEveryDayRewardsArray(EveryDayRewardsArray);

                let myDate = new Date();
                let Today = myDate.toLocaleDateString();
                GlobalD.GameData.SetLastTimeEveryDayRewardsDate(Today);

                this.GotBTN.active=false;

                this.ShowRewards(i);
                return;
            }
        }
    },
    //点了签到之后 签到按钮
    ShowRewards(Index)
    {
        UtilsPrefabs
            .init(this.EveryDaySignContainer)
            .addPrefabs(UtilsPrefabs.PrefabsName[1],null,function (node) {
                node.y = -200;
                node.x = -20;
                var GivePrize = UtilsPrefabs.getNode("GivePrize",node);
                //转
                var Sunshine = UtilsPrefabs.getNode("Sunshine",GivePrize);
                //物品
                var Rewards = UtilsPrefabs.getNode("Rewards",GivePrize);
                //关闭按钮
                var close = UtilsPrefabs.getNode("close",node);

                Rewards.getComponent(cc.Sprite).spriteFrame = this.getNode("ItemSprite",this.TodayNode).getComponent(cc.Sprite).spriteFrame
                Rewards.scale = 2;
                console.log("进来的说");

                Sunshine.scaleX = 0;
                Sunshine.scaleY = 0;
                Rewards.scaleX = 0;
                Rewards.scaleY = 0;
                Sunshine.stopAllActions();
                Rewards.stopAllActions();

                var sc2 = cc.scaleTo(0.5,1.2,1.2);
                Sunshine.runAction(sc2);
                var sc3 = cc.scaleTo(0.5,1.3,1.3);
                Rewards.runAction(sc3);


                var repeat = cc.repeatForever(cc.rotateBy(5.0, 360));
                Sunshine.runAction(repeat);



                this.EveryDaySignView.active=false;
                UtilsPrefabs.setOn(close,function () {


                    this.ManageUI.getComponent("ManageUI").onButtonEvent_EveryDay_Close();
                    UtilsPrefabs.setOff(close);
                    UtilsPrefabs.removePrefabs(this.EveryDaySignContainer,node.name);
                    this.next();
                    this.startsharebtn();
                }.bind(this));


            }.bind(this));

        switch (Index) {
            case 0:
                GlobalD.GameData.PlusGolden(2000);
                break;
            case 1:
                GlobalD.GameData.PlusDiamond(500);
                break;
            case 2:
                GlobalD.GameData.PlusGolden(5000);
                break;
            case 3:
                // let Golden = this.GetRandomRange(50,200);
                // GlobalD.GameData.PlusGolden(Golden);
                GlobalD.GameData.PlusDiamond(1000);
                break;
            case 4:
                GlobalD.GameData.PlusGolden(8000);
                break;
            case 5:
                GlobalD.GameData.PlusDiamond(1500);
                break;
            case 6:
                //随机获得钻石x1   或者金币50-300  或者建筑物一个
                GlobalD.GameData.PlusGolden(10000);
                GlobalD.GameData.PlusDiamond(4000);
                break;
        }
    },
    isTask : function(){
        if (task.TaskIconCountClick == 4){
            task.addTaskIconCountClick();
            task.removeTaskNodes();
        }
    },
    startsharebtn : function(){
        UtilsPrefabs
            .init(this.EveryDaySignContainer)
            .addPrefabs(UtilsPrefabs.sharePrefab[1],null,function (node) {
                node.y = -200;
                node.x = -20;
                var gosharebtn = UtilsPrefabs.getNode("gosharebtn",node);
                // var img1 = UtilsPrefabs.getNode("img1",node);
                // var NewLabel1 = UtilsPrefabs.getNode("New Label",img1);
                // var img2 = UtilsPrefabs.getNode("img2",node);
                // var NewLabel2 = UtilsPrefabs.getNode("New Label",img2);
                // NewLabel1.getComponent(cc.Label).string = 'x'+GlobalD.GameData.signInGive[0];
                // NewLabel2.getComponent(cc.Label).string = 'x'+GlobalD.GameData.signInGive[1];

                var close = UtilsPrefabs.getNode("close",node);
                UtilsPrefabs.setOn(close,function () {
                    this.isTask();
                    UtilsPrefabs.removePrefabs(this.EveryDaySignContainer,node.name);

                }.bind(this));

                UtilsPrefabs.setOn(gosharebtn,function () {
                    // console.log("点击分享按钮");
                    UtilsWX.sharebtn(function () {
                        UtilsPrefabs.setOff(gosharebtn);

                        GlobalD.GameData.PlusGolden(GlobalD.GameData.signInGive[0]);
                        GlobalD.GameData.PlusDiamond(GlobalD.GameData.signInGive[1]);
                        this.isTask();
                    }.bind(this));
                    UtilsPrefabs.removePrefabs(this.EveryDaySignContainer,node.name);
                }.bind(this))
            }.bind(this));
    },

    next : function(){
        if (task.taskCursor == 5) {
            var Canvas = cc.find("Canvas/UICamera");
            task.addTaskTips(Canvas,task.taskPrefab[5],0,0,task.taskCursorName[0],function (nodePrefabs) {
                UtilsPrefabs.setOn(nodePrefabs.getChildByName("SureBtn"),function () {
                    task.removeTaskNode(task.taskCursorName[0]);
                    if (task.taskCursor == 5){
                        task.addTaskCount();
                        var personnel = cc.find("Canvas/UICamera/BelowTheMask/personnel");
                        //转盘提示
                        task.addTaskTips(personnel,task.taskPrefab[1],0,0,task.taskCursorName[1],function (nodePrefabs1) {
                            nodePrefabs1.x = -50;
                            nodePrefabs1.y = -50;
                        }.bind(this));
                    }

                }.bind(this))
            }.bind(this));
        }
    },

    GetRandomRange(n, m){
        var random = Math.floor(Math.random()*(m-n+1)+n);
        return random;
    },

    getNode: function (name, parent) {
        if (parent == null) {
            return this.node.getChildByName(name);
        } else {
            return parent.getChildByName(name);
        }

    },

});
