﻿//<using>
//  nui/prototype.js
//  nui/nui.js
//  nui/lang.js
//  nui/quill/entity/entity.js
//  nui/quill/entity/question.js
//</using>

/**
 * The namespace of question entity used in netranking.cn 
 * @namespace NUI.Quill.Entity.Answer
 */
NUI.Quill.Entity.Answer = {
    Base : function() {},
    Choice : function() {},
    Matrix : function() {},
    Input : function() {},
    Rank : function() {},
    Sort : function() {},
    ConstSum : function() {},
    Location : function() {},
    MatrixInput : function() {}
};

/**
 * The base class of all kinds of answers.
 * @class NUI.Quill.Entity.Answer.Base
 */
NUI.Quill.Entity.Answer.Base = function(vSecret) {
    var m_secret = vSecret || {},
        m_question = m_secret.question || new NUI.Quill.Entity.Question.Base();
    
    return {
        /**
         * @variable type {NUI.Quill.Enum.QuestionType.*.id}
         */
        type : -1
    };
};

/**
 * The Choice Question's answer
 * @class NUI.Quill.Answer.Question.Choice
 */
NUI.Quill.Entity.Answer.Choice = (function() {
    var g_ns = NUI.Quill.Entity.Answer,
        g_enum = NUI.Quill.Enum;
        
    /**
     * Constructor
     * @param vQuestion {object}. The question entity
     */
    return function(vQuestion) {
        if(vQuestion == null || !(g_enum.QuestionType.CHOICE.id == vQuestion.type)) {
            return null;
        }
            
        var m_secret = {
                question : vQuestion
            },
            that = new g_ns.Base(m_secret),
            m_question = m_secret.question;
        
        NUI.Util.ObjectService.annex(that, {
            type : g_enum.QuestionType.CHOICE.id,
            /**
             * One dimension array.
             */
            answers : (function(){
                var retval = [],
                    len = m_question.optSet.options.length + (m_question.optSet.hasOtherOption ? 1 : 0);
                for(var i=0; i<len; i++) {
                    retval[i] = 0;
                }
                return retval;
            })(),
            other : ""
        });
        return that;
    };
})();

/**
 * The Matrix Question's answer
 * @class NUI.Quill.Answer.Question.Matrix
 */
NUI.Quill.Entity.Answer.Matrix = (function() {
    var g_ns = NUI.Quill.Entity.Answer,
        g_enum = NUI.Quill.Enum;
        
    /**
     * Constructor
     * @param vQuestion {object}. The question entity
     */
    return function(vQuestion) {
        if(vQuestion == null || !(g_enum.QuestionType.MATRIX.id == vQuestion.type)) {
            return null;
        }
            
        var m_secret = {
                question : vQuestion
            },
            that = new g_ns.Base(m_secret),
            m_question = m_secret.question;
        
        NUI.Util.ObjectService.annex(that, {
            type : g_enum.QuestionType.MATRIX.id,
            /**
             * Two dimension array. Store the selected options' indexes
             */
            answers : (function() {
                var retval = [],
                    rowCount = m_question.rows.options.length + (m_question.rows.hasOtherOption ? 1 : 0),
                    colCount = m_question.cols.options.length + (m_question.cols.hasOtherOption ? 1 : 0);
                for(var r=0; r<rowCount; r++) {
                    var row = [];
                    for(var c=0; c<colCount; c++) {
                        row[c] = 0;
                    }
                    retval[r] = row;
                }
                return retval;
            })(),
            rowOther : "",
            colOther : ""
        });
        return that;
    };
})();

/**
 * The Input Question's answer
 * @class NUI.Quill.Answer.Question.Input
 */
NUI.Quill.Entity.Answer.Input = (function() {
    var g_ns = NUI.Quill.Entity.Answer,
        g_enum = NUI.Quill.Enum;
        
    /**
     * Constructor
     * @param vQuestion {object}. The question entity
     */
    return function(vQuestion) {
        if(vQuestion == null || !(g_enum.QuestionType.INPUT.id == vQuestion.type)) {
            return null;
        }
            
        var m_secret = {
                question : vQuestion
            },
            that = new g_ns.Base(m_secret),
            m_question = m_secret.question;
        
        NUI.Util.ObjectService.annex(that, {
            type : g_enum.QuestionType.INPUT.id,
            /**
             * Store the input values
             */
            answers : (function() {
                var retval = [],
                    count = m_question.inputs.length;
                for(var i=0; i<count; i++) {
                    retval[retval.length] = "";
                }
                return retval;
            })()
        });
        return that;
    };
})();

/**
 * The Rank Question's answer
 * @class NUI.Quill.Answer.Question.Rank
 */
NUI.Quill.Entity.Answer.Rank = (function() {
    var g_ns = NUI.Quill.Entity.Answer,
        g_enum = NUI.Quill.Enum;
        
    /**
     * Constructor
     * @param vQuestion {object}. The question entity
     */
    return function(vQuestion) {
        if(vQuestion == null || !(g_enum.QuestionType.RANK.id == vQuestion.type)) {
            return null;
        }
            
        var m_secret = {
                question : vQuestion
            },
            that = new g_ns.Base(m_secret),
            m_question = m_secret.question;
        
        NUI.Util.ObjectService.annex(that, {
            type : g_enum.QuestionType.RANK.id,
            /**
             * Store the input values
             */
            answers : (function() {
                var retval = [],
                    count = m_question.options.length;
                for(var i=0; i<count; i++) {
                    retval[retval.length] = 0;
                }
                return retval;
            })()
        });
        return that;
    };
})();

/**
 * The Sort Question's answer
 * @class NUI.Quill.Answer.Question.Sort
 */
NUI.Quill.Entity.Answer.Sort = (function() {
    var g_ns = NUI.Quill.Entity.Answer,
        g_enum = NUI.Quill.Enum;
        
    /**
     * Constructor
     * @param vQuestion {object}. The question entity
     */
    return function(vQuestion) {
        if(vQuestion == null || !(g_enum.QuestionType.SORT.id == vQuestion.type)) {
            return null;
        }
            
        var m_secret = {
                question : vQuestion
            },
            that = new g_ns.Base(m_secret),
            m_question = m_secret.question;
        
        NUI.Util.ObjectService.annex(that, {
            type : g_enum.QuestionType.SORT.id,
            /**
             * Store the rank values
             */
            answers : (function() {
                var retval = [],
                    count = m_question.options.length;
                for(var i=0; i<count; i++) {
                    retval[retval.length] = -1;
                }
                return retval;
            })()
        });
        return that;
    };
})();

/**
 * The ConstSum Question's answer
 * @class NUI.Quill.Answer.Question.ConstSum
 */
NUI.Quill.Entity.Answer.ConstSum = (function() {
    var g_ns = NUI.Quill.Entity.Answer,
        g_enum = NUI.Quill.Enum;
        
    /**
     * Constructor
     * @param vQuestion {object}. The question entity
     */
    return function(vQuestion) {
        if(vQuestion == null || !(g_enum.QuestionType.CONSTSUM.id == vQuestion.type)) {
            return null;
        }
            
        var m_secret = {
                question : vQuestion
            },
            that = new g_ns.Base(m_secret),
            m_question = m_secret.question;
        
        NUI.Util.ObjectService.annex(that, {
            type : g_enum.QuestionType.CONSTSUM.id,
            /**
             * Store the rank values
             */
            answers : (function() {
                var retval = [],
                    count = m_question.options.length,
                    value = (count > 0) ? 1/count : -1;
                for(var i=0; i<count; i++) {
                    retval[retval.length] = value;
                }
                return retval;
            })()
        });
        return that;
    };
})();

/**
 * The Location Question's answer
 * @class NUI.Quill.Answer.Question.Location
 */
NUI.Quill.Entity.Answer.Location = (function() {
    var g_ns = NUI.Quill.Entity.Answer,
        g_enum = NUI.Quill.Enum;
        
    /**
     * Constructor
     * @param vQuestion {object}. The question entity
     */
    return function(vQuestion) {
        if(vQuestion == null || !(g_enum.QuestionType.LOCATION.id == vQuestion.type)) {
            return null;
        }
            
        var m_secret = {
                question : vQuestion
            },
            that = new g_ns.Base(m_secret),
            m_question = m_secret.question;
        
        NUI.Util.ObjectService.annex(that, {
            type : g_enum.QuestionType.LOCATION.id,
            /**
             * Store the rank values
             */
            answers : (function() {
                return [];
            })()
        });
        return that;
    };
})();

/**
 * The MatrixInput Question's answer
 * @class NUI.Quill.Answer.Question.MatrixInput
 */
NUI.Quill.Entity.Answer.MatrixInput = (function() {
    var g_ns = NUI.Quill.Entity.Answer,
        g_enum = NUI.Quill.Enum;
        
    /**
     * Constructor
     * @param vQuestion {object}. The question entity
     */
    return function(vQuestion) {
        if(vQuestion == null || !(g_enum.QuestionType.MATRIXINPUT.id == vQuestion.type)) {
            return null;
        }
            
        var m_secret = {
                question : vQuestion
            },
            that = new g_ns.Base(m_secret),
            m_question = m_secret.question;
        
        NUI.Util.ObjectService.annex(that, {
            type : g_enum.QuestionType.MATRIXINPUT.id,
            /**
             * Two dimension array. Store the selected options' indexes
             */
            answers : (function() {
                var retval = [],
                    rowCount = m_question.rows.options.length + (m_question.rows.hasOtherOption ? 1 : 0),
                    colCount = m_question.cols.options.length + (m_question.cols.hasOtherOption ? 1 : 0);
                for(var r=0; r<rowCount; r++) {
                    var row = [];
                    for(var c=0; c<colCount; c++) {
                        row[c] = "";
                    }
                    retval[r] = row;
                }
                return retval;
            })(),
            rowOther : "",
            colOther : ""
        });
        return that;
    };
})();

