
.namespace VideoSubtitleManager

SubtitleInitialPos = 
{
    x= 512,
    y= 630,
}

ShiftYForVideo = 
{
    cinematic_outro = -28,
    presentation    = -28,
    cs001 = 30,    
}

MAX_LINE_LENGTH = 65

g_timer = nil
g_videoName = nil
g_currentLine = 1
g_displaying = false
g_totalTime = -100
g_totalLines = 0
g_currentData = nil
g_guiText = nil
g_curBegin = 0
g_curEnd = 0

function OnEnable()
	
end

function loadSubtitleForVideo(_videoName)
	
	AssertPopup(type(_videoName) == "string", "A video name has to be provided in order to load its subtitles")

	
	OPLog.Log("loadsubtitle for video ".._videoName)
	local lang = DebugOptions.curlanguage
	
	local file = 'media/video/'.._videoName.."_"..lang..'.lua'

	VideoSubtitles = nil
	OPLua.Include(file)

	AssertPopup(VideoSubtitles ~= nil, "Subtitle file " .. file .. " is not valid")

	g_videoName = _videoName
	
	local shifty
	if ShiftYForVideo[_videoName] ~= nil then
		shifty=ShiftYForVideo[_videoName]
	else
		shifty=0
	end

	g_currentLine = 1
	g_currentData = VideoSubtitles[1]
	g_curBegin = convertTimeToMs(g_currentData[1])
	g_curEnd = convertTimeToMs(g_currentData[2])
	g_totalTime = -100
	g_displaying = false
	g_totalLines = table.getn(VideoSubtitles)
	
	SubtitlesDisplay.Enabled = true
end

function goodNumber2Digits(_number)
	return string.format("%02d", tonumber(_number))
end

function goodNumber3Digits(_number)
	return string.format("%03d", tonumber(_number))
end

function convertMsToTime(_millisecond)
	local ms = _millisecond
	local s = math.floor(_millisecond / 1000)
	ms = ms - (s * 1000)
	local m = math.floor(s / 60)
	s = s - (m * 60)
	local h = math.floor(m / 60)
	m = m - (h * 60)
	return '' .. goodNumber2Digits(h) .. ':' .. goodNumber2Digits(m) .. ':' .. goodNumber2Digits(s) .. ':' .. goodNumber3Digits(ms)
end

function convertTimeToMs(_time)
	local arr = OPUnitFactory.GetUnit("GlobalFunctions","Unit").UF_explode(':', _time)
	local ms = 0
	ms = ms + arr[4]
	ms = ms + (arr[3] * 1000)
	ms = ms + (arr[2] * 60000)
	ms = ms + (arr[1] * 3600000)
	return ms
end

function OnUpdate(dt)
	if VideoSubtitles == nil then return end
	-- Increment time
	g_totalTime = g_totalTime + 1000 * dt
	
	if g_currentData == nil then
		return
	end
	
	if (g_displaying == false) then
		if (g_totalTime >= g_curBegin) then
			g_displaying = true
			

			OPLog.Log("on affiche "..tostring(g_currentData[3]))
			SubtitlesDisplay.Enabled = true
			
			--OPLog.Log("on affiche "..g_currentData[3])
			SubtitlesDisplay.DisplaySubtitlesForSound(nil,(g_curEnd - g_curBegin) /1000 , g_currentData[3],true)

			
		end
	
	elseif (g_totalTime >= g_curEnd) then
		g_displaying = false
		g_currentLine = g_currentLine + 1
		
		
		OPLog.Log("on cache "..g_currentData[3])
		
		--SubtitlesDisplay.HideSubtitles()
		VideoText.Text = ""
		if not (g_currentLine > g_totalLines) then
			g_currentData = VideoSubtitles[g_currentLine]
			g_curBegin = convertTimeToMs(g_currentData[1])
			g_curEnd = convertTimeToMs(g_currentData[2])
		else
			g_curBegin = 9999999999999
		end
	end
end

