-- local skynet = require("skynet")
-- local serviceAddress = require("serviceAddress")
-- local serviceProxy = require("serviceProxy")
-- local eventd
-- local curlistenerHandleIndex = 1
-- local string_format = string.format
-- local table_insert = table.insert

-- local dispatch = {}

-- local crossEventClient = {}

-- function crossEventClient.addEventListener(crossEventType, eventId, objId, cb, obj)
--     assert(objId == nil or (type(objId) == "number" and objId > 0) or (type(objId) == "string" and #objId > 0),
--         string_format("[crossEventClient].addEventListener,objId"))
--     local dispatchKey = string_format("%s-%s", crossEventType, eventId)
--     local handle = curlistenerHandleIndex
--     curlistenerHandleIndex = curlistenerHandleIndex + 1
--     local listener = {cb, obj}
--     local eventListener = dispatch[dispatchKey]
--     if not eventListener then
--         eventListener = {}
--         dispatch[dispatchKey] = eventListener
--     end
--     local listeners
--     if objId then
--         eventListener.single = eventListener.single or {}
--         listeners = eventListener.single[objId]
--         if not listeners then
--             listeners = {}
--             eventListener.single[objId] = listeners
--         end
--     else
--         eventListener.all = eventListener.all or {}
--         listeners = eventListener.all
--     end

--     if not next(listeners) then
--         local addr = skynet.self()
--         skynet.send(eventd, "lua", "addEventListener", dispatchKey, addr, objId)
--     end
--     listeners[handle] = listener
--     return handle
-- end

-- function crossEventClient.removeEventListener(handle)
--     for dispatchKey, eventListener in pairs(dispatch) do
--         local singleListener = eventListener.single
--         if singleListener then
--             for objId, listeners in pairs(singleListener) do
--                 for h, _ in pairs(listeners) do
--                     if h == handle then
--                         listeners[h] = nil
--                         if not next(singleListener) then
--                             eventListener.single = nil
--                             local addr = skynet.self()
--                             skynet.send(eventd, "lua", "removeEventListener", dispatchKey, addr, objId)
--                         end
--                         break
--                     end
--                 end
--             end
--         end

--         local allListener = eventListener.all
--         if allListener then
--             for h, _ in pairs(allListener) do
--                 if h == handle then
--                     allListener[h] = nil
--                     if not next(allListener) then
--                         eventListener.all = nil
--                         local addr = skynet.self()
--                         skynet.send(eventd, "lua", "removeEventListener", dispatchKey, addr)
--                     end
--                     break
--                 end
--             end
--         end
--     end
-- end

-- function crossEventClient.dispatchEventBySend(crossEventType, eventId, objId, event)
--     assert(objId == nil or (type(objId) == "number" and objId > 0) or (type(objId) == "string" and #objId > 0),
--         string_format("[crossEventClient].addEventListener,objId"))
--     local dispatchKey = string_format("%s-%s", crossEventType, eventId)
--     event.dispatchKey = dispatchKey
--     event.dispatchObjId = objId
--     local strEvent = skynet.tostring(skynet.pack(event))
--     skynet.send(eventd, "lua", "dispatchEventBySend", dispatchKey, strEvent, objId)
-- end

-- function crossEventClient.dispatchEventByCall(crossEventType, eventId, objId, event)
--     assert(objId == nil or (type(objId) == "number" and objId > 0) or (type(objId) == "string" and #objId > 0),
--         string_format("[crossEventClient].addEventListener,objId"))
--     local dispatchKey = string_format("%s-%s", crossEventType, eventId)
--     event.dispatchKey = dispatchKey
--     event.dispatchObjId = objId
--     local strEvent = skynet.tostring(skynet.pack(event))
--     skynet.call(eventd, "lua", "dispatchEventByCall", dispatchKey, strEvent, objId)
-- end

-- function crossEventClient.dispatchEventComposeBySend(crossEventType, eventId, objId, event, amongSvr, amongCluster,
--     nodeNames)
--     assert(objId == nil or (type(objId) == "number" and objId > 0) or (type(objId) == "string" and #objId > 0),
--         string_format("[crossEventClient].addEventListener,objId"))
--     local dispatchKey = string_format("%s-%s", crossEventType, eventId)
--     event.dispatchKey = dispatchKey
--     event.dispatchObjId = objId
--     local strEvent = skynet.tostring(skynet.pack(event))
--     if amongSvr then
--         skynet.send(eventd, "lua", "dispatchEventBySend", dispatchKey, strEvent, objId)
--     end
--     if amongCluster then
--         for _, nodeName in pairs(nodeNames) do
--             local crossEventServerProxy = serviceProxy.remote(nodeName, "crossEvent")
--             crossEventServerProxy.remotePost.dispatchEventBySend(nodeName, dispatchKey, strEvent, objId)
--         end
--     end
-- end

-- local function dispatchEvent(session, _, strEvent)
--     local event = skynet.unpack(strEvent)
--     local dispatchKey = event.dispatchKey
--     local objId = event.dispatchObjId
--     local eventListener = dispatch[dispatchKey]
--     if not eventListener then
--         return
--     end

--     local tmpListeners = {}
--     if objId then
--         local singleListener = eventListener.single
--         if singleListener then
--             local listeners = singleListener[objId]
--             if listeners then
--                 for _, listener in pairs(listeners) do
--                     table_insert(tmpListeners, listener)
--                 end
--             end
--         end
--     end

--     local allListener = eventListener.all
--     if allListener then
--         for _, listener in pairs(allListener) do
--             table_insert(tmpListeners, listener)
--         end
--     end

--     for _, listener in pairs(tmpListeners) do
--         local cb = listener[1]
--         local obj = listener[2]
--         if obj then
--             cb(obj, event)
--         else
--             cb(event)
--         end
--     end

--     if session > 0 then
--         skynet.ret(skynet.pack(true))
--     end
-- end

-- local function init()
--     eventd = serviceAddress.crossEvent()
--     log.debug("[crossEventClient].init,eventd=%s", eventd)
--     skynet.register_protocol {
--         name = "crossevent",
--         id = skynet.PTYPE_TEXT,
--         unpack = skynet.tostring,
--         dispatch = dispatchEvent,
--     }
-- end

-- init()

-- return crossEventClient
