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) {
                this.provider = new LoopTask(this, option.getUrl, option.historyUrl, option.statusUrl);
            } else {
                message = 'loop mode: option.getUrl 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 taskControl = this;
    $.ajax({
    	url: this.submitUrl,
        type: 'POST',
        data: JSON.stringify(data),
        dataType: "json",
        contentType : 'application/json;charset=utf-8',
        success: function(result) {
            if (result.success) {
                success.call(taskControl, taskControl.provider.onSubmit(result));
            } else {
                error.call(taskControl, result.message);
            }
        },
        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);
    });
}

TaskControl.prototype.status = function(callback) {
    this.provider.getStatus(callback);
}

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

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

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

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

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

LoopTask.prototype.getStatus = function(callback) {
    if (this.statusUrl != undefined) {
        $.post(this.statusUrl, {}, function(status) {
            if (status != undefined) {
                callback(status);
            }
        });
    }
}


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

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

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

//列表模式领取任务
ListTask.prototype.getTask = function(success, none, error) {
    if (this.getCount < this.totalCount) {
        var task = this.taskList[this.getCount];
        task.index = this.getCount;
        task.exist = true;

        this.getCount++;
        success.call(this.taskControl, task);
    } else {
        none.call(this.taskControl, {
            exist: false,
            message: '无评卷任务'
        });
    }
}

//任务处理回调方法
ListTask.prototype.onSubmit = function(data) {
    if (data.index == this.markedCount) {
        this.markedCount++;
    }
    var status = {
        markedCount: this.markedCount,
        blockMarkedCount: this.markedCount,
        blockTotalCount: this.totalCount
    };
    return status;
}

//获取评卷历史方法
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);
}

ListTask.prototype.getStatus = function(callback) {
    var status = {
        valid: true,
        topCount: 0,
        markedCount: this.markedCount,
        blockMarkedCount: this.markedCount,
        blockTotalCount: this.totalCount,
        blockExceptionCount: 0
    };
    callback(status);
}