//JSON结构化数据显示模块
var json_view = function (option, success) {
    var object = new JsonView(option);
    success();
    return object;
}

function JsonView(option) {
    this.markControl = option.markControl;
    this.init();
    this.markControl.on('step.board.show', this, function (event, context, eventObject) {
        this.container.removeClass('span12');
        this.container.addClass('span10');
    });
    this.markControl.on('step.board.hide', this, function (event, context, eventObject) {
        this.container.removeClass('span10');
        this.container.addClass('span12');
    });
    this.markControl.on('task.get.before', this, function (event, context, eventObject) {
        this.task = undefined;
        this.render();
    });
    this.markControl.on('task.get.success', this, function (event, context, eventObject) {
        this.task = context.task;
        this.render();
    });
    this.markControl.on('task.get.none', this, function (event, context, eventObject) {
        this.task = undefined;
        this.render();
    });
}

JsonView.prototype.init = function () {
    this.container = this.markControl.container.imageContent;
    this.container.height(this.markControl.container.centerContent.height());
    this.container.css('overflow', 'scroll');

    this.holder = getDom(this.holder_dom, this.markControl).appendTo(this.container);
    this.holder.empty();
}

JsonView.prototype.render = function () {
    if (this.task != undefined) {
        this.task.markStepList.forEach(step => {
            let question = this.findQuestion(step.mainNumber, step.subNumber, this.task.paperData)
            let jsons = this.findJson(step.mainNumber, step.subNumber, this.task.jsonData)
            if (question != undefined ) {
                let numberTitle = {
                    "blocks": [{
                        "type": "text",
                        "param": {"bold": true},
                        "value": "题号：" + step.mainNumber + "-" + step.subNumber
                    }]
                };
                let bodyTitle = {
                    "blocks": [{
                        "type": "text",
                        "param": {"bold": true},
                        "value": "题干："
                    }]
                };
                var textcount = jsons.length > 0? this.textCount(jsons) : 0
                let answerTitle = {
                    "blocks": [{
                        "type": "text",
                        "param": {"bold": true},
                        "value": "考生答案："
                    }, {
                        "type": "text",
                        "param": {
                            "italic": true,
                            "danger": true
                        },
                        "value": "(字数统计：" + textcount + ")"
                    }]
                };
                let standardTitle = {
                    "blocks": [{
                        "type": "text",
                        "param": {"bold": true},
                        "value": "标准答案："
                    }]
                };
                $(renderSection(numberTitle)).appendTo(this.holder);
                $(renderSection(bodyTitle)).appendTo(this.holder);
                if (question.parentBody != null && question.parentBody != undefined) {
                    $(renderRichText(question.parentBody)).appendTo(this.holder);
                }
                $(renderRichText(question.body)).appendTo(this.holder);
                $(renderSection(answerTitle)).appendTo(this.holder);
                if(jsons.length > 0){
                	jsons.forEach(node => {
                		node.answer.forEach(node => {
                			$(renderRichText(node)).appendTo(this.holder);
                		});
                	})
                }
                $(renderSection(standardTitle)).appendTo(this.holder);
                let nodes =renderRichText(question.answer);
                nodes.forEach(node => {
                    $(node).appendTo(this.holder);
                })
            }
        });

        if (this.viewer != undefined) {
            this.viewer.destroy();
        }
        this.viewer = new Viewer(this.container[0], {});
        this.markControl.trigger('task.load.finish');
    } else {
        this.holder.empty();
    }
}

JsonView.prototype.textCount = function (jsons) {
    jsons = Array.isArray(jsons) ? jsons : []
    let count = 0
    jsons.forEach(data => {
        count += this.jsonTextCount(data.answer)
    })
    return count;
}

JsonView.prototype.jsonTextCount = function (jsonData) {
    let count = 0
    if (Array.isArray(jsonData)) {
        for (let i in jsonData) {
            count += this.jsonTextCount(jsonData[i])
        }
    } else {
        let sections = Array.isArray(jsonData.sections) ? jsonData.sections : [];
        sections.forEach(section => {
            let blocks = Array.isArray(section.blocks) ? section.blocks : [];
            blocks.forEach(block => {
                if (block.type === 'text' && block.value != undefined) {
                    count += block.value.length;
                }
            });
        });
    }
    return count
}

JsonView.prototype.findJson = function (mainNumber, subNumber, jsonData) {
    jsonData = Array.isArray(jsonData) ? jsonData : {}
    let result = []
    for (let i in jsonData) {
    	var sub = jsonData[i].subIndex==null?jsonData[i].subNumber:jsonData[i].subNumber+"-"+jsonData[i].subIndex;
        if (jsonData[i].mainNumber === mainNumber && sub == subNumber) {
            result.push(jsonData[i])
        }
    }
    return result
}

JsonView.prototype.findQuestion = function (mainNumber, subNumber, paperData) {
    paperData = paperData || {}
    if (Array.isArray(paperData.details)) {
        for (let i in paperData.details) {
            let detail = paperData.details[i]
            if (detail.number === mainNumber && Array.isArray(detail.questions)) {
                for (let j in detail.questions) {
                    if (detail.questions[j].number == subNumber) {
                        return detail.questions[j]
                    }
                    if(detail.questions[j].subQuestions!=undefined){
                    	for (let k in detail.questions[j].subQuestions) {
                    		var sub = detail.questions[j].number+'-'+detail.questions[j].subQuestions[k].number;
                    		if (sub == subNumber) {
                    			var q =detail.questions[j].subQuestions[k];
                    			q.parentBody = detail.questions[j].body;
                                return q;
                            }
                    	}
                    }
                }
            }
        }
    } else if (Array.isArray(paperData.questions)) {
        for (let j in paperData.questions) {
            if (paperData.questions[j].mainNumber === mainNumber && paperData.questions[j].subNumber == subNumber) {
                return paperData.questions[j]
            }
        }
    }
    return undefined
}

JsonView.prototype.holder_dom = '<div class="rich-text"></div>';