DiseaseConsoleCommandEvent = {}
local DiseaseConsoleCommandEvent_mt = Class(DiseaseConsoleCommandEvent, Event)
InitEventClass(DiseaseConsoleCommandEvent, "DiseaseConsoleCommandEvent")

function DiseaseConsoleCommandEvent.emptyNew()
    return Event.new(DiseaseConsoleCommandEvent_mt)
end

function DiseaseConsoleCommandEvent.new(commandName, arg1, arg2, arg3)
    local self = DiseaseConsoleCommandEvent.emptyNew()
    self.commandName = commandName
    self.arg1 = arg1
    self.arg2 = arg2
    self.arg3 = arg3
    return self
end

local function writeOptionalString(streamId, value)
    local hasValue = value ~= nil
    streamWriteBool(streamId, hasValue)
    if hasValue then
        streamWriteString(streamId, tostring(value))
    end
end

local function readOptionalString(streamId)
    if streamReadBool(streamId) then
        return streamReadString(streamId)
    end

    return nil
end

function DiseaseConsoleCommandEvent:writeStream(streamId, _)
    streamWriteString(streamId, tostring(self.commandName or ""))
    writeOptionalString(streamId, self.arg1)
    writeOptionalString(streamId, self.arg2)
    writeOptionalString(streamId, self.arg3)
end

function DiseaseConsoleCommandEvent:readStream(streamId, connection)
    self.commandName = streamReadString(streamId)
    self.arg1 = readOptionalString(streamId)
    self.arg2 = readOptionalString(streamId)
    self.arg3 = readOptionalString(streamId)
    self:run(connection)
end

function DiseaseConsoleCommandEvent:run(connection)
    if g_server ~= nil and DiseaseCore ~= nil and DiseaseCore.runRemoteConsoleCommand ~= nil then
        DiseaseCore:runRemoteConsoleCommand(connection, self.commandName, self.arg1, self.arg2, self.arg3)
    end
end

function DiseaseConsoleCommandEvent.send(commandName, arg1, arg2, arg3)
    if g_client ~= nil then
        g_client:getServerConnection():sendEvent(DiseaseConsoleCommandEvent.new(commandName, arg1, arg2, arg3))
    end
end
