
local Utils = {}

function Utils:GetScriptNameByScriptPath(scriptPath)
	local startIndex = string.find(scriptPath, "%a+.lua")
	local endIndex = string.find(scriptPath, ".lua") - 1
	local scriptName = string.sub(scriptPath, startIndex, endIndex)

	return scriptName
end

function Utils:OutputLuaTable(tData, szPrefix)
	if not szPrefix then szPrefix = "" end
    for _, v in pairs(tData) do
		if type(v) == "table" then
            Utils:OutputLuaTable(v, "\t")
        end
    end
end

function Utils:Table2String(tab, csp)
    local parentOffset = csp or ""
    local propertyOffset = parentOffset .. "\t"
    local str = ""
    str = str .. "{" .. "\n"
    for k, v in pairs(tab) do
         if type(v)=="table" then
             str = str .. propertyOffset.. k .. " = " .. Utils:Table2String(v, propertyOffset) .. ",\n"
         else
             str = str .. propertyOffset.. k .. " = '" .. v .. "',\n"
         end
    end
    str = string.sub(str, 1, string.len(str) - string.len(",\n")) .. "\n"
    str = str .. parentOffset .. "}"
    return str
end

function Utils:TableDeepCopy(tSrcTable)
    local tSearchTable = {}

    local function _copy(tSrcTable)
        if type(tSrcTable) ~= "table" then
            return tSrcTable
        end
        local tNewTable = {}
        tSearchTable[tSrcTable] = tNewTable
        for k, v in pairs(tSrcTable) do
            tNewTable[_copy(k)] = _copy(v)
        end

        return setmetatable(tNewTable, getmetatable(tSrcTable)) 
    end

    return _copy(tSrcTable)
end

function Utils:TimeNow()
    local nTimeNow = os.time()
    local szTimeFormat = os.date("%Y-%m-%d %H:%M:%S", nTimeNow)
    return nTimeNow, szTimeFormat
end

function Utils:FormatHMS(nTime)
    local nHour     = 0
    local nMin      = 0
    local nSecond   = 0

    nHour = math.floor(nTime / 3600)
    nMin  = math.floor((nTime - nHour * 3600) / 60)
    nSecond = math.floor(nTime % 60)

    local szTime = ""
	szTime = szTime .. string.format("%02d:", nHour)
	szTime = szTime .. string.format("%02d:", nMin)
	szTime = szTime .. string.format("%02d", nSecond)

    return szTime
end

function Utils:Clamp(value, nMaxValue, nMinValue)
    if value < nMinValue then
        return nMinValue
    end

    if value > nMaxValue then
        return nMaxValue
    end

    return value
end

do -- 队列
    local QueueLogic = {}

    function QueueLogic:Length()
        return self.last - self.first
    end

    function QueueLogic:Enqueue(val)
        self[self.last] = val
        self.last = self.last + 1
    end

    function QueueLogic:Dequeue()
        if (self:Length() <= 0) then
            return nil
        end

        local ret = self[self.first]
        self[self.first] = nil
        self.first = self.first + 1
        return ret
    end

    function QueueLogic:Peek()
        if (self:Length() <= 0) then
            return nil
        end
        return self[self.first]
    end

    function Utils:CreateQueue()
        local queue = {}
        setmetatable(queue, {__index = QueueLogic})
        queue.first = 0
        queue.last =  0

        return queue
    end
end

return Utils