local ZEventDispatchCenter = {}

ZEventDispatchCenter.EventType = {
    -- UPDATE EVENT
    UPDATE_RETRY                    = "UPDATE_RETRY",
    UPDATE_STATE_INFO               = "UPDATE_STATE_INFO",
    UPDATE_PROGRESS_PERCENT         = "UPDATE_PROGRESS_PERCENT",
    UPDATE_SHOW_TIPS                = "UPDATE_SHOW_TIPS",
    UPDATE_SHOW_UPDATE_DETAIL       = "UPDATE_SHOW_UPDATE_DETAIL",

    -- NET EVENT
    NET_CONNECT_STATE                   = "NET_CONNECT_STATE",
    HEARTBEAT                           = "HEARTBEAT",
}

ZEventDispatchCenter.EventQuene = {}

function ZEventDispatchCenter:RegisterEvent(eventType, callback)
    print("--------------------> ZEventDispatchCenter:RegisterEvent eventType", eventType, callback)
    if self.EventType[eventType] == nil or type(callback) ~= "function" then
        print("ZEventDispatchCenter:RegisterEvent param is error~ eventType:", eventType)
        return
    end

    if self.EventQuene[eventType] == nil then
        self.EventQuene[eventType] = {}
    end

    --local index = table.getn(self.EventQuene[eventType])
    --self.EventQuene[eventType][index + 1] = callback
    table.insert(self.EventQuene[eventType], callback)
end

function ZEventDispatchCenter:UnregisterEvent(eventType, callback)
    print("--------------------> ZEventDispatchCenter:UnregisterEvent eventType", eventType, callback)
    if self.EventQuene[eventType] == nil or type(callback) ~= "function" then
        print("ZEventDispatchCenter:UnregisterEvent param is error~ eventType:", eventType)
        return
    end

    for k, v in pairs(self.EventQuene[eventType]) do
        if callback == v then
            table.remove(self.EventQuene[eventType], k)
            return
        end
    end
end

function ZEventDispatchCenter:DispatchEvent(eventType, ...)
    print("--------------------> ZEventDispatchCenter:Dispatch eventType", eventType)
    if self.EventType[eventType] == nil then
        print("ZEventDispatchCenter:DispatchEvent eventType is unknow : ", eventType)
        return
    end

    if self.EventQuene[eventType] == nil then
        print("ZEventDispatchCenter:DispatchEvent callback is nil eventType:", eventType)
        return
    end

    for k, v in pairs(self.EventQuene[eventType]) do
        v(...)
    end
end

return ZEventDispatchCenter