local base = _G

module ('videomorecore')

function getSeason(session, answer)
	local t, i = {}, 1
	for w in base.string.gmatch(answer, '<div class="car%-desc">(.-)</div>') do
		--<a href="/angel_ili_demon/1_sezon/20_seriya" title="Ангел или демон 20-я серия">Ангел или демон 20-я серия</a>
		local adr, name = base.string.match(w, 'href="(.-)" title="(.-)"')
		t[i] 		= {}
		t[i].Id		= i
		t[i].Name	= name
		t[i].Adress	= 'http://videomore.ru' .. adr
		--debug_in_file(t[i].Name .. '	' .. t[i].Adress .. '\n\n', 'C:\\debug.txt')
		i = i + 1
	end
	local nextpage = base.string.match(answer, '<a class="next_page" rel="next" href="(.-)"')
	if nextpage ~= nil then 
		nextpage = 'http://videomore.ru' .. nextpage
		while true do
			--debug_in_file(nextpage .. '\n\n', 'C:\\debug.txt')
			rc, answer = base.m_simpleTV.WinInet.Get(session, nextpage)
			if rc == 200 then
				answer = base.m_simpleTV.Common.string_fromUTF8(answer)
				for w in base.string.gmatch(answer, '<div class=\\"car%-desc\\">(.-)<\\/div>') do
					local adr, name = base.string.match(w, 'href=\\"(.-)\\" title=\\"(.-)\\"')
					t[i] 		= {}
					t[i].Id		= i
					t[i].Name	= name
					t[i].Adress	= 'http://videomore.ru' .. adr
					--debug_in_file(t[i].Name .. '	' .. t[i].Adress .. '\n\n', 'C:\\debug.txt')
					--debug_in_file(w .. '\n\n', 'C:\\debug.txt')
					i = i + 1
				end
				nextpage = base.string.match(answer, '<a class=\"next_page" rel=\"next\" href=\"(.-)\"')
				if nextpage == nil then break end
			end
		end
	end
	return t
end

function getSeries(inAdr, serii, answer)
	local t, i = {}, 1
	local w = base.string.match(answer, '<div class="car%-desc">(.-)</div>')
	w = 'http://videomore.ru' .. base.string.match(w, 'href="(.-)"')
	local _, prefix = base.string.match(w, '/(%d-)_(.-)$')
	if prefix == 'vypusk' then w = 'Выпуск ' end
	--debug_in_file(answer, 'C:\\debug.txt')
	local b, e = base.string.match(serii, '(%d-)%-(%d+)$')
	if b ~= nil and e ~= nil then
		for n = b, e, 1 do
			t[i] = {}
			t[i].Id     = i
			t[i].Name   = w .. n
			t[i].Adress = inAdr .. '/' .. n .. '_' .. prefix
			i = i + 1
			--debug_in_file(adr .. '\n\n', 'C:\\_TEMP\\debug.txt')
		end
	end
	return t
end