-- ScriptCommonUtility.lua
-- Cross add-on utility functions
-- 11:36 10.12.2008
--------------------------------------------------------------------------------
-- COMMON UTILITY
--------------------------------------------------------------------------------
function IsWs( text )
	return common.IsWString( text )
end
--------------------------------------------------------------------------------
function IsEWs( text )
	return common.IsEmptyWString( text )
end
--------------------------------------------------------------------------------
function GetEWs()
	return common.GetEmptyWString()
end
--------------------------------------------------------------------------------
function ToWs( text )
	if common.HasDebugLibs() then
		return debugCommon.ToWString( text )
	else
		return common.GetEmptyWString()
	end
end
--------------------------------------------------------------------------------
function GetEVt()
	return common.CreateValuedText()
end
--------------------------------------------------------------------------------
function IsVt( text )
	return common.IsValuedText( text )
end
--------------------------------------------------------------------------------
function FromWs( text )
	if not common.HasDebugLibs() then
		return ""
	else
		if text == nil then
			return "nil value"
		
		elseif not IsWs( text ) then
			return "not a WString"

		elseif IsEWs( text ) then
			return "empty WString"
		end
		return debugCommon.FromWString( text )
	end
end
--------------------------------------------------------------------------------
function IsValidWs( text )
	return text ~= nil and IsWs( text ) and not IsEWs( text )
end
--------------------------------------------------------------------------------
function ValidateWs( text )
	if not common.IsFinalRelease() then
		local format
		if text == nil then
			format = common.GetStateRelatedText( "DefaultNilValue" )
		
		elseif not IsWs( text ) then
			format = common.GetStateRelatedText( "DefaultNotaWString" )
			
		elseif IsEWs( text ) then
			format = common.GetStateRelatedText( "DefaultEmptyWString" )
		end
	
		if format ~= nil then
			text = common.CreateValuedText()
			text:SetFormat( format )
		end
	else
		text = IsValidWs( text ) and text or GetEWs()
	end
	return text
end
--------------------------------------------------------------------------------
function IsValidTextValue( text )
	return text ~= nil and ( IsWs( text ) and not IsEWs( text ) or IsVt( text ) )
end
--------------------------------------------------------------------------------
function ValidateTextValue( text )
	if not common.IsFinalRelease() then
		local format
		if text == nil then
			format = common.GetStateRelatedText( "DefaultNilValue" )
		
		elseif not IsVt( text ) then
			if not IsWs( text ) then
				format = common.GetStateRelatedText( "DefaultNotaWString" )
			
			elseif IsEWs( text ) then
				format = common.GetStateRelatedText( "DefaultEmptyWString" )
			end
		end
	
		if format ~= nil then
			text = common.CreateValuedText()
			text:SetFormat( format )
		end
	else
		text = IsValidTextValue( text ) and text or GetEWs()
	end
	return text
end
--------------------------------------------------------------------------------
function GetNameByPrefix( prefix, id )
	return prefix .. string.format ( "%02d", id )
end
--------------------------------------------------------------------------------
function GetPrefixFromName( name )
	return string.sub( name, 1, string.len( name ) - 2 )
end
--------------------------------------------------------------------------------
function HasNamePrefix( name, prefix )
	return string.sub( name, 1, string.len( prefix ) ) == prefix
end
--------------------------------------------------------------------------------
function GetIndexFromName( name )
	return tonumber( string.sub( name, - 2 ) )
end
--------------------------------------------------------------------------------
function GetPrefixBySuffix( name, suffix )
	return string.sub( name, 1, string.len( name ) - string.len( suffix ) )
end
--------------------------------------------------------------------------------
function GetSuffixByPrefix( name, prefix )
	return string.sub( name, string.len( prefix ) - string.len( name ) )
end
--------------------------------------------------------------------------------
function GetText( textId )
	return common.GetAddonRelatedText( textId )
end
--------------------------------------------------------------------------------
function GetTextFrom( group, textId )
	return common.GetAddonRelatedGroupText( group, textId )
end
--------------------------------------------------------------------------------
function GetTexture( textureId )
	return common.GetAddonRelatedTexture( textureId )
end
--------------------------------------------------------------------------------
function GetTextureFrom( group, textureId )
	return common.GetAddonRelatedGroupTexture( group, textureId )
end
--------------------------------------------------------------------------------
function Clone( value )
	if type( value ) == "table" then
		local new = {}
		for id, val in value do
			new[ id ] = Clone( val )
		end
		return new
	else
		return value
	end
end
--------------------------------------------------------------------------------
function IsEqual( value01, value02 )
	if type( value01 ) == "table" and type( value02 ) == "table" then
		local match = true

		for id, value in value01 do
			match = IsEqual( value, value02[ id ] )
			if match == false then break end
		end
		return match
		
	elseif IsWs( value01 ) and IsWs( value02 ) then
		return common.CompareWString( value01, value02 ) == 0

	else
		return value01 == value02
	end
end
--------------------------------------------------------------------------------
function IsTable( tab )
	return type( tab ) == "table"
end
--------------------------------------------------------------------------------
function IsEmptyTable( tab )
	for id, value in tab do
		return false
	end
	return true
end
--------------------------------------------------------------------------------
function SearchInTable( tab, entry )
	if not ( IsTable( entry ) and IsEmptyTable( entry ) ) then
		for id, value in tab do
			if IsEqual( entry, value ) then
				return id, value
			end
		end
	end
	return nil
end
--------------------------------------------------------------------------------
function CountInTable( tab, entry )
	if not ( IsTable( entry ) and IsEmptyTable( entry ) ) then
		local counter = 0
		for id, value in tab do
			if IsEqual( entry, value ) then
				counter = counter + 1
			end
		end
		return counter
	end
end
--------------------------------------------------------------------------------
function RandomFromTable( tab )
	if IsTable( tab ) and not IsEmptyTable( tab ) then
		local length = 0
		local set = {}
		
		for id, value in tab do
			length = length + 1
			table.insert( set, id )
		end
	
		local id = set[ common.GetRandInt( 1, length ) ]
		return id, tab[ id ]
	else
		return 0, nil
	end
end
--------------------------------------------------------------------------------
function SetupWString( widget, params, style )
	if IsVt( params ) then
		widget:SetValuedText( params )
	
	elseif type( params ) == "table" then
		if IsValidWs( params.format ) then
			widget:SetFormat( params.format )
		end
		
		style = style ~= false
	
		if style then
			params.multiline = params.multiline ~= false
			widget:SetMultiline( params.multiline )
			widget:SetWrapText( params.multiline )
			widget:SetEllipsis( not params.multiline )
			widget:ShowClippedLine( not params.multiline )
			widget:ShowClippedSymbol( not params.multiline )
		end
		
		for id, value in params do
			if type( value ) == "string" then
				widget:SetClassVal( id, value )

			elseif IsWs( value ) or IsVt( value ) and id ~= "format" then
				widget:SetVal( id, value )
			end
		end
		
	else
		LogError( "SetupWString - invalid parameters passed" )
	end
end
--------------------------------------------------------------------------------
function SplitTime( milliseconds )
	local timer = {}
	timer.hours = milliseconds / 3600000
	milliseconds = milliseconds - math.floor( timer.hours ) * 3600000
	
	timer.minutes = milliseconds / 60000
	milliseconds = milliseconds - math.floor( timer.minutes ) * 60000

	timer.seconds = milliseconds / 1000
	timer.milliseconds = milliseconds - math.floor( timer.seconds ) * 1000
	return timer
end
--------------------------------------------------------------------------------
