import Game from "./Game";
import pg from "./pg";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    // LIFE-CYCLE CALLBACKS:
    private mask: cc.Node;
    private penguin: cc.Node;
    private penguin_1: cc.Node;
    private penguin_2: cc.Node;
    private penguin_1_wrong: cc.Node;
    private penguin_2_wrong: cc.Node;
    private panel_text: cc.Node;
    private txt: cc.Node;
    private right: cc.Node;
    private img: cc.Node;

    private robotScore: number;
    private playerScore: number;
    onLoad() {
        this.mask = pg.view.find(this, "mask");
        this.penguin = pg.view.find(this.mask, "penguin");
        this.penguin_1 = pg.view.find(this.penguin, "penguin_1");
        this.penguin_2 = pg.view.find(this.penguin, "penguin_2");
        this.penguin_1_wrong = pg.view.find(this.penguin, "penguin_1_wrong");
        this.penguin_2_wrong = pg.view.find(this.penguin, "penguin_2_wrong");
        this.panel_text = pg.view.find(this.penguin, "panel_text");
        this.txt = pg.view.find(this.panel_text, "txt");
        this.right = pg.view.find(this.panel_text, "right");
        this.img = pg.view.find(this.panel_text, "img");
        let btn_touch = pg.view.find(this, "btn_touch");
        pg.view.touchOn(btn_touch, this.onPlayerText, this);
        this.robotScore = Game.getIns().robotScore
        this.playerScore = Game.getIns().playerScore;
    }

    private data: any;
    private touched: boolean;
    showAni(data) {
        this.touched = false;
        this.data = data;
        this.penguin.y = -100;
        this.panel_text.opacity = 0;

        pg.view.visible(this.panel_text, true);
        pg.view.visible(this.penguin_1, true);
        pg.view.visible(this.penguin_2, false);
        pg.view.visible(this.penguin_1_wrong, false);
        pg.view.visible(this.penguin_2_wrong, false);

        if (data.type == "text") {
            pg.view.setString(this.txt, data.text);
            pg.view.visible(this.img, false)
            pg.view.visible(this.txt, true)
        } else {
            pg.view.setNetImg(this.img, data.pic, { w: 300, h: 100 });
            pg.view.visible(this.img, true)
            pg.view.visible(this.txt, false)
        }
        pg.hw.playLocalAudio("appear");
        // Game.getIns().pause
        // cc.tween(this.penguin).to(0.4, { y: 190 }).call(() => {
        //     if (Game.getIns().pause) {

        //     } else {
        //         cc.tween(this.penguin).delay(2).call(() => {
        //             this.touched = true;
        //             cc.tween(this.penguin).to(0.4, { y: -100 }).delay(0.1).call(() => {
        //                 this.node.parent = null;
        //             }).start();
        //         }).start();
        //     }
        // }).start();
        cc.tween(this.penguin).to(0.4, { y: 190 }).delay(2).call(() => {
            this.touched = true;
        }).to(0.4, { y: -100 }).delay(0.1).call(() => {
            this.node.parent = null;
        }).start();
        cc.tween(this.panel_text).to(0.2, { opacity: 255 }).delay(2.4).to(0.2, { opacity: 0 }).start();

        let wait = Game.getIns().robotLevel * 0.65;//
        if (data.right == "1") {
            let time = 0.4 + wait + Math.random() * 2;
            this.scheduleOnce(() => {
                if (this.touched) return;
                if (Game.getIns().pause) return;
                if (this.robotScore != Game.getIns().robotScore) return;
                if (this.playerScore != Game.getIns().playerScore) return;
                Game.getIns().isRobot = true;
                this.onTouchText();
            }, time);
        }
    }
    showSuccessAni() {
        cc.tween(this.penguin).to(0.4, { y: -100 }).delay(0.1).call(() => {
            this.node.parent = null;
        }).start();
        cc.tween(this.panel_text).delay(0.2).to(0.2, { opacity: 0 }).start();
    }
    showErrorAni() {
        pg.hw.playLocalAudio("wrong");
        pg.view.visible(this.penguin_1, false);
        pg.view.visible(this.penguin_2, false);
        pg.view.visible(this.penguin_1_wrong, true);
        pg.view.visible(this.penguin_2_wrong, false);
        this.scheduleOnce(() => {
            pg.view.visible(this.penguin_1, true);
            pg.view.visible(this.penguin_2, false);
            pg.view.visible(this.penguin_1_wrong, false);
            pg.view.visible(this.penguin_2_wrong, false);
            cc.tween(this.penguin).to(0.4, { y: -100 }).delay(0.1).call(() => {
                this.node.parent = null;
            }).start();
            cc.tween(this.panel_text).delay(0.2).to(0.2, { opacity: 0 }).start();
        }, 1)
    }
    onPlayerText() {
        if (this.touched) return;
        if (Game.getIns().pause) return;
        Game.getIns().isRobot = false;
        this.onTouchText();
    }
    onTouchText() {
        if (Game.getIns().isOver) return;
        if (Game.getIns().pause) return;
        if (this.touched) return;
        this.touched = true;
        let data = this.data;
        if (Game.getIns().isRobot) {
            if (data.right == "1") {
                Game.getIns().pause = true;
                pg.view.visible(this.right, true);
                Game.getIns().audioing = true;
                pg.hw.playLocalAudio("right").then(() => {
                    Game.getIns().audioing = false;
                })
                pg.view.playDBAnimation(this.right, "newAnimation", 1);
                pg.event.emit("item_touch_right", data);
            }
        } else {
            cc.Tween.stopAllByTarget(this.penguin);
            cc.Tween.stopAllByTarget(this.panel_text);
            if (data.right == "1") {
                Game.getIns().pause = true;
                //正确
                pg.view.visible(this.right, true);
                Game.getIns().audioing = true;
                pg.hw.playLocalAudio("right").then(() => {
                    Game.getIns().audioing = false;
                })
                pg.view.playDBAnimation(this.right, "newAnimation", 1);
                data.cbSuccess = () => {
                    //执行
                    this.showSuccessAni();
                }
                data.cbFailed = () => {
                    this.showErrorAni();
                }
                pg.event.emit("item_touch_right", data);
            } else {
                //错误
                // pg.event.emit("item_touch_error", data);
                this.showErrorAni();
            }
        }
    }
}
