-- vim: ft=terra
local pstr = lib.str.t
local modes = lib.enum [[follow mutual local fedi circle]]
local terra 
requires_login(m: modes.t): bool
	return m == modes.follow
	    or m == modes.mutual
	    or m == modes.circle
end

local terra 
render_timeline(co: &lib.srv.convo, hpath: lib.mem.ptr(lib.mem.ref(int8)))
	var modestr = lib.str.ref.null()
	var spec    = lib.str.ref.null()
	if hpath.ct >= 2 then
		modestr = hpath(1)
		if hpath.ct >= 3 then spec = hpath(2) end
	end
	var mode = modes.follow
	var circle: uint64 = 0
	if modestr:ref() then
		if     modestr:cmp('local' ) then mode = [modes['local']]
		elseif modestr:cmp('mutual') then mode = modes.mutual
		elseif modestr:cmp('fedi'  ) then mode = modes.fedi
		elseif modestr:cmp('circle') then mode = modes.circle
		end
	end
	if requires_login(mode) and co.aid == 0 then mode = [modes['local']] end


	var stoptime = lib.osclock.time(nil)

	var posts = [lib.mem.vec(lib.mem.ptr(lib.store.post))] { 
		sz = 0, run = 0
	}
	var fetchmode = lib.store.range {
		mode = 1; -- T->I
		from_time = stoptime;
		to_idx = 64;
	}
	var circ: lib.mem.ptr(lib.store.circle) = nil
	if mode == modes.follow or mode == modes.mutual then
		posts = co.srv:timeline_actor_fetch_uid(co.who.id,fetchmode)
	elseif mode == [modes['local']] then
		posts = co.srv:timeline_instance_fetch(fetchmode)
	elseif mode == modes.fedi then
	elseif mode == modes.circle and spec:ref() then
		var cid, ok = lib.math.shorthand.parse(spec.ptr,spec.ct)
		if ok then
			circ = co.srv:circle_search(&co.srv.pool,co.who.id,cid)
			if circ.ct == 1 then
				posts = co.srv:timeline_circle_fetch(cid,fetchmode)
			end
		end
	end

	var acc = co:stra(1024)
	var modelabels = arrayof(pstr, '<u>f</u>ollowed', 'm<u>u</u>tuals', '<u>l</u>ocal instance', 'fedi<u>v</u>erse', 'ci<u>r</u>cle')
	var keybinds = arrayof(pstr, 'f', 'u', 'l', 'v', 'r')
	var modelinks = arrayof(pstr, [modes.members])
	acc:lpush('<div class="kind-picker">showing ')
	for i=0, [modelabels.type.N] do
		if co.aid ~= 0 or not requires_login(i) then
			if i > 0 then acc:lpush(' · ') end
			if i == mode and not circ then
				acc:lpush('<strong>'):ppush(modelabels[i]):lpush('</strong>')
			else
				acc:lpush('<a href="/tl/'):ppush(modelinks[i]):lpush('" accesskey="'):ppush(keybinds[i]):lpush('">')
				if i == mode and circ:ref() then
					acc:lpush'<strong>':ppush(modelabels[i]):lpush'</strong> (':ppush(circ().name):lpush(')')
				else
					acc:ppush(modelabels[i])
				end
				acc:lpush('</a>')
			end
		end
	end
	acc:lpush('</div>')
	var newest: lib.store.timepoint = 0
	if mode == modes.circle and not spec then
		var circles = co.srv:circle_search(&co.srv.pool, co.who.id, 0)
		acc:lpush '<menu class="circles">'
		for i:intptr = 0, circles.ct do
			acc:lpush '<a href="/tl/circle/'
			   :shpush(circles(i).cid)
			if i <= 10 then
				acc:lpush '" accesskey="':ipush((i+1) % 10)
			end
			acc:lpush '">'
			   :ppush(circles(i).name)
			   :lpush '</a>'
		end
		-- TODO list circles
		acc:lpush '</menu>'
	else
		acc:lpush('<div id="tl" data-live="10">')
		for i = 0, posts.sz do
			var author = co:uid2actor(posts(i).ptr.author)
			if mode == modes.mutual and posts(i).ptr.author ~= co.who.id then
				if not author.relationship.recip.follow() then goto skip end
			end
			if author.relationship.rel.mute() or 
			   author.relationship.rel.avoid() or 
			   author.relationship.recip.exclude() then goto skip end
			if posts(i).ptr.rtdby ~= 0 then
				var rter = co:uid2actor(posts(i).ptr.rtdby)
				if rter.relationship.rel.mute()
				or rter.relationship.rel.attenuate()
				or rter.relationship.rel.avoid()
				or rter.relationship.recip.exclude() then goto skip end
			end
			lib.render.tweet(co, posts(i).ptr, &acc)
			var t = lib.math.biggest(lib.math.biggest(posts(i).ptr.posted, posts(i).ptr.discovered),posts(i).ptr.edited)
			if t > newest then newest = t end
			::skip:: posts(i):free()
		end
		if posts.run > 0 then posts:free() end
		acc:lpush('</div>')
	end

	var doc = [lib.srv.convo.page] {
		title = 'timeline';
		body = acc:finalize();
		class = 'timeline';
		cache = false;
	}
	if newest ~= 0
		then co:livepage(doc,newest)
		else co:stdpage(doc)
	end
	--doc.body:free()
end
return render_timeline
