-- QuizClientScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

-- RemoteEvents
local StartQuizEvent = ReplicatedStorage:WaitForChild("StartQuizEvent")
local AnswerSelectedEvent = ReplicatedStorage:WaitForChild("AnswerSelectedEvent")
local UpdateQuestionEvent = ReplicatedStorage:WaitForChild("UpdateQuestionEvent")

-- Reference to the NPC and its ProximityPrompt
local npc = workspace:WaitForChild("Level 1")
local proximityPrompt = npc:WaitForChild("HumanoidRootPart"):WaitForChild("ProximityPrompt")

-- After your other variables, add:
local playerTable = workspace:WaitForChild("PlayerTable")
local answerPart = playerTable:WaitForChild("Answer")
local answerSurfaceGui = answerPart:WaitForChild("SurfaceGui")
local answerLabel = answerSurfaceGui:WaitForChild("TextLabel")

-- GUI References
local npcDialogueGui = playerGui:WaitForChild("NPCDialogue")
local mainGui = playerGui:WaitForChild("MainGui")
local questionFrame = mainGui:WaitForChild("QuestionFrame")
local questionTextLabel = questionFrame:WaitForChild("Question")

npcDialogueGui.Enabled = false
mainGui.Enabled = false

-- Buttons
local acceptButton = npcDialogueGui.Frame:WaitForChild("Accept")
local declineButton = npcDialogueGui.Frame:WaitForChild("Decline")

local answerButtons = {
	questionFrame:WaitForChild("Answer1"),
	questionFrame:WaitForChild("Answer2"),
	questionFrame:WaitForChild("Answer3"),
	questionFrame:WaitForChild("Answer4"),
}

-- Set up answer button clicks
for _, button in ipairs(answerButtons) do
	button.MouseButton1Click:Connect(function()
		print("Button clicked:", button.Text)
		AnswerSelectedEvent:FireServer(button.Text)

		-- Disable all buttons after click
		for _, btn in ipairs(answerButtons) do
			btn.Active = false
		end
	end)
end

-- Function to create typing effect
local function typeText(textLabel, textToType, typingSpeed, callback)
	textLabel.Text = ""
	for i = 1, #textToType do
		textLabel.Text = string.sub(textToType, 1, i)
		wait(typingSpeed)
	end
	if callback then
		callback()
	end
end

-- Update the displayQuestion function to clear the answer text:
local function displayQuestion(questionData)
	mainGui.Enabled = true
	questionFrame.Visible = true

	-- Clear the answer display
	answerLabel.Text = ""

	-- Hide all buttons first
	for _, button in ipairs(answerButtons) do
		button.Visible = false
		button.Active = false
	end

	-- Display question with typing effect
	typeText(questionTextLabel, questionData.question, 0.05, function()
		-- After typing is done, show the answer buttons
		for i, button in ipairs(answerButtons) do
			if questionData.answers[i] then
				button.Text = questionData.answers[i]
				button.Visible = true
				button.Active = true
			else
				button.Visible = false
				button.Active = false
			end
		end
	end)
end

-- Update the UpdateQuestionEvent handler to clear the answer when showing results:
UpdateQuestionEvent.OnClientEvent:Connect(function(data)
	if data.quizFinished then
		mainGui.Enabled = false
		questionFrame.Visible = false
		answerLabel.Text = "" -- Clear answer when quiz finishes
		return
	end

	if data.result then
		-- Show result with typing effect
		for _, button in ipairs(answerButtons) do
			button.Visible = false
		end
		typeText(questionTextLabel, data.result, 0.05)
		-- Don't clear answer text here so player can see their last answer
	else
		-- Show new question
		displayQuestion(data)
	end
end)

-- Handle ProximityPrompt
proximityPrompt.Triggered:Connect(function()
	npcDialogueGui.Enabled = true
	local textLabel = npcDialogueGui.Frame:WaitForChild("Text")

	-- Hide buttons initially
	acceptButton.Visible = false
	declineButton.Visible = false

	-- Show text with typing effect, then show buttons
	typeText(textLabel, "You think you're smart enough to defeat me?!", 0.05, function()
		acceptButton.Visible = true
		declineButton.Visible = true
	end)
end)

-- Handle Accept button
acceptButton.MouseButton1Click:Connect(function()
	npcDialogueGui.Enabled = false
	StartQuizEvent:FireServer()
end)

-- Handle Decline button
declineButton.MouseButton1Click:Connect(function()
	npcDialogueGui.Enabled = false
end) (nevermind, just don't make it blank, sync the numbers between the buttons)