import Userdata from "./Userdata";
import Global from "./Global";
import SoundManager from "./SoundManager";
import { Sdk } from "./Sdk/Sdk";
import { SdkToutiao } from "./Sdk/SdkToutiao";

const { ccclass, property } = cc._decorator;

@ccclass
export default class EveryDayLayer extends cc.Component {
    @property([cc.Sprite])
    gotitSprites: cc.Sprite[] = [];

    @property(cc.Button)
    gotButton: cc.Button = null;

    @property(cc.Button)
    doubleButton: cc.Button = null;

    closeFunc: Function = null;

    //每日奖励数据
    dailyData: any[] = [
        {
            gem: 100
        },
        {
            gem: 150
        },
        {
            gem: 150
        },
        {
            gem: 200
        },
        {
            gem: 200
        },
        {
            gem: 250
        },
        {
            gem: 300
        }
    ];

    onEnable() {
        this.updateSelectDay();
        this.updateButtonShow();
    }

    setListener(closeFunction: Function) {
        this.closeFunc = closeFunction;
    }

    updateSelectDay() {
        let currDay = 0;
        let isGot: boolean = false;
        if (Userdata.DailyDate != Global.getTodayString()) {//今天未领取
            if (Userdata.DailyDay >= 7) {
                currDay = 0;
            } else {
                currDay = Userdata.DailyDay;
            }
        } else {//已领取回到上一天
            isGot = true;
            currDay = Userdata.DailyDay - 1;
            if (currDay < 0) {
                currDay = 6;
            }
        }

        cc.log("currDay = " + currDay);

        for (let i = 0; i < this.gotitSprites.length; i++) {
            if (i <= currDay) {//已经领取/当天
                if (i == currDay && !isGot) {//未领取，不显示标记
                    this.gotitSprites[i].node.active = false;
                } else {
                    this.gotitSprites[i].node.active = true;
                }
            } else {
                this.gotitSprites[i].node.active = false;
            }
        }
    }


    updateButtonShow() {
        //非同一天，可以领取
        if (Userdata.DailyDate != Global.getTodayString()) {
            this.gotButton.interactable = true;
            this.doubleButton.interactable = true;
        } else {//已领取
            this.gotButton.interactable = false;
            this.doubleButton.interactable = false;
            //this.doubleLabel.string = "已领取";

            cc.log("已领取");
        }
    }

    onClickClose() {
        SoundManager.playButton();
        this.hide();
    }

    hide() {
        if (this.closeFunc) {
            this.closeFunc();
        }

        this.node.removeFromParent();
        this.node.destroy();

        Userdata.saveUserdata();
    }

    claimReward(isDouble: boolean = false) {
        if (Userdata.DailyDay >= 7) {
            Userdata.DailyDay = 0;
        }

        let data = this.dailyData[Userdata.DailyDay];
        if (data['gem']) {
            let num = isDouble ? data.gem * 3 : data.gem;
            Userdata.addGem(num);
            Global.showTip("获得 " + num + " 钻石!");
        }

        Userdata.DailyDate = Global.getTodayString();
        Userdata.DailyDay += 1;

        Userdata.saveUserdata();
        this.gotButton.interactable = false;
        this.doubleButton.interactable = false;

        this.updateSelectDay();
        this.updateButtonShow();
    }

    onClickClaim() {
        SoundManager.playButton();
        this.claimReward();

    }

    onClickDouble() {
        SoundManager.playButton();

        if (Sdk.isToutiao){
            SdkToutiao.showRewardVideoAd(()=>{
                this.claimReward(true);
            }, ()=>{
                Global.showTip('广告展示失败,请稍后重试!');
            });
        }else{
            Sdk.ins.showVideoAd((res) => {
                if (res) {
                    this.claimReward(true);
                } else {
                    Global.showTip('No Ads ready!');
                }
            });
        }

        
    }
}
