local X=(require(script.Parent.loader)).load(script)local I=X("\066\097\115\101\079\098\106\101\099\116")local d=X("\068\114\097\119\105\110\103\082\101\110\100\101\114\101\114")local T=X("\082\120\065\116\116\114\105\098\117\116\101\085\116\105\108\115")local Q=X("\080\101\110\099\105\108\067\111\110\115\116\097\110\116\115")local v=X("\065\116\116\114\105\098\117\116\101\086\097\108\117\101")local D=X("\081\117\101\117\101")local M=X("\080\101\110\099\105\108\067\111\110\116\114\111\108")local J=setmetatable({},I)J.ClassName="\080\101\110\099\105\108\067\108\105\101\110\116"J.__index=J;(X("\080\114\111\109\105\115\101\082\101\109\111\116\101\069\118\101\110\116\077\105\120\105\110")):Add(J,Q.REMOTE_EVENT_NAME)function J.new(X,T)local M=setmetatable(I.new(X),J)M._serviceBag=assert(T,"\078\111 \115\101\114\118\105\099\101\066\097\103")M._drawParts=D.new()M._isDrawing=v.new(M._obj,Q.IS_DRAWING_ATTRIBUTE,false)M._maid:GiveTask((M._isDrawing:Observe()):Subscribe(function(X)if X then M._maid._drawingRenderer=d.new(M._serviceBag,M)else M._maid._drawingRenderer=nil end end))return M end function J.OnVRHandHold(I,X)I:EnableControl()X:GiveTask(function()I:DisableControl()end)end function J.EnableControl(X)if not X._maid._control then X._maid._control=M.new(X._serviceBag,X)end end function J.DisableControl(X)X._maid._control=nil end function J.GiveDrawPart(I,X)I._drawParts:PushRight(X)while I._drawParts:GetCount()>Q.MAX_PARTS_COUNT do local X=I._drawParts:PopLeft()X:Destroy()end end function J.GetPosition(X)return X._obj.CFrame:pointToWorldSpace(X._obj.Size*Vector3.new(0,0,.5))end function J.RequestStopDrawing(X)X._isDrawing.Value=false;(X:PromiseRemoteEvent()):Then(function(X)X:FireServer(Q.REQUEST_SET_IS_DRAWING,false)end)end function J.RequestStartDrawing(X)X._isDrawing.Value=true;(X:PromiseRemoteEvent()):Then(function(X)X:FireServer(Q.REQUEST_SET_IS_DRAWING,true)end)end function J.ObserveColor(X)return T.observeValue(X._obj,Q.COLOR_ATTRIBUTE,Q.DEFAULT_COLOR)end function J.GetColor(X)local I=X._obj:GetAttribute(Q.COLOR_ATTRIBUTE)if not I then return Q.DEFAULT_COLOR else return I end end return J
--[=[
	@class PencilClient
]=]

local require = require(script.Parent.loader).load(script)

local BaseObject = require("BaseObject")
local DrawingRenderer = require("DrawingRenderer")
local RxAttributeUtils = require("RxAttributeUtils")
local PencilConstants = require("PencilConstants")
local AttributeValue = require("AttributeValue")
local Queue = require("Queue")
local PencilControl = require("PencilControl")

local PencilClient = setmetatable({}, BaseObject)
PencilClient.ClassName = "PencilClient"
PencilClient.__index = PencilClient

require("PromiseRemoteEventMixin"):Add(PencilClient, PencilConstants.REMOTE_EVENT_NAME)

function PencilClient.new(obj, serviceBag)
	local self = setmetatable(BaseObject.new(obj), PencilClient)

	self._serviceBag = assert(serviceBag, "No serviceBag")

	self._drawParts = Queue.new()
	self._isDrawing = AttributeValue.new(self._obj, PencilConstants.IS_DRAWING_ATTRIBUTE, false)

	self._maid:GiveTask(self._isDrawing:Observe():Subscribe(function(isDrawing)
		if isDrawing then
			self._maid._drawingRenderer = DrawingRenderer.new(self._serviceBag, self)
		else
			self._maid._drawingRenderer = nil
		end
	end))


	return self
end

function PencilClient:OnVRHandHold(maid)
	self:EnableControl()

	maid:GiveTask(function()
		self:DisableControl()
	end)
end

function PencilClient:EnableControl()
	if not self._maid._control then
		self._maid._control = PencilControl.new(self._serviceBag, self)
	end
end

function PencilClient:DisableControl()
	self._maid._control = nil
end

function PencilClient:GiveDrawPart(drawPart)
	self._drawParts:PushRight(drawPart)

	while self._drawParts:GetCount() > PencilConstants.MAX_PARTS_COUNT do
		local removed = self._drawParts:PopLeft()
		removed:Destroy()
	end
end

function PencilClient:GetPosition()
	return self._obj.CFrame:pointToWorldSpace(self._obj.Size * Vector3.new(0, 0, 0.5))
end

function PencilClient:RequestStopDrawing()
	self._isDrawing.Value = false

	self:PromiseRemoteEvent():Then(function(remoteEvent)
		remoteEvent:FireServer(PencilConstants.REQUEST_SET_IS_DRAWING, false)
	end)
end

function PencilClient:RequestStartDrawing()
	self._isDrawing.Value = true

	self:PromiseRemoteEvent():Then(function(remoteEvent)
		remoteEvent:FireServer(PencilConstants.REQUEST_SET_IS_DRAWING, true)
	end)
end

function PencilClient:ObserveColor()
	return RxAttributeUtils.observeValue(self._obj, PencilConstants.COLOR_ATTRIBUTE, PencilConstants.DEFAULT_COLOR)
end

function PencilClient:GetColor()
	local attribute = self._obj:GetAttribute(PencilConstants.COLOR_ATTRIBUTE)
	if not attribute then
		return PencilConstants.DEFAULT_COLOR
	else
		return attribute
	end
end

return PencilClient