const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    // LIFE-CYCLE CALLBACKS:

    // private EditBox: cc.EditBox = null;

    onLoad(): void {
        // this.EditBox = this.node.getChildByName("EditBox").getComponent(cc.EditBox);
        // this.EditBox.node.on("text-changed", this.Voice, this);
        // let url: string = "https://code.responsivevoice.org/responsivevoice.js?key=8LG4XCOk";
        // cc.assetManager.loadRemote(url, (err: Error, res: cc.JsonAsset) => {
        //     if (err) {
        //         console.error(`[Error] ${url}載入失敗 err: ${err}`);
        //         return;
        //     }
        // });
    }

    start(): void {
        let self: this = this;

        let url: string = window.location.search;
        let request: object = [];
        if (url.indexOf("?") !== -1) {
            let str: string = url.substring(1);
            let strs: string[] = str.split("&");
            for (let i: number = 0; i < strs.length; i++) {
                request[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
            }
        }

        let ResponsiveVoice: any = window["responsiveVoice"];
        // var voicelist: any = ResponsiveVoice.getVoices();
        // let voice: string = this.EditBox.string;
        // let voice: string = "傻B豆豆";
        let voice: string = request["voice"];
        ResponsiveVoice.speak(voice, "Chinese Female", {
            onstart: function (Callback: any): void {
                //   IsEnd = true;
                self.node.getChildByName("Label").getComponent(cc.Label).string = voice;
            },
            // onend: function (Callback: any): void {
            //     //   IsEnd = true;
            // },
        });
    }

    // public Voice(): void {
    //     let ResponsiveVoice: any = responsiveVoice;
    //     // var voicelist: any = ResponsiveVoice.getVoices();
    //     let voice: string = this.EditBox.string;
    //     ResponsiveVoice.speak(voice, "Chinese Female", {
    //         // onend: function (EndCallback: any): void {
    //         //     //   IsEnd = true;
    //         // }
    //     });
    // }

    // update (dt) {}
}
