#1 Will be implemented #2 is just for reference

#1 Use GetQuestResetTime to calculate and store the time when daily quest data will become invalid
+ after a server reset a newly logged in client will clean it's invalid data
+ compatible with older addon versions - although it adds a timestamp to each category

m_grace_period = 60
m_reset_time = 0

OnLogin
	load m_reset_time
	login_time = time()
	if m_reset_time > login_time
		-- Daily quest count hasn't changed since last login, therefore saved data is still valid
		load daily_quests
		mark each set category as uncertain
		init Viewer with daily_quests
	end
	SetTimer reset_timer 0
	request daily_quests
end

OnLogout
	save daily_quests
end

OnTimer reset_timer
	-- The reset timer managment asures that even if there is a lag
	-- the quests will be correctly reset at the QuestResetTime and the cache
	-- will not get dirty because of received information shortly before or after
	-- the QuestResetTime. This is done by defining a grace period before and after
	-- the QuestReset were no received quest information will be stored.
	-- The time until the next QuestReset will be divided into halfs. It's only waited
	-- until half the time is over. This process is repeated until the grace
  -- period is reached. By this we asure that the QuestReset time is hit on
	-- the client precisely at the time it is hit on the server - even if the clients
	-- system clock is ticking differntly as the servers clock
	-- Chose the grace time high enough that _defintely_ no one will process
	-- outdated information

	server_time = time()
	remaining_time = m_reset_time - server_time

	seconds = 0
	if remaining_time <= -m_grace_period
		-- grace period after daily quest reset reached, reactivate updates, start new wait period
		lock from updates
		delete daily_quests
		clear Viewer
		unlock for updates
		m_reset_time = server_time + GetQuestResetTime
		save m_reset_time
		seconds = 0
	else if remaining_time <= 0
		-- daily quest reset reached, reset all data
		lock from updates
		delete daily_quests
		clear Viewer
		seconds = m_grace_period + remaining_time
	else if remaining_time <= m_grace_period
		-- grace period before daily quest reset reached, deactivate updates
		lock from updates
		seconds = remaining_time
	else if remaining_time / 2 <= m_grace_period
		-- near grace period before daily quest reset, just wait long enough to hit
		-- the grace period with the next timer
		seconds = remaining_time - m_grace_period
	else
		-- enough time until next daily quest reset, divide the period into halfs
		-- and wait until the first half is over
		seconds = remaining_time / 2
	end
	SetTimer reset_timer seconds

end

OnGossipShow / OnQuestDetail
	mark category as certain and store confirmation_time
end

OnSendDailyResponse
	add certain mark and confirmation_time of category to outgoing message
end

OnReceiveDailyResponse
	if received quest certain mark
		if stored_category confirmation_mark == uncertain
			|| (stored_category confirmation_mark == certain && received category confirmation_time >= stored category confirmation_time )
			update stored quest
			remove received quest from send queue
		end
	else if stored quest certain mark == uncertain && received category confirmation_time >= stored category confirmation_time
		update stored quest
		remove received quest from send queue
	end
end




==================================================================================




#2 Using GetDailyQuestsCompleted
+ relativley easy to implement
+ compatible with older addon versions
- in case GetDailyQuestsCompleted is 0 on login, all information is discarded
- after a server reset a newly logged in client will mess up all others data if he decides to broadcast

OnLogin
	dailies_completed = GetDailyQuestsCompleted
	if dailies_completed != 0
		-- Daily quest count hasn't changed since last login, therefore saved data is still valid
		load daily_quests
		broadcast daily_quests
		-- in case we get our own broadcast we don't need to do Viewer init here
		init Viewer with daily_quests
	else
		-- Daily quest count has changed since last login, therefore saved data is invalid
		delete daily_quests
	end
	-- Ask the others for their known dailies
	request daily_quests
end

OnLogout
	save daily_quests
end

OnTimer
	if dailies_completed != GetDailyQuestsCompleted
		-- a reset of the daily quest count occured, therefore all collected data is invalid
		delete daily_quests
		clear Viewer
		dailies_completed = 0
	end
end
OnQuestComplete / OnQuestLogUpdate
	if dailies_completed < GetDailyQuestsCompleted
		dailies_completed = GetDailyQuestsCompleted
	end
end

OnIncomingBroadcast
	if daily_quests contained in received_quests
		add received_quests to daily_quests
		update Viewer
	else if daily_quests disagree on received_quests
		delete daily_quests
		clear Viewer
		dailies_completed = 0
	end
end
