//多媒体显示模块
var json_loader = function (option, success) {
    var object = new JsonLoader(option);
    success();
    return object;
}

function JsonLoader(option) {
    this.markControl = option.markControl;
    this.jsonServer = option.jsonServer;
}

JsonLoader.prototype.build = function (task, callback) {
    var self = this;
    if (task != undefined && task.jsonUrl != undefined && task.paperUrl != undefined) {
//      TODO-测试代码，读取固定的json文件
//         var result = '[{"mainNumber": 2,"subNumber": 10,"body": {"sections": [{ "blocks":[{"type": "text","value": "我是题目我是题目我是题目"}]}]},"answer": {"sections": [{"blocks":[{"type": "image","value": "https://ecs-test-static.qmth.com.cn/oe-answer-file/3/8/20/3_8_20_15748452889591137.jpeg"}]},{"blocks":[{"type": "audio","value": "https://ecs-test-static.qmth.com.cn/oe-answer-file/3/8/19/3_8_19_15748452552644264.mp3"}]}]},"studentAnswer": {"sections": [{"blocks":[{"type": "image","value": "https://ecs-test-static.qmth.com.cn/oe-answer-file/3/8/20/3_8_20_15748452889591137.jpeg"},{"type": "image","value": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582709869784&di=39683b1330bd09bebd93c95fe925ca70&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn10108%2F170%2Fw600h370%2F20190222%2Fd6de-htknpmh2595255.jpg"},{"type": "text","value": "我是答案答案daan"}]}]}}]';
//         task.jsonData = JSON.parse(result);
//         callback();
        let promises = []
        promises.push(new Promise((resolve, reject) => {
            $.ajax({
                type: "GET",
                url: self.jsonServer + task.paperUrl,
                dataType: "json",
                success: function (data) {
                    task.paperData = data
                    resolve()
                },
                error: function () {
                    console.log('paper load error:' + self.jsonServer + task.paperUrl);
                    reject('paper load error');
                }
            });
        }))
        promises.push(new Promise((resolve, reject) => {
            $.ajax({
                type: "GET",
                url: self.jsonServer + task.jsonUrl,
                dataType: "json",
                success: function (data) {
                    task.jsonData = data;
                    resolve();
                },
                error: function () {
                    console.log('json load error:' + self.jsonServer + task.jsonUrl);
                    reject('json load error');
                }
            });
        }))
        Promise.all(promises).then(() => {
            callback()
        }).catch(error => {
            callback(error)
        })
    }
}