// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import Common5 from "../../Platform/th/Common5";
import BagManager from "../Manager/BagManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ZhaoHuanTai extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    @property(sp.Skeleton)
    ani: sp.Skeleton = null

    @property(cc.Node)
    taizi: cc.Node = null

    @property(cc.Node)
    btn: cc.Node = null
    callback: any;

    // onLoad () {}

    start() {

    }

    init(goodid,callback) {
        this.callback = callback
        this.btn.active = false
        let taizi = this.node.getChildByName("taizi")
        for (let i = 3730; i <= 3736; i++) {
            let node = taizi.getChildByName(`${i}`)
            if (BagManager.getBagGoodNums(i) > 0 && goodid != i) {
                Common5.addUrlSprite_custom("GoodIcon/texture/icon/" + i, node.getComponent(cc.Sprite))
            } else {
                node.active = false
            }
        }

        //掉落到召唤台
        let node = taizi.getChildByName(`${goodid}`)
        node.active = true
        Common5.addUrlSprite_custom("GoodIcon/texture/icon/" + goodid, node.getComponent(cc.Sprite))
        let position = cc.v2(node.position.x, node.position.y)
        let scale = node.scale
        // node.scale = 0.9
        node.setPosition(cc.v2(0, 400))
        cc.tween(node)
            .sequence(
                cc.tween().to(0.5, { position: cc.v3(position.x, position.y, 0), scale: scale }),
                cc.tween().call(() => {
                    //集齐了
                    if (BagManager.isHaveAllGood([3730, 3731, 3732, 3733, 3734, 3735, 3736])) {
                        this.taizi.active = false
                        this.ani.node.active = true
                        this.ani.setAnimation(0, "召唤台", false)
                        this.ani.setCompleteListener(() => {
                            this.ani.setCompleteListener(null)
                            PrefabManage.loadPrefabByType(GameType.ZouCeHua, null, () => {
                            })
                            this.onTouchClose()
                        })
                    } else {
                        this.btn.active = true
                    }
                })
            )
            .start()

    }

    onTouchClose() {
        if (this.callback) {
            this.callback()
        }
        this.node.removeFromParent()
        this.node.destroy()
    }

    // update (dt) {}
}