import { BEAT_GUILD, CHECK_FRAGMENT, CHECK_TOUCH, eventTarget, HAMMER, RESET_BOTTLE, RESET_FRAGMENT, SHOW_CARD_POPUP, STOP_GUILD_TAP } from "../Events";
import { Global } from "../Global";
import FragmentController from "./FragmentController";
import TheController from "./TheController";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BreakingBottleController extends cc.Component {

    @property(cc.Node)
    private hammerNode: cc.Node = null;

    private _spine: sp.Skeleton = null;
    private _skins = ["Ga", "Gai_lung", "Gay", "Bua",];
    private _countFragment = 0;

    private _fragmentsPosition: cc.Vec3[] = [];
    private _fragmentsNode: cc.Node[] = [];


    onLoad() {
        cc.director.getPhysicsManager().enabled = true;
        // cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit |
        //     cc.PhysicsManager.DrawBits.e_jointBit |
        //     cc.PhysicsManager.DrawBits.e_shapeBit;

        this._spine = this.hammerNode.getComponentInChildren(sp.Skeleton);

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);

        eventTarget.on(CHECK_FRAGMENT, this.checkFragment, this);
        eventTarget.on(HAMMER, this.onHammer, this);
        eventTarget.on(RESET_BOTTLE, this.resetBottle, this);
        eventTarget.on(BEAT_GUILD, this.beatGuild, this);

        this._fragmentsNode = this.getComponentsInChildren(FragmentController).map(item => item.node);
        this._fragmentsPosition = this._fragmentsNode.map(item => item.position.clone());
    }


    private beatGuild() {
        this.onHammer(cc.v2(100));
    }

    protected start(): void {
        this.resetHammer();
    }

    private resetBottle() {
        this._countFragment = 0;
        this._spine.setSkin(this._skins[Global.tool % this._skins.length]);

        this._fragmentsNode.forEach((node, index) => {
            node.position = this._fragmentsPosition[index];
        })
        eventTarget.emit(RESET_FRAGMENT);
    }

    private onHammer(newPos: cc.Vec2) {
        cc.Tween.stopAllByTarget(this.hammerNode);

        this._spine.setAnimation(0, "Dap4", false);

        cc.tween(this.hammerNode)
            .to(0, { active: false })
            .to(0, { active: true })
            .to(0, { position: cc.v3(newPos.x, newPos.y, 0), active: false })
            .to(0, { active: true })
            .delay(1)
            .to(0.5, { position: cc.v3(300) })
            .start();
    }

    private checkFragment(name: string) {
        this._countFragment++;
        if (this._countFragment == 8) {
            eventTarget.emit(SHOW_CARD_POPUP);
        }
    }

    private onTouchStart(event: cc.Event.EventTouch) {
        eventTarget.emit(STOP_GUILD_TAP);
        const pos = event.getLocation();
        const nodePos = this.node.convertToNodeSpaceAR(pos);

        eventTarget.emit(CHECK_TOUCH, nodePos);
        this._spine.setSkin(this._skins[Global.tool % this._skins.length]);
    }

    private onTouchEnd(event: cc.Event.EventTouch) {

    }

    private resetHammer() {
        this.hammerNode.position = cc.v3(300);
    }



}
