--[[ 
	This quest send the player a letter with Information about new Stuff
	At the Moment we have 3 possible options:
	1. Happy Hour in the shop
	2. New stuff in the shop
	3. security announce
	
	Concept side: https://wiki.gameforge.de/bin/view/MMO/Metin2/ConceptIngameAnnounce
	Workflow ticket: https://mantis-i.gameforge.de/view.php?id=76862
]]

quest ingame_message begin
	state start begin

		-- return the unique message id for campaign	
		function get_message_id()
			return (game.get_event_flag("message_id"))
		end --end function get_message_id

		-- return which kind of message appear	
		function get_message_type()
			return (game.get_event_flag("message_type"))
        end --end function get_message_type
		
		-- return the displayed item vnum	
		function get_announce_vnum()
			return (game.get_event_flag("message_item_vnum"))
		end --end function get_announce_vnum
	
		-- return the vnum that can is the gift	
		function get_gift_vnum()
			return (game.get_event_flag("message_gift_vnum"))
		end --end function get_gift_vnum
		
		--[[ 
			Provide a duration time to have a option to show options often
			Value should be hours ;)
		]]
		function get_message_duration()
			local duration = game.get_event_flag("message_duration")
			-- if not duration is set, we will implement the standard 24h duration
			if duration == 0 then
				duration = 24
			end
			
			return duration
		end --end function get_message_id
		
		when info or button begin
			local message_type = ingame_message.get_message_type()
			if message_type == 1 then
				--local happyhour_start = game.get_event_flag("message_happyhour_start")
				--local happyhour_end = game.get_event_flag("message_happyhour_end")
				--say_size(250, 250)
				-- say_show_item(70029)
				-- Eine günstige Gelegenheit
				say_important_title(gameforge.ingame_message._010_sayTitle)
				--say_important(string.format("Heute von %s bis %s Happy Hour!", happyhour_start, happyhour_end))
				say("")
				-- Eine neue Aktion ist im Item-Shop gestartet! Verpasse nicht diese günstige Gelegenheit und besuche direkt den Item-Shop.
				say_important(gameforge.ingame_message._020_say)
				local c =  select(gameforge.locale.guild.yes, gameforge.locale.guild.no)
        if 2 == c then
        	return
        else
          game.open_web_mall()
        end
			elseif message_type == 2 then
				local new_item_vnum = ingame_message.get_announce_vnum()
				say_important_title(gameforge.ingame_message._030_sayTitle)
				say_item_vnum(new_item_vnum) -- icon of item
				say_important(gameforge.ingame_message._040_say)
				-- We will provide the user the option to select the gift
				local gift_vnum = ingame_message.get_gift_vnum()
				local message_id = ingame_message.get_message_id()
				local gift_check = pc.getqf("gift_" .. message_id .. "_" ..gift_vnum)
				
				if gift_check < 1 and gift_vnum != 0 then
					wait()
					-- Ein Geschenk der Götter
					say_title(gameforge.important_message._050_sayTitle)
					say(gameforge.ingame_message._060_say)
					local gift = select(gameforge.ingame_message._070_select, gameforge.ingame_message._080_select)
					if gift == 1 then
						pc.give_item2(gift_vnum, 1)
						pc.setqf("gift_"..message_id.."_"..gift_vnum, 1)
					end -- gift vergeben
				end -- gift check
				
			else
				say_important_title("GM News")
				say_important("Du wirst nie nach Login gefragt")
			end
		end --endbegin letter
		
		when login with ingame_message.get_message_type() > 0 begin
			local message_type = ingame_message.get_message_type()
			local duration_time = ingame_message.get_message_duration()
			
			if get_time() > pc.getqf("duration") then
				pc.setqf("duration",get_time()+60*60*duration_time)--------------24 Stunden
				if message_type == 1 then
					send_letter(gameforge.ingame_message._010_sayTitle)
				elseif message_type == 2 then
					send_letter(gameforge.ingame_message._030_sayTitle)
				else 
					send_letter("GM News")
				end -- endif 
			end -- endif duration
		end -- endif
    end -- endbegin state start
end -- endbegin quest