
import { _decorator, Component, Node, director } from 'cc';
import BasePanel from '../../../core/component/BasePanel';
import Data from '../../../core/manager/Data';
import WindowManager from '../../../core/manager/WindowManager';
import { Main } from '../Main';
import levelsData from 'db://assets/data/levelsData';
import audioManager from 'db://assets/manager/audioManager';
import { constants } from 'db://assets/data/constants';
const { ccclass, property } = _decorator;

@ccclass('LevelClearLayer')
export class LevelClearLayer extends BasePanel {
    start() {
        this.SetText("lbl_lvv", "关卡" + Data.user.lv + "\n通过");
        Data.user.lv++;
      
    }

    onBtnClicked(event: any, customEventData: any) {
        if (!this.ok) return;
        audioManager.playOneShot(constants.audios.btnclick);
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_hall":
                Data.user.star += this.real_add_star;
                Data.save();
                this.close();
                Main.Instance().backHall();
                break;
            case "btn_next":
                Data.user.star += this.real_add_star;
                Data.save();
                this.close();
                Main.Instance().play();
                break;
        }
    }

    addstar = 0;
    real_add_star = 0;
    setAddStar(addstar: number) {
        this.real_add_star = addstar;
        this.addstar = addstar;
        this.cur = Data.user.star % 100;

        this.SetProgressBar("ProgressBar", this.cur / 100);
        this.SetText("lbl_pro", (Math.floor(this.cur / 100) * 100) + "%")
    }

    cur = 0;
    cdt = 0;
    ok = false;
    update(dt: number) {
        if (this.ok) return;
        this.cdt += dt;
        if (this.cdt < 0.01) return
        this.cdt = 0;

        if (this.addstar > 0) {
            this.addstar--;
            this.cur++;
            if (this.cur > 100) {
                this.cur = 100;
                this.ok = true;
            }
        }
        else {
            this.ok = true;
        }

        let OpenItems:Array<any> = levelsData.getNormModelDatas();
        if (this.ok && this.cur >= 100 && Data.user.openIndex < OpenItems.length - 1) {
            //解锁新物品
            WindowManager.ins.open("NewPackOpenedLayer", Data.user.openIndex);
        }

        this.SetProgressBar("ProgressBar", this.cur / 100);
        this.SetText("lbl_pro", this.cur + "%")
    }
}
