/**
 * 文件功能描述
 */

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

const { ccclass, property } = cc._decorator;

@ccclass
export class Bubble extends cc.Component {

    @property({ type: cc.Prefab, tooltip: '泡泡破碎预制体' })
    bubble: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: '炸弹破碎预制体' })
    bomb_gj: cc.Prefab = null;

    @property({ type: cc.Prefab, tooltip: '七色破碎预制体' })
    rainbow_gj: cc.Prefab = null;

    @property({ type: [cc.Prefab], tooltip: '普通泡泡破碎特效集合\n0.紫色\n1.绿色\n2.蓝色\n3.黄色\n4.红色\n石头' })
    bubbleEffect: cc.Prefab[] = [];

    public i: number;
    public j: number;
    public color: number;
    public isVisited: boolean;
    public isLinked: boolean;

    /**初始化 */
    public init(i: number, j: number, color: number): void {
        this.i = i;
        this.j = j;
        this.color = color;
        this.isVisited = false;
        this.isLinked = false;
    }

    /** 消除动画 */
    public playDeathAnimation(combo: number, type: number = 0): void {
        this.node.children[0].removeComponent(cc.PhysicsCircleCollider);
        cc.tween(this.node)
            .call(() => {
                GameDataCenter.bubbles[this.i][this.j] = undefined;
            })
            // .to(0.1, { scale: 0, opacity: 0 })
            .call(() => {
                Audio.playSoundByPath('game:res/snd/remove');
                this.node.removeComponent(cc.Sprite);
                this.node.scale = 1;
                this.node.opacity = 255;
                this.effect(type);
                let node = this.node.getChildByName('score');
                node.getComponent(cc.Label).string = `${combo * 10}`
                node.active = true;
                cc.tween(node)
                    .by(0.5, { y: 100 })
                    .removeSelf()
                    .start();
            })
            .delay(2)
            .removeSelf()
            .start();
    }

    /** 下落动画 */
    public playDownAnimation(combo: number, endPos: cc.Vec2): void {
        this.node.children[0].removeComponent(cc.PhysicsCircleCollider);
        let random = Math.random() * 250 - 125;
        let pos = this.node.position;
        cc.tween(this.node)
            .call(() => {
                GameDataCenter.bubbles[this.i][this.j] = undefined;
            })
            .bezierTo(0.5, cc.v2(pos.x, pos.y), cc.v2(pos.x + random, pos.y + 300), cc.v2(pos.x + random, endPos.y + 100))
            // .to(0.1, { scale: 0, opacity: 0 })
            .call(() => {
                if (this.color == 6) {
                    Audio.playSoundByPath('game:res/snd/ice');
                }
                else if (this.color == 7) {
                    Audio.playSoundByPath('game:res/snd/brick');
                }
                else {
                    Audio.playSoundByPath('game:res/snd/fall');
                }
                this.node.removeComponent(cc.Sprite);
                this.node.scale = 1;
                this.node.opacity = 255;
                this.effect(0);
                let node = this.node.getChildByName('score');
                node.getComponent(cc.Label).string = `${combo * 10}`
                node.active = true;
                cc.tween(node)
                    .by(0.5, { y: 100 })
                    .removeSelf()
                    .start();
            })
            .delay(2)
            .removeSelf()
            .start();
    }

    /**爆炸特效 */
    private effect(type: number): void {
        let node: cc.Node = null;
        switch (type) {
            case 0:
                switch (this.color) {
                    case 1:
                        node = cc.instantiate(this.bubbleEffect[0]);
                        break;
                    case 2:
                        node = cc.instantiate(this.bubbleEffect[1]);
                        break;
                    case 3:
                        node = cc.instantiate(this.bubbleEffect[2]);
                        break;
                    case 4:
                        node = cc.instantiate(this.bubbleEffect[3]);
                        break;
                    case 5:
                        node = cc.instantiate(this.bubbleEffect[4]);
                        break;
                    default:
                        node = cc.instantiate(this.bubbleEffect[5]);
                        break;
                }
                break;
            case 1:
                node = cc.instantiate(this.bomb_gj);
                break;
            case 2:
                return;
            case 3:
                node = cc.instantiate(this.rainbow_gj);
                break;
            case 4:
                return;
        }
        node.parent = this.node;
    }
}
