/**
 * 代码描述
 */

import { Audio } from "../../common/src/Audio";
import { GameDataCenter } from "../../common/src/GameDataCenter";
import { UIManager } from "../../common/src/UIManager";
import { Utils } from "../../common/src/Utils";


const { ccclass, property } = cc._decorator;

@ccclass
export class RewardedPanel extends cc.Component {

    @property({ type: cc.Prefab, tooltip: '奖励预制体' })
    itemPrefab: cc.Prefab = null;

    @property({ type: cc.Node, tooltip: '物品节点' })
    item: cc.Node = null;

    @property({ type: [cc.SpriteFrame], tooltip: '奖励集合\n0.炸弹\n1.激光\n2.七色\n3.闪电\n4.金币' })
    itemSpriteFrame: cc.SpriteFrame[] = [];


    private type: number[] = [];
    private num: number[] = [];
    private ui: string[] = ['special1', 'special2', 'special3', 'special4', 'coin'];

    onEnable() {
        //初始化面板
        this.node.getChildByName('panel').getChildByName('closeButton').on('click', this.onClose, this);
        this.node.getChildByName('panel').getChildByName('doubleGet').on('click', this.onDoubleGet, this);

        for (let i = 0; i < GameDataCenter.rewarded.length; i++) {
            let array = GameDataCenter.rewarded[i].split(':');
            let type: number = +array[0];
            let num: number = +array[1];
            this.type.push(type);
            this.num.push(num);
            let node = cc.instantiate(this.itemPrefab);
            node.parent = this.item;
            node.getComponent(cc.Sprite).spriteFrame = this.itemSpriteFrame[type];
            node.children[0].getComponent(cc.Label).string = `:${num}`;
            node.scale = 0;
            cc.tween(node)
                .to(0.3, { scale: 1 })
                .start();
            cc.systemEvent.emit('updateUI', this.ui[type], num);
        }
        //背景动画
        cc.tween(this.node.getChildByName('panel').getChildByName('bg'))
            .by(0.05, { angle: 1 })
            .repeatForever()
            .start();
        //面板缩放
        Utils.openPanel(this.node.getChildByName('panel'));
        //按钮跳动
        Utils.btnTween(this.node.getChildByName('panel').getChildByName('doubleGet'));
    }

    /**关闭 */
    private onClose(): void {
        Audio.playSoundByPath('rewarded:res/snd/click');
        UIManager.close('rewarded:RewardedPanel', true);
    }

    /**双倍领取 */
    private onDoubleGet(): void {
        Audio.playSoundByPath('rewarded:res/snd/click');

        for (let i = 0; i < this.type.length; i++) {
            cc.systemEvent.emit('updateUI', this.ui[this.type[i]], this.num[i]);
        }
        this.onClose();
    }
}
