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;
    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");
        pg.view.touchOn(this.panel_text, this.onPlayerText, this);
    }

    start() {
        // let data = {
        //     text: "info"
        // }
        // this.showAni(data);
    }
    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");
        cc.tween(this.penguin).to(0.4, { y: 190 }).delay(2).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 point = 6 - Game.getIns().robotLevel;//
        if (data.right == "1") {
            if (Math.random() * point >= 3) {
                let time = 0.4 + Math.random() * 2 + Game.getIns().robotLevel * 0.65;
                this.scheduleOnce(() => {
                    if (this.touched) return;
                    Game.getIns().isRobot = true;
                    this.onTouchText();
                }, time);
            }
        }
    }
    showSuccessAni() {
        Game.getIns().pause = false;
        pg.hw.playLocalAudio("right");
        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;
        Game.getIns().isRobot = false;
        this.onTouchText();
    }
    onTouchText() {
        if (Game.getIns().isOver) return;
        if (this.touched) return;
        this.touched = true;
        let data = this.data;
        if (Game.getIns().isRobot) {
            if (data.right == "1") {
                pg.view.visible(this.right, true);
                pg.hw.playLocalAudio("right");
                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);
                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();
            }
        }
    }
}
