local z=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local T=game:GetService("\077\097\114\107\101\116\112\108\097\099\101\083\101\114\118\105\099\101")local P=game:GetService("\080\108\097\121\101\114\115")local E=z.AvatarEditor local K=require(E.Shared.Util.Spring)local o=require(E.Shared.Util.Maid)local f=require(E.Shared.Util.Promise)local x=require(E.Client.GuiLib.LazyLoader)local c=require(E.Shared.GetProductInfo)local N=require(E.Client.Theme)local O=P.LocalPlayer local y={}y.__index=y function y.new(z)local T=setmetatable({},y)T.Frame=z T.PurchaseFrame=T.Frame.Frame T.CreateFrame=T.Frame.CreateOutfit T.ConfirmFrame=T.Frame.Confirm T.Maid=o.new()T.Listeners=o.new()for z,P in ipairs(z:GetChildren())do if not P:IsA("\070\114\097\109\101")then continue end T.Maid:GiveTask(N:Bind(P,"\066\097\099\107\103\114\111\117\110\100\067\111\108\111\114\051","\080\114\105\109\097\114\121"))T.Maid:GiveTask(N:Bind(P.Buttons.Button1,"\066\097\099\107\103\114\111\117\110\100\067\111\108\111\114\051","\067\097\110\099\101\108"))T.Maid:GiveTask(N:Bind(P.Buttons.Button1,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"))T.Maid:GiveTask(N:Bind(P.Buttons.Button2,"\066\097\099\107\103\114\111\117\110\100\067\111\108\111\114\051","\067\111\110\102\105\114\109"))T.Maid:GiveTask(N:Bind(P.Buttons.Button2,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"))end T.Maid:GiveTask(N:Bind(T.ConfirmFrame.Body.MessageLabel,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"))T.Maid:GiveTask(N:Bind(T.ConfirmFrame.Body.TextLabel,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"))T.Maid:GiveTask(N:Bind(T.ConfirmFrame.Body.TextLabel.Line,"\066\097\099\107\103\114\111\117\110\100\067\111\108\111\114\051","\084\101\120\116"))T.Maid:GiveTask(N:Bind(T.CreateFrame.Body.TextLabel,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"))T.Maid:GiveTask(N:Bind(T.CreateFrame.Body.TextLabel.Line,"\066\097\099\107\103\114\111\117\110\100\067\111\108\111\114\051","\084\101\120\116"))T.Maid:GiveTask(N:Bind(T.CreateFrame.Body.Info.Search,"\066\097\099\107\103\114\111\117\110\100\067\111\108\111\114\051","\084\097\098\066\117\116\116\111\110"))T.Maid:GiveTask(N:Bind(T.CreateFrame.Body.Info.Search.TextBox,"\080\108\097\099\101\104\111\108\100\101\114\067\111\108\111\114\051","\080\108\097\099\101\104\111\108\100\101\114\084\101\120\116"))T.Maid:GiveTask(N:Bind(T.CreateFrame.Body.Info.Search.TextBox,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"))T.Maid:GiveTask(N:Bind(T.PurchaseFrame.Body.ProductInfo.ItemPreviewImage.ItemPreviewImageContainer,"\066\097\099\107\103\114\111\117\110\100\067\111\108\111\114\051","\084\101\120\116"))T.Maid:GiveTask(N:Bind(T.PurchaseFrame.Body.ProductInfo.ProductDescription.ProductDescriptionText,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"))T.Maid:GiveTask(N:Bind(T.PurchaseFrame.Body.AdditionalDetails,"\084\101\120\116\067\111\108\111\114\051","\084\101\120\116"))T.TweenSpring=K.new(1)T.TweenSpring.Damper=1 T.TweenSpring.Speed=20 for z,T in ipairs(z:GetChildren())do if T:IsA("\070\114\097\109\101")then T.Visible=false end end return T end function y.OnRenderStep(T,z)local P=T.Frame local E=T.TweenSpring E:TimeSkip(z)local K=E.Position P.BackgroundTransparency=.4+K*.6 P.Visible=P.BackgroundTransparency<.98 end function y.PromptPurchase(P,z)local E=P.Listeners local K=P.PurchaseFrame local o=K.Buttons local f=K.Body.ProductInfo local x=f.ProductDescription.ProductDescriptionText local N=f.ItemPreviewImage.ItemPreviewImageContainer.ItemImage P:_setup(K)x.Text=""N.Image=""o.Button2.Text="\063\063\063"local y=c(z);(y:andThen(function(P)x.Text=P.Name N.Image="\114\098\120\116\104\117\109\098\058\047\047\116\121\112\101\061\065\115\115\101\116\038\105\100\061"..(P.AssetId.."\038\119\061\049\053\048\038\104\061\049\053\048")if P.IsPublicDomain then o.Button2.Text="\070\114\101\101"elseif P.IsForSale then o.Button2.Text=P.PriceInRobux.." \082\111\098\117\120"else o.Button2.Text="\079\102\102\115\097\108\101"end E:GiveTask(o.Button2.Activated:Connect(function(P)T:PromptPurchase(O,z)E:DoCleaning()end))end)):catch(warn)E:GiveTask(function()y:cancel()x.Text=""N.Image=""o.Button2.Text="\063\063\063"end)end function y.PromptCostume(T,z)local P=T.Listeners local E=T.CreateFrame local K=E.Buttons local o=E.Body.Info.Search.TextBox T:_setup(E)E.Body.Info.TextLabel.Visible=false local f=x.Classes.TextMask.new(o)P:GiveTask(f)f:SetMaskType("\065\108\112\104\097\110\117\109\098\101\114\105\099")f:SetMaxLength(20)local function c()if o.Text~=""then z(o.Text)P:DoCleaning()end end P:GiveTask(f.Frame.FocusLost:Connect(function(z,T)if not z then return end c()end))P:GiveTask(K.Button2.Activated:Connect(function(z)c()end))P:GiveTask(function()o.Text=""end)o:CaptureFocus()end function y.PromptConfirm(P,z,T)local E=P.Listeners local K=P.ConfirmFrame local o=K.Buttons K.Body.TextLabel.Text=z.Title or"\067\111\110\102\105\114\109"K.Body.MessageLabel.Text=z.Text or"\065\114\101 \121\111\117 \115\117\114\101 \121\111\117 \119\097\110\116 \116\111 \100\111 \116\104\105\115 \097\099\116\105\111\110\063"E:GiveTask(o.Button2.Activated:Connect(function(z)T()E:DoCleaning()end))P:_setup(K)end function y._setup(T,z)local P=T.Listeners local E=T.TweenSpring local K=z.Buttons P:GiveTask(K.Button1.Activated:Connect(function(z)P:DoCleaning()end))P:GiveTask(z.Parent.Activated:Connect(function(z)P:DoCleaning()end))P:GiveTask(function()z.Visible=false E.Target=1 end)z.Visible=true E.Target=0 end function y.Destroy(z)z.Maid:DoCleaning()z.Maid=nil z.Listeners:DoCleaning()z.Listeners=nil z.Frame=nil z.PurchaseFrame=nil z.CreateFrame=nil z.ConfirmFrame=nil z.TweenSpring=nil setmetatable(z,nil)end return y


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Marketplace = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local AvatarEditor = ReplicatedStorage.AvatarEditor
local Spring = require(AvatarEditor.Shared.Util.Spring)
local Maid = require(AvatarEditor.Shared.Util.Maid)
local Promise = require(AvatarEditor.Shared.Util.Promise)
local GuiLib = require(AvatarEditor.Client.GuiLib.LazyLoader)
local GetProductInfo = require(AvatarEditor.Shared.GetProductInfo)
local Theme = require(AvatarEditor.Client.Theme)

local player = Players.LocalPlayer

local Class = {}
Class.__index = Class


function Class.new(frame)
	local self = setmetatable({}, Class)
	
	self.Frame = frame
	self.PurchaseFrame = self.Frame.Frame
	self.CreateFrame = self.Frame.CreateOutfit
	self.ConfirmFrame = self.Frame.Confirm
	
	self.Maid = Maid.new()
	self.Listeners = Maid.new()
	
	for i, v in ipairs(frame:GetChildren()) do
		if not v:IsA("Frame") then
			continue
		end
		self.Maid:GiveTask(Theme:Bind(v, "BackgroundColor3", "Primary"))
		self.Maid:GiveTask(Theme:Bind(v.Buttons.Button1, "BackgroundColor3", "Cancel"))
		self.Maid:GiveTask(Theme:Bind(v.Buttons.Button1, "TextColor3", "Text"))
		self.Maid:GiveTask(Theme:Bind(v.Buttons.Button2, "BackgroundColor3", "Confirm"))
		self.Maid:GiveTask(Theme:Bind(v.Buttons.Button2, "TextColor3", "Text"))
	end

	self.Maid:GiveTask(Theme:Bind(self.ConfirmFrame.Body.MessageLabel, "TextColor3", "Text"))
	self.Maid:GiveTask(Theme:Bind(self.ConfirmFrame.Body.TextLabel, "TextColor3", "Text"))
	self.Maid:GiveTask(Theme:Bind(self.ConfirmFrame.Body.TextLabel.Line, "BackgroundColor3", "Text"))
	
	self.Maid:GiveTask(Theme:Bind(self.CreateFrame.Body.TextLabel, "TextColor3", "Text"))
	self.Maid:GiveTask(Theme:Bind(self.CreateFrame.Body.TextLabel.Line, "BackgroundColor3", "Text"))
	self.Maid:GiveTask(Theme:Bind(self.CreateFrame.Body.Info.Search, "BackgroundColor3", "TabButton"))
	self.Maid:GiveTask(Theme:Bind(self.CreateFrame.Body.Info.Search.TextBox, "PlaceholderColor3", "PlaceholderText"))
	self.Maid:GiveTask(Theme:Bind(self.CreateFrame.Body.Info.Search.TextBox, "TextColor3", "Text"))
	
	self.Maid:GiveTask(Theme:Bind(self.PurchaseFrame.Body.ProductInfo.ItemPreviewImage.ItemPreviewImageContainer, "BackgroundColor3", "Text"))
	self.Maid:GiveTask(Theme:Bind(self.PurchaseFrame.Body.ProductInfo.ProductDescription.ProductDescriptionText, "TextColor3", "Text"))
	self.Maid:GiveTask(Theme:Bind(self.PurchaseFrame.Body.AdditionalDetails, "TextColor3", "Text"))
	
	self.TweenSpring = Spring.new(1)
	self.TweenSpring.Damper = 1
	self.TweenSpring.Speed = 20
	
	for i, v in ipairs(frame:GetChildren()) do
		if v:IsA("Frame") then
			v.Visible = false
		end
	end
	
	return self
end


function Class:OnRenderStep(delta)
	local frame = self.Frame
	local tweenSpring = self.TweenSpring
	
	tweenSpring:TimeSkip(delta)
	
	local pos = tweenSpring.Position
	frame.BackgroundTransparency = 0.4 + pos * 0.6
	frame.Visible = frame.BackgroundTransparency < 0.98
end


function Class:PromptPurchase(assetId)
	local maid = self.Listeners
	local frame = self.PurchaseFrame
	
	local overlayButtons = frame.Buttons
	local productInfoFrame = frame.Body.ProductInfo
	local descriptionLabel = productInfoFrame.ProductDescription.ProductDescriptionText
	local previewImage = productInfoFrame.ItemPreviewImage.ItemPreviewImageContainer.ItemImage
	
	self:_setup(frame)
	
	descriptionLabel.Text = ""
	previewImage.Image = ""
	overlayButtons.Button2.Text = "???"
	
	local productInfoPromise = GetProductInfo(assetId)
	productInfoPromise:andThen(function(productInfo)
		descriptionLabel.Text = productInfo.Name
		previewImage.Image = "rbxthumb://type=Asset&id=" .. productInfo.AssetId .. "&w=150&h=150"

		if productInfo.IsPublicDomain then
			overlayButtons.Button2.Text = "Free"
		elseif productInfo.IsForSale then
			overlayButtons.Button2.Text = productInfo.PriceInRobux .. " Robux"
		else
			overlayButtons.Button2.Text = "Offsale"
		end
		
		maid:GiveTask(overlayButtons.Button2.Activated:Connect(function(inputObject)
			Marketplace:PromptPurchase(player, assetId)
			maid:DoCleaning()
		end))
	end)
	:catch(warn)
	
	maid:GiveTask(function()
		productInfoPromise:cancel()
		descriptionLabel.Text = ""
		previewImage.Image = ""
		overlayButtons.Button2.Text = "???"
	end)
end


function Class:PromptCostume(callback)
	local maid = self.Listeners
	
	local frame = self.CreateFrame
	local overlayButtons = frame.Buttons
	local textBox = frame.Body.Info.Search.TextBox
	
	self:_setup(frame)
	
	frame.Body.Info.TextLabel.Visible = false -- TODO error messages?
	
	local mask = GuiLib.Classes.TextMask.new(textBox)
	maid:GiveTask(mask)
	
	mask:SetMaskType("Alphanumberic")
	mask:SetMaxLength(20)
	
	local function onFocusLost()
		if textBox.Text ~= "" then
			callback(textBox.Text)
			maid:DoCleaning()
		end
	end
	
	maid:GiveTask(mask.Frame.FocusLost:Connect(function(enterPressed, inputThatCausedFocusLoss)
		if not enterPressed then
			return
		end
		onFocusLost()
	end))
	
	maid:GiveTask(overlayButtons.Button2.Activated:Connect(function(inputObject)
		onFocusLost()
	end))
	
	maid:GiveTask(function()
		textBox.Text = ""
	end)
	
	textBox:CaptureFocus()
end


function Class:PromptConfirm(configTable, callback)
	local maid = self.Listeners
	
	local frame = self.ConfirmFrame
	local overlayButtons = frame.Buttons
	
	frame.Body.TextLabel.Text = configTable.Title or "Confirm"
	frame.Body.MessageLabel.Text = configTable.Text or "Are you sure you want to do this action?"
	
	maid:GiveTask(overlayButtons.Button2.Activated:Connect(function(inputObject)
		callback()
		maid:DoCleaning()
	end))
	
	self:_setup(frame)
end


function Class:_setup(frame)
	local maid = self.Listeners
	local tweenSpring = self.TweenSpring
	
	local overlayButtons = frame.Buttons
	
	maid:GiveTask(overlayButtons.Button1.Activated:Connect(function(inputObject)
		maid:DoCleaning()
	end))
	
	maid:GiveTask(frame.Parent.Activated:Connect(function(inputObject)
		maid:DoCleaning()
	end))
	
	maid:GiveTask(function()
		frame.Visible = false
		tweenSpring.Target = 1
	end)
	
	frame.Visible = true
	tweenSpring.Target = 0
end


function Class:Destroy()
	self.Maid:DoCleaning()
	self.Maid = nil
	self.Listeners:DoCleaning()
	self.Listeners = nil
	self.Frame = nil
	self.PurchaseFrame = nil
	self.CreateFrame = nil
	self.ConfirmFrame = nil
	self.TweenSpring = nil
	setmetatable(self, nil)
end


return Class
