import pg from "./tool/pg_TXZS_G_005";
import Game, { Option } from "./tool/Game_TXZS_G_005";

const { ccclass, property } = cc._decorator;
@ccclass
export default class CardItem extends cc.Component {
    public onEnable() {

    }
    protected onDestroy(): void {
    }
    private cardId: number = 0;
    private isClick = false;
    public data: Option;
    public isDispel = false;
    protected onLoad(): void {
        pg.event.on("click_card", (cardId) => {
            if (!this || this.isDispel) return;
            if (cardId == this.cardId) {
                this.showFront();
            }
        })
        pg.event.on("card_back", (cardId) => {
            if (!this || this.isDispel) return;
            if (cardId == this.cardId) {
                this.showBack();
            }
        })
        pg.event.on("disappear_card", (cardId) => {
            if (!this || this.isDispel) return;
            if (cardId == this.cardId) {
                this.disappear();
            }
        })
        this.node.on(cc.Node.EventType.TOUCH_END, this.clickCard, this);
    }

    public showCard(data) {
        this.data = data;
        this.cardId = data.id;

        let card_front = cc.find("card_front", this.node);
        let word = cc.find("word", card_front);
        let img = cc.find("img", card_front);
        if (data.type == "img") {
            pg.view.setNetImg(img, data.picUrl, { w: 230, h: null });
        } else {
            word.getComponent(cc.Label).string = data.txt;
        }

        img.active = data.type == "img";
        word.active = data.type == "txt";
    }
    private clickCard() {
        if (this.isClick || !Game.getIns().isCanClick) return;
        this.isClick = true;
        this.showFront();
        this.playLocalAudio("clickcard");
        if (Game.getIns().oClickedCard == null) {
            Game.getIns().oClickedCard = this;
            return;
        }
        if (Game.getIns().oClickedCard.node.uuid == this.node.uuid) {
            // 如果球又被点了一次,
            // 直接退出...
            return;
        }
        Game.getIns().isCanClick = false;
        this.scheduleOnce(() => {
            // 获取上一个点击的卡牌控件
            let oPrevCardComp: CardItem = Game.getIns().oClickedCard;

            if (oPrevCardComp.data.pairKey == this.data.pairKey) {
                this.scheduleOnce(() => {
                    this.playLocalAudio("right");
                    pg.event.emit("disappear_card", this.cardId);
                    pg.event.emit("disappear_card", oPrevCardComp.cardId);
                }, 0.1);
            } else {
                this.playLocalAudio("error");
                pg.event.emit("card_back", this.cardId);
                pg.event.emit("card_back", oPrevCardComp.cardId);
                Game.getIns().failCount++;
            }
            Game.getIns().oClickedCard = null;
        }, 1);

    }
    private showBack() {
        let card_back = cc.find("card_back", this.node);
        cc.tween(this.node).to(0.3, { scaleX: 0 })
            .call(() => { card_back.active = true })
            .to(0.3, { scaleX: 1 })
            .call(() => {
                this.isClick = false;
                Game.getIns().isCanClick = true;
            })
            .start();
    }
    private showFront() {
        let card_front = cc.find("card_front", this.node);
        let card_back = cc.find("card_back", this.node);
        cc.tween(this.node).to(0.3, { scaleX: 0 })
            .call(() => { card_back.active = false })
            .to(0.3, { scaleX: 1 })
            .call(() => {
                pg.audio.playAudioByUrl(this.data.audioUrl);
            })
            .start();
    }
    private disappear() {
        let card_front = cc.find("card_front", this.node);
        let light = cc.find("light", this.node);
        cc.tween(card_front).to(0.2, { scale: 0 }).call(async () => {
            light.getComponent(dragonBones.ArmatureDisplay).playAnimation("newAnimation", 1);
            light.active = true;
            this.scheduleOnce(() => {
                Game.getIns().isCanClick = true;
                light.active = false;
                this.isDispel = true;
                Game.getIns().dispelCount -= 1;

                if (Game.getIns().dispelCount <= 0) {
                    pg.event.emit("check_next_question");
                }
            }, 1)
        })
            .start();
    }


    playLocalAudio(audioName) {
        const audio = cc.find(`Canvas/res/audio/${audioName}`).getComponent(cc.AudioSource);
        return new Promise((resolve, reject) => {
            const id = cc.audioEngine.playEffect(audio.clip, false);
            cc.audioEngine.setFinishCallback(id, () => { resolve(id); });
        })
    }
}