import pg from "./pg_jj15_zww";
import { showTrebleFirework } from "../script/util_jj15_zww";

let win: any = window;
let courseware = win.courseware;
const hw = {
    log: {
        d(str) {
            const node = cc.find('middleLayer');
            if (node) {
                node.getComponent('middleLayer').log(str);
            } else {
                console.log(str);
            }
        }
    },
    record: {
        // 测评， api的值主要用于总分获取
        testing: false,
        testAudio: "",
        startTest(text) {
            return new Promise(resolve => {
                if (hw.record.testing) return resolve(false);
                hw.record.testing = true;
                hw.record.testAudio = "";
                hw.log.d("-----------------------startTest")
                cc.audioEngine.stopAllEffects();
                courseware && courseware.startTest(text); //开始录音
                resolve(true);
            })
        },
        //data 数据 {text:识别的文本内容， audioUrl:录音内容}
        endTest() {
            return new Promise(resolve => {
                if (!hw.record.testing) return;
                hw.record.testing = false;
                if (!courseware) {
                    let data = {
                        "audioUrl": "https://staging-teach.cdn.ireadabc.com/bea0ba87bcf8bc09815f34b4790ffac9.mp3",// 录音音频
                        "eof": 1,
                        "file": "/storage/emulated/0/Android/data/com.example.oxford/cache/shengtong/gradeVoice.mp3",
                        "result": {
                            "overall": 87,// 总分
                            "duration": "2.670",
                            "fluency": 86,
                            "integrity": 50,
                            "kernel_version": "3.7.1",
                            "pronunciation": 28,
                            "resource_version": "2.3.11",
                            "rhythm": 28,
                            "sentences": [
                                {
                                    "beginIndex": 0,
                                    "details": [
                                        {
                                            "charType": 0, "end": 0, "overall": 56, "prominence": 0, "start": 0, "word": "1234567890123456789012345678901234567890"
                                        }, {
                                            "charType": 0, "end": 0, "overall": 0, "prominence": 0, "start": 0, "word": "12345"
                                        }],
                                    "end": 0, "endIndex": 0, "overall": 28,
                                    "sentence": "Good morning", "start": 0
                                }],
                            "speed": 28
                        }, "tokenId": "62d7a2343327934258000009"
                    }
                    pg.hw.addRecord(data)
                    resolve(data)
                } else {
                    //结束录音
                    courseware.stopTest((data) => {
                        data = JSON.parse(data);
                        hw.log.d("data: " + JSON.stringify(data));
                        pg.hw.addRecord(data)
                        resolve(data);
                    });
                }
            })
        }
    },
    util: {
        /**
         * RibbonNodeBase 节点  jj17_duicuo中有   textures/jj_common/sahua
         * RibbonNodeBase 与canvas同级别
         * end 放置到 Canvas/res/audio/end
        */
        playEndTrebleFirework() {
            let audioNode = cc.find("Canvas/res/audio/end");
            pg.audio.playLocalAudio(audioNode);
            const camera = cc.find('Canvas/Main Camera');
            camera.zIndex = 100;

            showTrebleFirework(camera, cc.find('RibbonNodeBase').children);
        },
        showTipNodeAuto(node, audioName) {
            audioName && pg.hw.playLocalAudio(audioName)
            pg.view.visible(node, true);
            node.opacity = 255;
            cc.Tween.stopAllByTarget(node)
            cc.tween(node).delay(0.8).to(0.5, { opacity: 0 }).start();
        }
    }
}
export default hw;





