--News from Big Land[0.5beta] by Debrovski
-------------------------------------
local ffi
local rssPlugin

function on_game_start()
	RegisterScriptCallback("on_game_load",on_game_load)
end

function on_game_load()
	ffi = require("ffi")
	rssPlugin = ffi.load("newsFromBigLandAddon")
	ffi.cdef[[
	int    getStatus();
	char*  getText();
	void startDownloading();
	]]
	rssPlugin.startDownloading();
	CreateTimeEvent("newsFromBigLand","updateTime",60,update)
	if(alife_storage_manager.get_state().addon_news_from_big_land_last_message==nil) then
		alife_storage_manager.get_state().addon_news_from_big_land_last_message="NONE";
	end
		
end

function update()
	local status = rssPlugin.getStatus()
	if(status==0) then     --| 0 -> still downloading
		set_timer(10)
	elseif(status==1) then --| 2 -> ready
		local currNewsTitle = ffi.string(rssPlugin.getText())

		
		if(currNewsTitle==alife_storage_manager.get_state().addon_news_from_big_land_last_message) then --old news, just refresh
			rssPlugin.startDownloading()
			set_timer(60)
		else
			news_manager.send_tip(db.actor, "News from the Big Land. "..currNewsTitle, nil, "tourist", nil, nil)
			rssPlugin.startDownloading()
			set_timer(300)
		end
		alife_storage_manager.get_state().addon_news_from_big_land_last_message=currNewsTitle
	elseif(status==2) then --| 1 -> error
			news_manager.send_tip(db.actor, "News from the Big Land. ".."Source connection error, will try again later", nil, "tourist", nil, nil)
			rssPlugin.startDownloading()
			set_timer(300)
	end

end

function set_timer(delay)
	ResetTimeEvent("newsFromBigLand","updateTime",delay)
end