
import { _decorator, Component, Node, tween, v3, Label, Vec3 } from 'cc';
import { BOX_TYPE, ITEM_TYPE, TItem } from '../../../core/common/GameConst';
import BasePanel from '../../../core/component/BasePanel';
import Data from '../../../core/manager/Data';
import Utils from '../../../core/utils/Utils';
import { constants } from 'db://assets/data/constants';
import audioManager from 'db://assets/manager/audioManager';
const { ccclass, property } = _decorator;

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

    start() {
        this.GetGameObject("item").active = false;
        this.GetGameObject("item").setPosition(Vec3.ZERO);
        this.GetGameObject("box").setPosition(Vec3.ZERO);
    }

    //最多奖励4个
    private list: TItem[];
    private boxType;
    setInfo(boxType: BOX_TYPE, list: TItem[]) {
        this.boxType = boxType;
        this.list = list;
        for (var i = 0; i < 4; ++i) {
            let tmp = this.GetGameObject("item").children[i];
            if (list[i]) {
                tmp.active = true;
                tmp.getChildByName("Time").active = list[i].type == ITEM_TYPE.Time;
                tmp.getChildByName("Frozen").active = list[i].type == ITEM_TYPE.Frozen;
                tmp.getChildByName("Hint").active = list[i].type == ITEM_TYPE.Hint;
                tmp.getChildByName("Magnet").active = list[i].type == ITEM_TYPE.Magnet;
                tmp.getChildByName("Coin").active = list[i].type == ITEM_TYPE.Coin;
                tmp.getChildByName("Label").getComponent(Label).string = "+" + list[i].num;
            }
            else {
                tmp.active = false;
            }
        }

        this.GetGameObject("chest-level-reward-close").active = boxType == BOX_TYPE.LEVEL;
        this.GetGameObject("chest-team-reward-close").active = boxType == BOX_TYPE.TEAM;
        this.GetGameObject("chest-star-reward-close").active = boxType == BOX_TYPE.STAR;

        this.GetGameObject("chest-level-reward-open").active = false
        this.GetGameObject("chest-star-reward-open").active = false
        this.GetGameObject("chest-team-reward-open").active = false
    }

    private open = false;
    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_open":
                if (this.open) {
                    this.list.map(a => {
                        Data.user.addItem(a.type, a.num);
                        if (a.type == ITEM_TYPE.Coin) {
                            Utils.flyAnim(ITEM_TYPE.Coin, this.GetNode("item"), this.GetNodeFromCanvas("coin-icon"), 5, 50, (b) => {
                            });
                        }
                    })

                    if (this.boxType == BOX_TYPE.LEVEL) {
                        Data.user.lvboxDrawInfo[Math.floor(Data.user.lv / 10)] = 1;
                    }
                    else if (this.boxType == BOX_TYPE.STAR) {
                        Data.user.starboxDrawInfo[Math.floor(Data.user.star / 1000)] = 1;
                    }
                    Data.save();
                    this.close();
                }
                else {
                    this.open = true;
                    this.GetGameObject("item").active = true;
                    tween(this.GetGameObject("item")).by(0.3, { worldPosition: v3(0, 90, 0) }).start();
                    tween(this.GetGameObject("box")).by(0.3, { worldPosition: v3(0, -60, 0) }).start();
                    this.GetGameObject("chest-level-reward-close").active = false
                    this.GetGameObject("chest-team-reward-close").active = false
                    this.GetGameObject("chest-star-reward-close").active = false

                    this.GetGameObject("chest-level-reward-open").active = this.boxType == BOX_TYPE.LEVEL;
                    this.GetGameObject("chest-team-reward-open").active = this.boxType == BOX_TYPE.TEAM;
                    this.GetGameObject("chest-star-reward-open").active = this.boxType == BOX_TYPE.STAR;
                    this.SetText("lbl_btn", "领取")
                }

                break;
        }
    }

}
