function TaskControl(option) {
    this.option = option;
}

//初始化任务控制器
TaskControl.prototype.init = function() {
    var message = '';
    var option = this.option;
    //任务提交地址必须有
    if (option.submitUrl != undefined && option.submitUrl.length > 0) {
        this.submitUrl = option.submitUrl;
        if (option.mode == 'loop') {
            if (option.getUrl != undefined && option.getUrl.length > 0 && option.historyUrl != undefined && option.historyUrl.length > 0) {
                this.provider = new LoopTask(this, option.getUrl, option.historyUrl);
            } else {
                message = 'loop mode: option.getUrl or option.historyUrl is null';
            }
        } else if (option.mode == 'list') {
            if (isArray(option.tasks) && option.tasks.length > 0) {
                this.provider = new ListTask(this, option.tasks);
            } else {
                message = 'list mode: option.tasks is empty';
            }
        }
    } else {
        message = 'option.submitUrl is null';
    }

    if (this.provider == undefined) {
        if (option.error != undefined && typeof(option.error) == 'function') {
            option.error.call(this, message);
        }
    } else {
        if (option.success != undefined && typeof(option.success) == 'function') {
            option.success.call(this);
        }
    }
}

TaskControl.prototype.isFinish = function() {
    return this.provider.isFinish();
}

//领取新的任务
TaskControl.prototype.fetch = function(success, none, error) {
    this.provider.getTask(function(task) {
        this.currentTask = task;
        success.call(this, task);
    }, function(status) {
        this.currentTask = undefined;
        none.call(this, status);
    }, function() {
        this.currentTask = undefined;
        error.call(this);
    });
}

//提交任务处理结果
TaskControl.prototype.submit = function(data, success, error) {
    var submitUrl = this.submitUrl;
    var taskControl = this;
    $.ajax({
        url: submitUrl,
        type: 'POST',
        data: data,
        success: function() {
            taskControl.provider.onSubmit(data);
            success.call(taskControl);
        },
        error: function() {
            error.call(taskControl);
        }
    });
}

TaskControl.prototype.history = function(data, success, error) {
    this.provider.getHistory(data, function(list) {
        success.call(this, list);
    }, function(message) {
        error.call(this, message);
    });
}

function LoopTask(taskControl, getUrl, historyUrl) {
    this.taskControl = taskControl;
    this.getUrl = getUrl;
    this.historyUrl = historyUrl;
}

LoopTask.prototype.isFinish = function() {
    return false;
}

//遍历模式领取任务
LoopTask.prototype.getTask = function(success, none, error) {
    var getUrl = this.getUrl;
    var taskControl = this.taskControl;
    $.ajax({
        url: getUrl,
        type: 'POST',
        success: function(task) {
            if (task != undefined && task.exist === true) {
                success.call(taskControl, task);
            } else {
                none.call(taskControl, task);
            }
        },
        error: function(request, message) {
            error.call(taskControl, message);
        }
    });
}

//任务处理回调方法
LoopTask.prototype.onSubmit = function() {}

//获取评卷历史方法
LoopTask.prototype.getHistory = function(data, success, error) {
    var historyUrl = this.historyUrl;
    var taskControl = this.taskControl;
    $.ajax({
        url: historyUrl,
        type: 'POST',
        data: data,
        success: function(obj) {
            success.call(taskControl, obj);
        },
        error: function(request, message) {
            error.call(taskControl, message);
        }
    });
}


//列表方式的任务引擎
//用于预先指定需要评卷的任务的情况

function ListTask(taskControl, taskList) {
    this.taskControl = taskControl;
    this.taskList = taskList;
    this.totalCount = taskList.length;
    this.markedCount = 0;
}

ListTask.prototype.isFinish = function() {
    return this.markedCount == this.totalCount;
}

//列表模式领取任务
ListTask.prototype.getTask = function(success, none, error) {
    if (this.markedCount < this.totalCount) {
        var task = this.taskList[this.markedCount];
        task.index = this.markedCount;
        task.exist = true;
        task.blockToMarkCount = this.totalCount;
        task.blockMarkedCount = this.markedCount;
        task.markCount = this.markedCount;
        success.call(this.taskControl, task);
    } else {
        none.call(this.taskControl, {
            exist: false,
            blockToMarkCount: this.totalCount,
            blockMarkedCount: this.markedCount,
            markCount: this.markedCount
        });
    }
}

//任务处理回调方法
ListTask.prototype.onSubmit = function(data) {
    if (data.index == this.markedCount) {
        this.markedCount++;
    }
}

//获取评卷历史方法
ListTask.prototype.getHistory = function(data, success, error) {
    var start = this.markedCount - (data.pageNumber - 1) * data.pageSize - 1;
    var end = start - data.pageSize;
    var list = [];
    for (var i = start; i > 0 && i > end; i--) {
        var task = this.taskList[i];
        task.previous = true;
        list.push(task);
    }
    success.call(this.taskControl, list);
}