
import { _decorator, Component, Node } from 'cc';
import GameConst from '../../../core/common/GameConst';
import BasePanel from '../../../core/component/BasePanel';
import Data from '../../../core/manager/Data';
import Utils from '../../../core/utils/Utils';
import platformSystem from 'db://assets/platform/platformSystem';
import audioManager from 'db://assets/manager/audioManager';
import { constants } from 'db://assets/data/constants';
const { ccclass, property } = _decorator;

@ccclass('BuyLifeLayer')
export class BuyLifeLayer extends BasePanel {

    update() {
        this.SetText("lbl_life", Data.user.life + "")
        if (Data.user.life >= GameConst.MAX_LIFE) {
            this.SetText("lbl_life_time", "已满")
        }
        else {
            if (Data.user.life_cost_time == 0) {
                Data.user.life_cost_time = Date.now();
            }

            let nLeft = (Date.now() - Data.user.life_cost_time) / 1000;
            let addLife = Math.floor(nLeft / 60 / GameConst.MAX_LIFE_RECOVERY_TIME);
            if (addLife > 0)
                Data.user.life += addLife;
            nLeft -= addLife * GameConst.MAX_LIFE_RECOVERY_TIME * 60;
            this.SetText("lbl_life_time", Utils.getTimeStrByS(GameConst.MAX_LIFE_RECOVERY_TIME * 60 - nLeft))
        }
    }

    star() {
        platformSystem.platform.showInterstitialAd();
    }

    onBtnClicked(event: any, customEventData: any) {
        audioManager.playOneShot(constants.audios.btnclick);
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_close":
                this.close();
                break;
            case "btn_ad":
                platformSystem.platform.showRewardVideo((b) => {
                    if (b) {
                        Data.user.life += 1;
                        Data.save();
                        this.close();
                    }
                })
                break;
        }
    }
}