--[[
 -- @Description:
 -- Hello , DiaoSi ~ ( or not ? ) ,glad you saw my comment
 -- If you think my mod is okay , you can sponsor my breakfast by a tiny amount donation  ($_$)
 -- paypal:td1madao@163.com     alipay:td1madao@163.com
 -- If any question ,Please email me
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- @version 1.0.0
 -- @author td1madao
 -- @email td1madao@163.com
 -- @qq 360810498
 -- @date 16/8/6
 ]]
local TD = TD.td1madao.rivendst
local Rivendst_common_task_manager = Class(function(self, inst)
    TD.see()
    self.inst = inst
    self.taskList = {}
    self.accounter = 0
end)
function Rivendst_common_task_manager:common_period(period, fun, taskName, cleanTime, cleanFunction)
    TD.see()
    local player = self.inst
    if not fun then
        return
    end
    if not taskName then
        taskName = "task" .. tostring(self.accounter)
        taskName = taskName + 1
    end
    if not period then
        period = 1
    end
    if not cleanTime then
        cleanTime = 15
    end
    if not cleanFunction then
        cleanFunction = TD.empty
    end
    self:common_clean(taskName)
    self:common_clean(taskName .. "cleaner")
    self:common_task(cleanTime, function()
        cleanFunction()
        self:common_clean(taskName)
        self:common_clean(taskName .. "cleaner")
    end, taskName .. "cleaner")
    self.taskList[taskName] = player:DoPeriodicTask(period, fun)
    return self.taskList[taskName]
end

function Rivendst_common_task_manager:common_task(time, fun, taskName)
    --    TD.see()
    local player = self.inst
    if not fun then
        return
    end
    if not taskName then
        taskName = "task" .. tostring(self.accounter)
        taskName = taskName + 1
    end
    self:common_clean(taskName)
    self.taskList[taskName] = player:DoTaskInTime(time or 1, function()
        fun()
        self:common_clean(taskName)
    end)
    return self.taskList[taskName]
end

function Rivendst_common_task_manager:common_clean(taskName)
    -- TD.see()
    local player = self.inst
    if not taskName then
        return
    end
    if player and self.taskList[taskName] then
        if self.taskList[taskName].Cancel then
            self.taskList[taskName]:Cancel()
        end
        self.taskList[taskName] = nil
    end
end

function Rivendst_common_task_manager:common_exist(taskName)
    TD.see()
    local player = self.inst
    if not taskName then
        return false
    end
    return player and self.taskList[taskName]
end

function Rivendst_common_task_manager:common_cleanAll(arr)
    TD.see()
    if not arr then
        return
    end
    for i = 1, #arr do
        local name = arr[i]
        if name then
            self:common_clean(name)
        end
    end
end

function Rivendst_common_task_manager:server_period(period, fun, taskName, cleanTime, cleanFunction)
    return self:common_period(period, fun, taskName, cleanTime, cleanFunction)
end

function Rivendst_common_task_manager:player_period(period, fun, taskName, cleanTime, cleanFunction)
    return self:common_period(period, fun, taskName, cleanTime, cleanFunction)
end

function Rivendst_common_task_manager:server_task(time, fun, taskName)
    return self:common_task(time, fun, taskName)
end

function Rivendst_common_task_manager:player_task(time, fun, taskName)
    return self:common_task(time, fun, taskName)
end

function Rivendst_common_task_manager:server_clean(taskName)
    self:common_clean(taskName)
end

function Rivendst_common_task_manager:player_clean(taskName)
    self:common_clean(taskName)
end

function Rivendst_common_task_manager:server_exist(taskName)
    return self:common_exist(taskName)
end

function Rivendst_common_task_manager:player_exist(taskName)
    return self:common_exist(taskName)
end

function Rivendst_common_task_manager:server_cleanAll(arr)
    self:common_cleanAll(arr)
end

function Rivendst_common_task_manager:player_cleanAll(arr)
    self:common_cleanAll(arr)
end

return Rivendst_common_task_manager