-- vim: ft=terra
local pstr = lib.mem.ptr(int8)
local terra cs(s: rawstring)
	return pstr { ptr = s, ct = lib.str.sz(s) }
end

local terra 
push_promo_header(co: &lib.srv.convo, acc: &lib.str.acc, rter: &lib.store.actor, rid: uint64)
	acc:lpush('<div class="lede"><div class="promo"><img src="'):push(rter.avatar,0):lpush('"><a href="/')
	if rter.origin ~= 0 then acc:lpush('@') end
	acc:push(rter.xid,0):lpush('" class="username">')
	lib.render.nym(rter, 0, acc, true)
	acc:lpush('</a> retweeted</div>')
	if co.aid ~= 0 and co.who.id == rter.id then
		acc:lpush('<a href="/post/'):shpush(rid):lpush('/del" class="del">✖</a>')
	end
end
			
local terra 
render_tweet(co: &lib.srv.convo, p: &lib.store.post, acc: &lib.str.acc)
	var author: &lib.store.actor = nil
	var retweeter: &lib.store.actor = nil
	for j = 0, co.actorcache.top do
		if p.author == co.actorcache(j).ptr.id then author    = co.actorcache(j).ptr end
		if p.rtdby  == co.actorcache(j).ptr.id then retweeter = co.actorcache(j).ptr end
		if author ~= nil and (p.rtdby == 0 or retweeter ~= nil) then
			goto foundauth
		end
	end
	if author == nil then
		author = co.actorcache:insert(co.srv:actor_fetch_uid(p.author)).ptr
	end
	if p.rtdby ~= 0 and retweeter == nil then
		retweeter = co.actorcache:insert(co.srv:actor_fetch_uid(p.rtdby)).ptr
	end

	::foundauth::
	var timestr: int8[26] lib.osclock.ctime_r(&p.posted, &timestr[0])
	for i=0,26 do if timestr[i] == @'\n' then timestr[i] = 0 break end end -- 🙄

	var bhtml = lib.smackdown.html([lib.mem.ptr(int8)] {ptr=p.body,ct=0},false)
	defer bhtml:free()

	var idbuf: int8[lib.math.shorthand.maxlen]
	var idlen = lib.math.shorthand.gen(p.id, idbuf)
	var permalink: lib.str.acc permalink:compose('/post/',{idbuf,idlen})
	var fullname = lib.render.nym(author,0,nil, false) defer fullname:free()
	var tpl = data.view.tweet {
		text = bhtml;
		subject = cs(lib.coalesce(p.subject,''));
		nym = fullname;
		when = cs(&timestr[0]);
		avatar = cs(author.avatar);
		acctlink = cs(author.xid);
		permalink = permalink:finalize();
		attr = pstr{'',0};
		stats = pstr{'',0};
		extra = pstr{'',0};
	}
	if p.isreply then
		var parent = co.srv:post_fetch(p.parent) defer parent:free()
		if not parent then
			lib.bail('schema integrity violation - could not match post to parent')
		end
		var pauth = co.srv:actor_fetch_uid(parent(0).author) defer pauth:free()
		var pidbuf: int8[lib.math.shorthand.maxlen]
		var pidlen = lib.math.shorthand.gen(p.parent, pidbuf)
		var pa: lib.str.acc pa:init(128)
		pa:lpush('<small>in reply to <a class="username" href="/post/'):push(&pidbuf[0],pidlen):lpush('">')
		lib.render.nym(pauth.ptr,0,&pa,true)
		pa:lpush('</a></small>')
		tpl.extra = pa:finalize()
	end
	if p.rts + p.likes > 0 then
		var s: lib.str.acc s:init(128)
		s:lpush('<div class="stats">')
		if p.rts   > 0 then s:lpush('<div class="rt">'  ):dpush(p.rts  ):lpush('</div>') end
		if p.likes > 0 then s:lpush('<div class="like">'):dpush(p.likes):lpush('</div>') end
		s:lpush('</div>')
		tpl.stats = s:finalize()
	end

	var attrbuf: int8[64]
	var attrcur = &attrbuf[0]
	if p.accent ~= -1 and p.accent ~= co.ui_hue then
		var hdecbuf: int8[21]
		var hdec = lib.math.decstr(p.accent, &hdecbuf[20])
		attrcur = lib.str.cpy(attrcur,' style="--hue:')
		attrcur = lib.str.cpy(attrcur, hdec)
		-- var len = &hdecbuf[20] - hdec 
		attrcur = lib.str.cpy(attrcur, '"')
	end
	if co.aid ~= 0 and p.author == co.who.id then attrcur = lib.str.cpy(attrcur, ' data-own') end
	if retweeter ~= nil then attrcur = lib.str.cpy(attrcur, ' data-rt') end

	if attrcur ~= &attrbuf[0] then tpl.attr = &attrbuf[0] end

	defer tpl.permalink:free()
	if acc ~= nil then
		if retweeter ~= nil then push_promo_header(co, acc, retweeter, p.rtact) end
		tpl:append(acc)
		if retweeter ~= nil then acc:lpush('</div>') end
		if p.rts + p.likes > 0 then tpl.stats:free() end
		if tpl.extra.ct > 0 then tpl.extra:free() end
		return [lib.mem.ptr(int8)]{ptr=nil,ct=0}
	end

	if retweeter ~= nil then
		var rta: lib.str.acc rta:init(512)
		push_promo_header(co, &rta, retweeter, p.rtact)
		tpl:append(&rta) rta:lpush('</div>')
		if tpl.extra.ct > 0 then tpl.extra:free() end
		return rta:finalize()
	else
		var txt = tpl:tostr()
		if tpl.extra.ct > 0 then tpl.extra:free() end
		if p.rts + p.likes > 0 then tpl.stats:free() end
		return txt
	end
end
return render_tweet
