
import { _decorator, Component, Node, tween } from 'cc';
import BasePanel from '../../../core/component/BasePanel';
import WindowManager from '../../../core/manager/WindowManager';
import { LevelClearLayer } from './LevelClearLayer';
import platformSystem from 'db://assets/platform/platformSystem';
import { constants } from 'db://assets/data/constants';
import audioManager from 'db://assets/manager/audioManager';
const { ccclass, property } = _decorator;

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

    indicator: Node = null;
    start() {
        this.indicator = this.GetGameObject("star-gamification-indicator");
        audioManager.playOneShot(constants.audios.pass);
    }

    star = 0;
    setStar(s) {
        this.star = s;
        this.SetText("lbl_star", s + "");
    }

    dir = 1;
    beisu = 1;
    update() {
        if (this.pause) return;
        let pos = this.indicator.getPosition();
        pos.x += this.dir * 10;
        if (pos.x > 190) this.dir = -1;
        if (pos.x < -190) this.dir = 1;
        this.indicator.setPosition(pos);

        let x = this.indicator.getPosition().x;
        if (x >= -190 && x < -145) {
            this.beisu = 2;
        }
        else if (x >= -145 && x < -98) {
            this.beisu = 3;
        }
        else if (x >= -98 && x < -36) {
            this.beisu = 4;
        }
        else if (x >= -36 && x < 36) {
            this.beisu = 5;
        }
        else if (x >= 36 && x < 98) {
            this.beisu = 4;
        }
        else if (x >= 98 && x < 145) {
            this.beisu = 3;
        }
        else if (x >= 145 && x <= 190) {
            this.beisu = 2;
        }
        this.SetText("lbl_beisu", "x" + this.beisu);
    }

    pause = false;
    onBtnClicked(event, customEventData) {
        audioManager.playOneShot(constants.audios.btnclick);
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_get":
                this.pause = true;
                WindowManager.ins.open("LevelClearLayer").then((com: LevelClearLayer) => {
                    com.setAddStar(this.star);
                })
                this.close();
                break;
            case "btn_ad":
                this.pause = true;
                tween(this.node).delay(1).call(() => {
                    platformSystem.platform.showRewardVideo((b) => {
                        if (b) {
                            WindowManager.ins.open("LevelClearLayer").then((com: LevelClearLayer) => {
                                com.setAddStar(this.star * this.beisu);
                            })
                            this.close();
                        }
                    })
                }).start()
                break;
        }
    }
}
