// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import pg from "./pg";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    // LIFE-CYCLE CALLBACKS:
    private text_tip: cc.Node;
    private btn_audio_start: cc.Node;
    private btn_audio_stop: cc.Node;
    private speak_ske: cc.Node;
    private bg_goodjob: cc.Node;
    private bg_tryagain: cc.Node;
    onLoad() {
        this.text_tip = pg.view.find(this, "text_tip");
        this.btn_audio_start = pg.view.find(this, "btn_audio_start");
        this.btn_audio_stop = pg.view.find(this, "btn_audio_stop");
        this.speak_ske = pg.view.find(this, "speak_ske");
        this.bg_goodjob = pg.view.find(this, "bg_goodjob");
        this.bg_tryagain = pg.view.find(this, "bg_tryagain");

        this.text_tip.active = false;
        this.btn_audio_start.active = false;
        this.btn_audio_stop.active = false;
        this.speak_ske.active = false;
        this.bg_goodjob.active = false;
        this.bg_tryagain.active = false;

        pg.view.touchOn(this.btn_audio_start, this.onTouchStart, this);
        pg.view.touchOn(this.btn_audio_stop, this.onTouchStop, this);
        this.tryCount = 0;

        pg.event.on("speak_open", (data) => {
            this.tryCount = 0;
            this.open(data)
        })
    }

    private tryCount: number;
    private val: string;
    open(cell) {
        if (cell) {
            this.val = cell.value;
        }
        this.text_tip.active = true;
        this.btn_audio_start.active = true;
        this.btn_audio_stop.active = false;
        this.speak_ske.active = true;
        this.bg_goodjob.active = false;
        this.bg_tryagain.active = false;
    }
    close() {
        this.text_tip.active = false;
        this.btn_audio_start.active = false;
        this.btn_audio_stop.active = false;
        this.speak_ske.active = false;
        this.bg_goodjob.active = false;
        this.bg_tryagain.active = false;
    }

    onTouchStart() {
        pg.view.playDBAnimation(this.speak_ske, "newAnimation", 0);
        this.btn_audio_start.active = false;
        this.btn_audio_stop.active = true;

        let win: any = window;
        win.courseware && win.courseware.startTest(this.val); //开始录音
    }
    onTouchStop() {
        this.btn_audio_start.active = false;
        this.btn_audio_stop.active = false;
        let win: any = window;
        if (!win.courseware) {
            setTimeout(() => {
                let data = {
                    "dtLastResponse": "2021-09-17 13:36:26:109",
                    "refText": "I haven't found anything out about him yet.",
                    "recordId": "6144295213f0a2200000ed93",
                    "eof": 1,
                    "result": {
                        "pronunciation": 25,
                        "resource_version": "3.1.0",
                        "fluency": 60,
                        "rhythm": 90,
                        "kernel_version": "5.2.4",
                        "overall": 75,
                        "integrity": 100,
                        "duration": "6.779",
                        "rear_tone": "fall",
                        "speed": 96,
                    },
                    "tokenId": "6144295408558b08dd000001",
                    "applicationId": "154838659000009e"
                };
                this.onRealStop(data.result.overall);
            }, 100);
        } else {
            win.courseware && win.courseware.stopTest((data: any) => {
                data = JSON.parse(data);
                this.onRealStop(data.result.overall);
            });
        }

    }
    onRealStop(score) {
        pg.view.playDBAnimation(this.speak_ske, "newAnimation", 1);
        this.text_tip.active = false;
        this.btn_audio_start.active = false;
        this.btn_audio_stop.active = false;
        this.speak_ske.active = false;
        this.bg_goodjob.active = false;
        this.bg_tryagain.active = false;

        setTimeout(() => {
            // let score = Math.random() * 100 - 50;
            pg.hw.playLocalAudio("goodjob");
            if (score > 50) {
                this.bg_goodjob.active = true;
                setTimeout(() => {
                    this.close();
                    pg.event.emit("speak_success");
                }, 2000);
            } else {
                if (this.tryCount >= 1) {
                    this.bg_tryagain.active = true;
                    pg.hw.playLocalAudio("tryagain");
                    this.tryCount++;
                    //需要处理提供两次机会。重置一次到open状态
                    setTimeout(() => {
                        this.close();
                        pg.event.emit("speak_fail");
                    }, 2000);

                } else {
                    this.bg_tryagain.active = true;
                    pg.hw.playLocalAudio("tryagain");
                    this.tryCount++;
                    setTimeout(() => {
                        this.open(null);
                    }, 1000);
                }
            }
        }, 300);
    }

    start() {

    }

    // update (dt) {}
}
