local q=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local Q=q:WaitForChild("\077\111\100\117\108\101\115")local R=require(Q.src.utils.Logger)local j=((game:GetService("\083\116\097\114\116\101\114\080\108\097\121\101\114")):WaitForChild("\083\116\097\114\116\101\114\080\108\097\121\101\114\083\099\114\105\112\116\115")).clientSrc local E=require(Q.Roact)local x=require(Q.RoactRodux)local F=require(j.getApiFromComponent)local J=require(j.Components.common.UICorner)local S=require(j.Components.common.RoundButton)local k=require(j.Components.common.DynamicTable)local i=require(j.Components.common.TextListWithHeader)local g=require(j.Components.common.TextLabel)local X=require(j.Components.PlayersPlayingTableRow)local L=require(Q.M)local h=E.createElement local v=E.PureComponent:extend("\070\105\110\105\115\104\083\099\114\101\101\110")function v.init(q)q.state={open=true}q.api=F(q)end function v.render(q)local Q=q.props.playersPlaying local j=q.props.noTimer local x=L.count(Q)==0 if not q.props.isFinishScreenOpen or not q.state.open then return nil end if not j and x then return nil end local F local v if j then F="\089\111\117 \070\105\110\105\115\104\101\100"R:w("\102\105\110\105\115\104\101\100")v=h(i,{title=F;[E.Children]=h(g,{Text="\067\111\110\103\114\097\116\117\108\097\116\105\111\110\115\033\033";Position=UDim2.new(0,0,0,0),Size=UDim2.new(1,0,1,0);TextSize=36,TextXAlignment=Enum.TextXAlignment.Center,TextYAlignment=Enum.TextYAlignment.Center,LayoutOrder=2;TextColor3=Color3.fromRGB(255,255,255)})})else F="\070\073\078\073\083\072\069\082\083"v=h(i,{title=F,[E.Children]=h(k,{items=Q;rowComponent=X,rowProps={TextColor3=Color3.fromRGB(255,255,255),ghostPlayerId=q.props.ghostPlayerId;startTime=q.props.startTime,endTime=q.props.endTime}})})end function OnClick()q:setState({open=false})end local b=h(S,{Text="\067\076\079\083\069",Position=UDim2.new(.5,0,1,10),AnchorPoint=Vector2.new(.5,0);onClicked=OnClick})return h("\070\114\097\109\101",{Size=UDim2.new(1,0,1,0),BackgroundColor3=Color3.fromRGB(124,0,215)},{UICorner=h(J);PlayersList=v;Button=b})end function v.didUpdate(R,q,Q)if not q.isFinishScreenOpen and not Q.open then R:setState(function()return{open=true}end)end end local b=(x.connect(function(q)local Q=q.player.lastFinishedRoomId if Q==nil then return{}end local R=q.rooms[Q]return{isFinishScreenOpen=q.player.isFinishScreenOpen,startTime=R.startTime,endTime=R.endTime;playersPlaying=R.playersPlaying;countDownTime=R.countDownTime;noTimer=R.config.noTimer}end))(v)return b
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')
local logger = require(Modules.src.utils.Logger)
local clientSrc = game:GetService('StarterPlayer'):WaitForChild('StarterPlayerScripts').clientSrc

local Roact = require(Modules.Roact)
local RoactRodux = require(Modules.RoactRodux)

local getApiFromComponent = require(clientSrc.getApiFromComponent)

local UICorner = require(clientSrc.Components.common.UICorner)
local RoundButton = require(clientSrc.Components.common.RoundButton)
local DynamicTable = require(clientSrc.Components.common.DynamicTable)
local TextListWithHeader = require(clientSrc.Components.common.TextListWithHeader)
local TextLabel = require(clientSrc.Components.common.TextLabel)
local PlayersPlayingTableRow = require(clientSrc.Components.PlayersPlayingTableRow)
local M = require(Modules.M)

local createElement = Roact.createElement

local FinishScreen = Roact.PureComponent:extend('FinishScreen')

function FinishScreen:init()
	self.state = { open = true }

	self.api = getApiFromComponent(self)
end

function FinishScreen:render()
	local playersPlaying = self.props.playersPlaying
	local noTimer = self.props.noTimer

	local noPlayers = M.count(playersPlaying) == 0

	if not self.props.isFinishScreenOpen or not self.state.open then
		return nil
	end

	if not noTimer and noPlayers then
		return nil
	end

	local title
	local playersList

	if noTimer then
		title = 'You Finished'
		logger:w('finished')
		playersList = createElement(TextListWithHeader, {
			title = title,
			[Roact.Children] = createElement(TextLabel, {
				Text = 'Congratulations!!',
				Position = UDim2.new(0, 0, 0, 0),
				Size = UDim2.new(1, 0, 1, 0),
				TextSize = 36,
				TextXAlignment = Enum.TextXAlignment.Center,
				TextYAlignment = Enum.TextYAlignment.Center,
				LayoutOrder = 2,
				TextColor3 = Color3.fromRGB(255, 255, 255),
			}),
		})
	else
		title = 'FINISHERS'

		playersList = createElement(TextListWithHeader, {
			title = title,
			[Roact.Children] = createElement(DynamicTable, {
				items = playersPlaying,
				rowComponent = PlayersPlayingTableRow,
				rowProps = {
					TextColor3 = Color3.fromRGB(255, 255, 255),
					ghostPlayerId = self.props.ghostPlayerId,
					startTime = self.props.startTime,
					endTime = self.props.endTime,
				},
			}),
		})
	end

	function OnClick()
		self:setState({ open = false })
	end

	local button = createElement(RoundButton, {
		Text = 'CLOSE',
		Position = UDim2.new(0.5, 0, 1, 10),
		AnchorPoint = Vector2.new(0.5, 0),
		onClicked = OnClick,
	})

	return createElement(
		'Frame',
		{
			Size = UDim2.new(1, 0, 1, 0),
			BackgroundColor3 = Color3.fromRGB(124, 0, 215),
		},
		{
			UICorner = createElement(UICorner),
			PlayersList = playersList,
			Button = button,
		}
	)
end
--[[
function FinishScreen:shouldUpdate(newProps, newState)
	return newProps.isFinishScreenOpen ~= self.props.isFinishScreenOpen or newState.open ~= self.state.open
end
]]

function FinishScreen:didUpdate(previousProps, previousState)
	if not previousProps.isFinishScreenOpen and not previousState.open then
		self:setState(function()
			return { open = true }
		end)
	end
end

local FinishScreenConnected = RoactRodux.connect(function(state)
	local roomId = state.player.lastFinishedRoomId
	if roomId == nil then
		return {}
	end

	local room = state.rooms[roomId]

	return {
		isFinishScreenOpen = state.player.isFinishScreenOpen,
		startTime = room.startTime,
		endTime = room.endTime,
		playersPlaying = room.playersPlaying,
		countDownTime = room.countDownTime,
		noTimer = room.config.noTimer,
	}
end)(FinishScreen)

return FinishScreenConnected