
--[[
			// OutfitData.lua

			// Created by Kip Turner
			// Copyright Roblox 2015
]]

local CoreGui = Game:GetService("CoreGui")
local GuiRoot = CoreGui:FindFirstChild("RobloxGui")
local Modules = GuiRoot:FindFirstChild("Modules")

local Utility = require(Modules:FindFirstChild('Utility'))
local UserData = require(Modules:FindFirstChild('UserData'))


local Http = require(Modules:FindFirstChild('Http'))
local EventHub = require(Modules:FindFirstChild('EventHub'))

local OutfitData = {}

local wearingOutfit = nil

local function CreateOutfitItem(outfitInfo)
	local this = {}
	local isWearing = false
	this.IsWearingChanged = Utility.Signal()

	function this:GetUserId()
		return outfitInfo['UserId']
	end

	function this:GetOutfitId()
		return outfitInfo['OutfitId']
	end

	function this:GetId()
		return outfitInfo['Id']
	end

	function this:GetName()
		return outfitInfo['Name']
	end

	function this:IsOwned()
		return true
	end

	function this:IsWearing()
		local wasWearing = isWearing
		isWearing = (self:GetId() == wearingOutfit)
		if isWearing ~= wasWearing then
			self.IsWearingChanged:fire(isWearing)
		end
		return isWearing
	end

	function this:WearAsync()
		local result = Http.PostWearUserOutfitAsync(self:GetId())
		-- if result and result['success'] == true then
			wearingOutfit = self:GetId()
			EventHub:dispatchEvent(EventHub.Notifications["DonnedDifferentOutfit"], self:GetId())
		-- end
		return
	end

	return this
end


local OutfitCache = nil
local RbxUid = nil

local debounceGetGetMyOutfitsAsync = false
function OutfitData:GetMyOutfitsAsync()
	while debounceGetGetMyOutfitsAsync do wait() end

	debounceGetGetMyOutfitsAsync = true
	UserData.GetLocalPlayerAsync()

	if RbxUid ~= UserData:GetRbxUserId() then
		OutfitCache = nil
	end

	while not OutfitCache do
		local startRbxUid = UserData:GetRbxUserId()
		local outfits = {}
		local index = 0
		local count = 20

		repeat
			local result = Http.GetMyUserOutfitsAsync(index, count)

			if result then
				local userOutfits = result['UserOutfits']
				if userOutfits then
					for _, outfitInfo in pairs(userOutfits) do
						table.insert(outfits, CreateOutfitItem(outfitInfo))
					end
				end
			end

			index = index + count
		until result == nil or result['FinalPage']

		local nowRbxUid = UserData:GetRbxUserId()
		if startRbxUid == nowRbxUid then
			OutfitCache = outfits
		end
		print("Getting info cache again" , "now" , nowRbxUid , "startRbxUid" , startRbxUid)
	end

	debounceGetGetMyOutfitsAsync = false
	return OutfitCache
end


EventHub:addEventListener(EventHub.Notifications["DonnedDifferentPackage"], "OutfitData",
function()
	wearingOutfit = nil
	if OutfitCache then
		for _, outfit in pairs(OutfitCache) do
			outfit:IsWearing()
		end
	end
end)
EventHub:addEventListener(EventHub.Notifications["DonnedDifferentOutfit"], "OutfitData",
function()
	if OutfitCache then
		for _, outfit in pairs(OutfitCache) do
			outfit:IsWearing()
		end
	end
end)


return OutfitData



