freeslot("sfx_drtalk", "sfx_aqua", "sfx_shady")

local dialogue = {
	NEC1 = {
		[1] = {
			dialoguetext = "There's a huge reptile relaxing on a beach chair.",
		},
		[2] = {
			dialoguetext = "It seems that they are quite a heavy sleeper. At least no one else can notice it with the sunglasses on.",
		},
		[3] = {
			dialoguetext = "...",
		},
		[4] = {
			dialoguetext = "They don't seem to wake up any time soon.",
		}
	},
	NEC2 = {
		[1] = {
			dialoguetext = "Meow w.i.p 1 :3",
		},
		[2] = {
			dialoguetext = "Meow w.i.p 2 :3",
		}
	},
	AQUA = {
		[1] = {
			sprite = "AQUA2",
			dialoguetext = "Woah! You went pretty fast down there...",
			sound = sfx_aqua
		},
		[2] = {
			sprite = "AQUA3",
			dialoguetext = "I probably wouldn't notice you at all if I yawned at the perfect time of you zooming by...",
			sound = sfx_aqua
		},	
		[3] = {
			sprite = "AQUA4",
			dialoguetext = "Either way, I'm glad you decided to stop by despite all the rush. I hope you'll enjoy our borgors!",
			sound = sfx_aqua
		},	
		[4] = {
			sprite = "AQUA5",
			dialoguetext = "And I know that this is a bit quick, but what do you think of this spot so far?",
			sound = sfx_aqua
		},
		[5] = {
			sprite = "AQUA5",
			dialoguetext = "Is it good? Or... is it not quite there yet for a 5 star experience?",
			sound = sfx_aqua
		},	
		[6] = {
			sprite = "AQUA4",
			dialoguetext = "Because we're always open for critique AND improvement 24/7!",
			sound = sfx_aqua
		},
		[7] = {
			sprite = "AQUA6",
			dialoguetext = "But... I'm not actually that good about taking criticism...",
			sound = sfx_aqua
		},
		[8] = {
			sprite = "AQUA7",
			dialoguetext = "So... maybe share your ideas with "+"\x86"+"Shady\x80 instead? Be gentle with them though...",
			sound = sfx_aqua
		},
		[9] = {
			sprite = "AQUA2",
			dialoguetext = "They once lobbed a melting hot burger at a guy when he was a bit too critical...",
			sound = sfx_aqua
		},
		[10] = {
			sprite = "AQUA7",
			dialoguetext = "And you should be aware that you have a 50/50 chance that your order will become coal...",
			sound = sfx_aqua
		},
		[11] = {
			sprite = "AQUA3",
			dialoguetext = "\x86"+"Shady\x80 is responsible for the cooking part. ",
			sound = sfx_aqua
		},
		[12] = {
			sprite = "AQUA6",
			dialoguetext = "but it's a bit risky seeing that I have a moody dragon for a cook...",
			sound = sfx_aqua
		},
		[13] = {
			sprite = "AQUA2",
			dialoguetext = "(I still remember that one time when they almost burned the whole place down...)",
			sound = sfx_aqua
		},
		[14] = {
			sprite = "AQUA7",
			dialoguetext = "Still, I hope that you're the type to like extra crunchy coal flavored food!...",
			sound = sfx_aqua
		},
		[15] = {
			sprite = "AQUA8",
			dialoguetext = "And not to point fingers... but I noticed a strange thing going around here recently...",
			sound = sfx_aqua
		},
		[16] = {
			sprite = "AQUA5",
			dialoguetext = "The orders and toppings started dissappearing without any trace in large quantities...",
			sound = sfx_aqua
		},
		[17] = {
			sprite = "AQUA8",
			dialoguetext = "At this point I might as well work here as both a cook AND a security guard...",
			sound = sfx_aqua
		},
		[17] = {
			sprite = "AQUA8",
			dialoguetext = "I also noticed that they dissappear only when "+"\x86"+"Shady\x80 is on the shift...",
			sound = sfx_aqua
		},
		[18] = {
			sprite = "AQUA8",
			dialoguetext = "...",
			sound = sfx_aqua
		},
		[19] = {
			sprite = "AQUA2",
			dialoguetext = "...",
			sound = sfx_aqua
		},
		[20] = {
			sprite = "AQUA7",
			dialoguetext = "But yes, despite all these quirks, I still find running this spot very fun!",
			sound = sfx_aqua
		},
		[21] = {
			sprite = "AQUA1",
			dialoguetext = "Especially this time of the year. Mondays are especially nice actually.",
			sound = sfx_aqua
		},
		[22] = {
			sprite = "AQUA9",
			dialoguetext = "Nothing beats a cold breeze with colorful sky early in the morning...",
			sound = sfx_aqua
		},
		[23] = {
			sprite = "AQUA3",
			dialoguetext = "But sometimes it's pretty dry around here. Like now for example. Literally no one around...",
			sound = sfx_aqua
		},
		[24] = {
			sprite = "AQUA10",
			dialoguetext = "And I'm not even sure if I can go for a walk!",
			sound = sfx_aqua
		},
		[25] = {
			sprite = "AQUA8",
			dialoguetext = "What if someone suddely appears at the perfect moment once I'm gone?... Very bleh...",
			sound = sfx_aqua
		},
		[26] = {
			sprite = "AQUA3",
			dialoguetext = "All that sitting around doing nothing reminds me of school near long breaks...",
			sound = sfx_aqua
		},
		[27] = {
			sprite = "AQUA4",
			dialoguetext = "But I really like the fact that "+"\x86"+"Shady\x80 is working here. We're actually the perfect duo for the job.",
			sound = sfx_aqua
		},
		[28] = {
			sprite = "AQUA1",
			dialoguetext = "\x86"+"Shady\x80 cooks burgers with their fire breath... ",
			sound = sfx_aqua
		},
		[29] = {
			sprite = "AQUA6",
			dialoguetext = "And I put out the fire when "+"\x86"+"Shady\x80 has a bad day again!... ",
			sound = sfx_aqua
		},
		[30] = {
			sprite = "AQUA5",
			dialoguetext = "And this is kinda ironic. Despite being somewhat related to dragon lizard thingies.",
			sound = sfx_aqua
		},
		[31] = {
			sprite = "AQUA4",
			dialoguetext = "My element is actually water instead of something like fire.",
			sound = sfx_aqua
		},
		[32] = {
			sprite = "AQUA7",
			dialoguetext = "So I'm actually a 2 in 1 combo of a cook that also doubles as a fire extinguisher!...",
			sound = sfx_aqua
		},
		[33] = {
			sprite = "AQUA1",
			dialoguetext = "And I can actually do a cool fire breath thingy regular dragons can...",
			sound = sfx_aqua
		},
		[34] = {
			sprite = "AQUA6",
			dialoguetext = "But only a ton of water bubbles come out instead...",
			sound = sfx_aqua
		},
		[35] = {
			sprite = "AQUA7",
			dialoguetext = "But hey, that's still an extra attraction for the people I guess! Gotta turn those flaws into cools!",
			sound = sfx_aqua
		},
		[36] = {
			sprite = "AQUA8",
			dialoguetext = "And tell me... Have you stopped here because of the world's most adorable mascot?...",
			sound = sfx_aqua
		},
		[37] = {
			sprite = "AQUA8",
			dialoguetext = "(I need this data to make sure that our marketing strat is actually working...)",
			sound = sfx_aqua
		},
		[38] = {
			sprite = "AQUA4",
			dialoguetext = "Because if yes, then "+"\x86"+"Shady\x80 really went all out on "+"\x8E"+"Kenlra's\x80 design...",
			sound = sfx_aqua
		},
		[39] = {
			sprite = "AQUA9",
			dialoguetext = "(And yes, I also would stop for a kitty mascot...)",
			sound = sfx_aqua
		},
		[40] = {
			sprite = "AQUA1",
			dialoguetext = "...",
			sound = sfx_aqua
		},
		[41] = {
			sprite = "AQUA2",
			dialoguetext = "...",
			sound = sfx_aqua
		},
		[42] = {
			sprite = "AQUA7",
			dialoguetext = "I just realized... I didn't bore you into a 1 star review with all this yapping right?",
			sound = sfx_aqua
		},
		[43] = {
			sprite = "AQUA3",
			dialoguetext = "My small talk always ends up like this...",
			sound = sfx_aqua
		},
		[44] = {
			sprite = "AQUA7",
			dialoguetext = "Uhh... and now stuff became a bit awkward... So... ",
			sound = sfx_aqua
		},
		[45] = {
			sprite = "AQUA5",
			dialoguetext = "There's a 25% deal every friday... and today's friday... so...",
			sound = sfx_aqua
		},
		[46] = {
			sprite = "AQUA6",
			dialoguetext = "Please order something?... If you want?...",
			sound = sfx_aqua
		},
	},
	SHADY = {
		[1] = {
			sprite = "SHADY1",
			dialoguetext = "Well, I didn't expect that we'll get any costumers in the first place...",
			sound = sfx_shady
		},
		[2] = {
			sprite = "SHADY1",
			dialoguetext = "...",
			sound = sfx_shady
		},
		[3] = {
			sprite = "SHADY3",
			dialoguetext = "But I'm not complaining!!! I hope that you'll enjoy your stay!!!",
			sound = sfx_shady
		},
		[4] = {
			sprite = "SHADY4",
			dialoguetext = "Btw, be honest be with me... is our food any good??? Yes? No?",
			sound = sfx_shady
		},
		[5] = {
			sprite = "SHADY2",
			dialoguetext = "And if you have any complains... please go jump in the nearest ocean-",
			sound = sfx_shady
		},
		[6] = {
			sprite = "SHADY3",
			dialoguetext = "I mean, go talk to "+"\x88"+"Aqua\x80"+" about it!!!",
			sound = sfx_shady
		},
		[7] = {
			sprite = "SHADY1",
			dialoguetext = "Speaking of food...",
			sound = sfx_shady
		},
		[8] = {
			sprite = "SHADY3",
			dialoguetext = "Cooking burgers is pretty easy with being a Dragon and all!!!",
			sound = sfx_shady
		},
		[9] = {
			sprite = "SHADY1",
			dialoguetext = "...",
			sound = sfx_shady
		},
		[10] = {
			sprite = "SHADY4",
			dialoguetext = "Well, I still have to worry about not burning them to a crisp...",
			sound = sfx_shady
		},
		[11] = {
			sprite = "SHADY1",
			dialoguetext = "Also, don't tell "+"\x88"+"Aqua\x80 this, but I been secretly eating the food that he left for the costumers...",
			sound = sfx_shady
		},
		[12] = {
			sprite = "SHADY4",
			dialoguetext = "and why I'm telling you this you might say...",
			sound = sfx_shady
		},
		[13] = {
			sprite = "SHADY3",
			dialoguetext = "Well, I don't know hahahahaha!!!!",
			sound = sfx_shady
		},
		[14] = {
			sprite = "SHADY1",
			dialoguetext = "...",
			sound = sfx_shady
		},
		[15] = {
			sprite = "SHADY4",
			dialoguetext = "Man, honestly, this working stuff is boring, I should be at home sleeping or something.",
			sound = sfx_shady
		},
		[16] = {
			sprite = "SHADY2",
			dialoguetext = "And yeah, I only been working for an hour by now.",
			sound = sfx_shady
		},
		[17] = {
			sprite = "SHADY2",
			dialoguetext = "And yes, I got wings and no, I'm not going to show them to you.",
			sound = sfx_shady
		},
		[18] = {
			sprite = "SHADY2",
			dialoguetext = "Stop asking or you are going to be the next one burning to a crisp...",
			sound = sfx_shady
		},
		[19] = {
			sprite = "SHADY4",
			dialoguetext = "...",
			sound = sfx_shady
		},
		[20] = {
			sprite = "SHADY3",
			dialoguetext = "ANYWAYS...",
			sound = sfx_shady
		},
		[21] = {
			sprite = "SHADY1",
			dialoguetext = "What do you think of our mascot, "+"\x8E"+"Kelnra\x80??? personally, I think she's prrrrreeeetttttty rad...",
			sound = sfx_shady
		},
		[22] = {
			sprite = "SHADY3",
			dialoguetext = "TOTALLY not biased btw...  it's not like I designed her or something...",
			sound = sfx_shady
		},
		[23] = {
			sprite = "SHADY1",
			dialoguetext = "And you know what... setting up shop in a place like this is not bad!!!",
			sound = sfx_shady
		},
		[24] = {
			sprite = "SHADY3",
			dialoguetext = "Definitely, one of best ideas that "+"\x88"+"Aqua\x80 had!!!",
			sound = sfx_shady
		},
		[25] = {
			sprite = "SHADY4",
			dialoguetext = "(much better than my idea with setting it up by that small shed at near the that old jungle tour...)",
			sound = sfx_shady
		},
		[26] = {
			sprite = "SHADY3",
			dialoguetext = "Okay I'm done now, buy our stuff!!!! It's 25% off today!!!!",
			sound = sfx_shady
		},
	}
}

local function TriggerDialogue(mo, dialogue)
	mo.dialoguethinker = true
	mo.dialoguecurrent = dialogue
	mo.dialoguenum = 1
	mo.dialoguetimer = 0
	mo.currentline = 1
	mo.linebegin = 1
	mo.spriteless = 0
	mo.textprogress = {
		line1 = nil,
		line2 = nil,
		line3 = nil,
		line4 = nil
	}
	mo.linebreaks = {
		line2 = false,
		line3 = false,
		line4 = false
	}
end



addHook("PlayerThink", function(player)
	if (player.mo and player.mo.valid) and player.mo.dialoguethinker == true 
		player.pflags = $|PF_STASIS|PF_JUMPSTASIS
		local dialogue = player.mo.dialoguecurrent
		local num =  player.mo.dialoguenum
		if dialogue[num].sprite ~= nil
			player.mo.spriteless = 0
		else
			player.mo.spriteless = 1
		end
		if player.mo.dialoguetimer < string.len(dialogue[num].dialoguetext) then
			if player.cmd.buttons & BT_SPIN
				for i = player.mo.dialoguetimer, string.len(dialogue[num].dialoguetext)
					player.mo.dialoguetimer = $ + 1
					player.mo.textprogress["line"..player.mo.currentline] = string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.dialoguetimer)
					if string.sub(dialogue[num].dialoguetext, player.mo.dialoguetimer, player.mo.dialoguetimer) == "*"
						local length = player.mo.dialoguetimer - player.mo.linebegin
						if length > 0 then
							player.mo.textprogress["line"..player.mo.currentline] = string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.dialoguetimer - 1)
						else
							player.mo.textprogress["line"..player.mo.currentline] = ""
						end
						player.mo.currentline = $ + 1
				     	player.mo.linebreaks["line"..player.mo.currentline] = true
						player.mo.linebegin = player.mo.dialoguetimer + 1
						if string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.linebegin) == " " then
							player.mo.linebegin = player.mo.linebegin + 1
						end
						return
					end
					if string.len(player.mo.textprogress["line"..player.mo.currentline]) > (26 + player.mo.spriteless*8)
						local linestring = player.mo.textprogress["line"..player.mo.currentline]
						local reverso = string.reverse(linestring)
						local ending = string.len(linestring) - ((string.find(reverso, " ") or 1) - 1)
						player.mo.textprogress["line"..player.mo.currentline] = string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.linebegin + ending - 1)
						player.mo.currentline = $ + 1
						player.mo.linebegin = $ + ending
						if string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.linebegin) == " " then
							player.mo.linebegin = player.mo.linebegin + 1
						end
						return
					end
				end
			else
				player.mo.dialoguetimer = $ + 1
			end
			if player.mo.dialoguetimer % 1 == 0 then 
				if dialogue[num].sound ~= nil
					S_StartSound(player.mo, dialogue[num].sound, player) 
				else
					S_StartSound(player.mo, sfx_drtalk, player)
				end
			end
		end
		if player.mo.dialoguetimer >= string.len(dialogue[num].dialoguetext)
			if player.cmd.buttons & BT_JUMP and not (player.lastbuttons & BT_JUMP)
				if dialogue[num + 1] == nil
					player.mo.dialoguethinker = false
				else
					player.mo.dialoguenum = $ + 1
					player.mo.dialoguetimer = 0
					player.mo.currentline = 1
					player.mo.linebegin = 0
					player.mo.textprogress = {
						line1 = nil,
						line2 = nil,
						line3 = nil,
						line4 = nil
					}
					player.mo.linebreaks = {
						line2 = false,
						line3 = false,
						line4 = false
					}
				end
			end
		end
		player.mo.textprogress["line"..player.mo.currentline] = string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.dialoguetimer)
		if string.sub(dialogue[num].dialoguetext, player.mo.dialoguetimer, player.mo.dialoguetimer) == "*"
			local length = player.mo.dialoguetimer - player.mo.linebegin
			if length > 0 then
				player.mo.textprogress["line"..player.mo.currentline] = string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.dialoguetimer - 1)
			else
				player.mo.textprogress["line"..player.mo.currentline] = ""
			end
			player.mo.currentline = $ + 1
			player.mo.linebreaks["line"..player.mo.currentline] = true
			player.mo.linebegin = player.mo.dialoguetimer + 1
			if string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.linebegin) == " " then
				player.mo.linebegin = player.mo.linebegin + 1
			end
			return
		end
		if string.len(player.mo.textprogress["line"..player.mo.currentline]) > (26 + player.mo.spriteless*8)
			local linestring = player.mo.textprogress["line"..player.mo.currentline]
			local reverso = string.reverse(linestring)
			local ending = string.len(linestring) - ((string.find(reverso, " ") or 1) - 1)
			player.mo.textprogress["line"..player.mo.currentline] = string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.linebegin + ending - 1)
			player.mo.currentline = $ + 1
			player.mo.linebegin = $ + ending
			if string.sub(dialogue[num].dialoguetext, player.mo.linebegin, player.mo.linebegin) == " " then
				player.mo.linebegin = player.mo.linebegin + 1
			end
			return
		end
	end
end)

local function DialogueDrawer(v, player)
	if (player.mo and player.mo.valid) and player.mo.dialoguethinker == true 
		local dialogue = player.mo.dialoguecurrent
		local num =  player.mo.dialoguenum
		v.draw(12, 110, v.cachePatch("DIALOBOX"), V_SNAPTOBOTTOM)
		if dialogue[num].sprite ~= nil then v.drawScaled(24*FRACUNIT, 120*FRACUNIT, 3*FRACUNIT/7, v.cachePatch(dialogue[num].sprite), V_SNAPTOBOTTOM) end
		v.drawString(90 - (player.mo.spriteless*64), 125, "*", V_SNAPTOBOTTOM)
		for i = 1, 4 do
			local line = player.mo.textprogress["line"..i]
			local broke = player.mo.linebreaks["line"..i]
			if line == nil then return end
			v.drawString(100 - (player.mo.spriteless*64), 125 + (i-1)*15, line, V_SNAPTOBOTTOM)
			if broke == true then v.drawString(90  - (player.mo.spriteless*64), 10 + (i-1)*15, "*", V_SNAPTOBOTTOM) end
		end
	end
end

hud.add(DialogueDrawer, "game")

addHook("LinedefExecute", function(line, mo, sec)
	TriggerDialogue(mo, dialogue[line.stringargs[1]])
end, "DTDIALOG")