--[[

	Creator: Gerod
	Data: 21.06.20
	Debug TRUE

]]

-- Creates a new class
FlyBlock = class(nil)

-- Sets ShapeClass constants
FlyBlock.maxParentCount = 0
FlyBlock.maxChildCount = 0
FlyBlock.connectionInput = sm.interactable.connectionType.none
FlyBlock.connectionOutput = sm.interactable.connectionType.none
FlyBlock.colorNormal = sm.color.new( 0x778777ff )
FlyBlock.colorHighlight = sm.color.new( 0x8888889f )
FlyBlock.debug = true
FlyBlock.playerSpawned = nil

-- GenericClass events
function FlyBlock.client_onCreate( self )
	self.playerSpawned = sm.localPlayer.getPlayer()
	self.network:sendToServer('cl_init', "loadsave")
end

function FlyBlock.loadsave( self, mode )
	self:cl_init(mode)
end 

function FlyBlock.cl_init( self, mode )
	if self.playerSpawned == nil then return end

	self.cl = {}
	--| SAVED |--
	self.cl.saved = self.storage:load()
	if self.debug then print( "Load Saved:", self.cl.saved ) end
	if self.cl.saved == nil then
		mode = "createsave"
		
		self.cl.saved = {}
		self.cl.saved.speedPlayer = 1
		self.cl.saved.playerOwner = self.playerSpawned
		
		self.cl.saved.fly = {}
		self.cl.saved.fly.swimmingActive = false
		self.storage:save( self.cl.saved )
	else
		mode = "loadsave"
	end
	-- ^SAVED^ --
	
	
	--| Global Varibles |--
	if mode == "loadsave" then self.cl.active = true else self.cl.active = false end
	self.cl.playerOwner = self.cl.saved.playerOwner
	self.cl.characterPlayer = self.cl.playerOwner.character
	self.cl.speedPlayer = self.cl.saved.speedPlayer
	
	self.cl.fly = self.cl.saved.fly
	self.cl.fly.swimmingActive = self.cl.saved.fly.swimmingActive
	
	if mode == "loadsave" then self.cl.loadingSave = false end
	-- ^Global Varibles^ --
	
	if mode == "loadsave" then self:changeFlyMode(mode) end
end


function FlyBlock.changeFlyMode( self, mode )
	local ownerName = self.cl.playerOwner:getName()
	for id, player in pairs(sm.player.getAllPlayers()) do
		if player:getName() == ownerName then
			local player = self.cl.playerOwner
			local character = player:getCharacter()
			
			if self.cl.active then
				if mode == "onInteract" then
					if character:isSwimming() then
						self.cl.fly.swimmingActive = false
					else
						self.cl.fly.swimmingActive = true
					end
				end
				if self.cl.fly.swimmingActive then self.cl.speedPlayer = 3 else self.cl.speedPlayer = 1 end
				
				character:setSwimming(self.cl.fly.swimmingActive)
				character:setMovementSpeedFraction(self.cl.speedPlayer)
				self.cl.active = false
				
				self.cl.saved.speedPlayer = self.cl.speedPlayer
				self.cl.saved.fly.swimmingActive = self.cl.fly.swimmingActive
				if self.debug then print("Save Saved: ", self.cl.saved) end
				self.storage:save( self.cl.saved )
			end
		else
			if self.debug then
				sm.gui.chatMessage("#0090ff[FlyMode]\n#00ff90[ENG]#ff0000 Owner(#ffff00"..ownerName..")#ff0000 not found this world\n#00ff90[РУС]#ff0000 Владелец(#ffff00"..ownerName..")#ff0000 не находится в этом мире.")
			end
		end
	end	
end

function FlyBlock.client_onInteract( self, character, state )
	if state == true then
		self.cl.active = true
		self.network:sendToServer("changeFlyMode", "onInteract")
	end
end