import { Data } from "../../../mk/data/Data";
import { RewardType } from "../GameData";

/**
 * @description 奖励数据
 * @author 邹勇 薛鸿潇
 */
export class RewardData extends Data {
    
    /** 增加的红包币 */
    public add_redbag_value: number = 0;

    private _data: Array<any> = [];
    /** 奖励列表 */
    set data(value: Array<any>) {
        this._data = value;
    }
    get data(): Array<any> {
        return this._data;
    }

    /**
     * 气泡子红包类型
     * - 1 主界面气泡、关卡气泡
     * - 2 关卡消除
     */
    subType: number = 1;

    /**
    * 奖励添加
    */
    public addReward() {
        const i_count = gData.reward.data.length;
        for (let i = 0; i < i_count; i++) {
            if (gData.reward.data[i].rewardType == RewardType.redBag) {
                gData.gameData.gameData.redMoney += gData.reward.data[i].rewardNum;
            } else if (gData.reward.data[i].rewardType == RewardType.rmb) {
                // ?
            } else if (gData.reward.data[i].rewardType == RewardType.pigRmb) {
                gData.gameData.gameData.piggyBank += gData.reward.data[i].rewardNum;
            }
        }
        gData.reward.data = [];
    }

    public flyCoinAnim() {
        if (!gData.reward.data || !gData.reward.data.length) return;
        let is_add_money = true;
        const i_count = gData.reward.data.length;
        for (let i = 0; i < i_count; i++) {
            let target_node = gData.gameData.gameStyle.icon_hb;
            if (gData.reward.data[i].rewardType == RewardType.redBag) {
                target_node = gData.gameData.gameStyle.icon_hb;
            } else if (gData.reward.data[i].rewardType == RewardType.rmb) {
                // target_node = gData.gameData.gameStyle.icon_zb;
            } else if (gData.reward.data[i].rewardType == RewardType.pigRmb) {
                target_node = gData.gameData.gameStyle.icon_zb;
            }
            const end_world_pos = target_node.parent.convertToWorldSpaceAR(target_node.getPosition());
            const end_node_pos = gData.gameData.gameStyle.node.parent.convertToNodeSpaceAR(end_world_pos);
            let start_node_pos = new cc.Vec2(0, -300);
            mk.fly.PlayCoinAnim(gData.reward.data[i].rewardType, 10, start_node_pos, end_world_pos, () => {
                is_add_money && this.addReward();
            })
        }

    }
}