----------------------------
-- Robo
-- doors.lua
----------------------------

----------------------------
-- Door table structure definition
-- Doors {
--			entIndex {
--						owner "entity" -- networked
--						coowners {"table of entities"}
--						title "string"  -- networked
--						offline {"table of entities"}
--					 }
--		 }
----------------------------

Doors = {}

/* I'll rewrite this one day to allow faction doors in the door save system
function CAKE.GetDoorGroup(ent)
		
	local pos = ent:GetPos();
	local doorgroups = {};

	for k, v in pairs(Doors) do
		
		if(tonumber(v["x"]) == math.ceil(tonumber(pos.x)) and tonumber(v["y"]) == math.ceil(tonumber(pos.y)) and tonumber(v["z"]) == math.ceil(tonumber(pos.z))) then
			
			table.insert(doorgroups, v["group"]);
				
		end
			
	end

	return doorgroups;

end
*/

function ccPurchaseDoor(ply, cmd, args)

	-- server only, would cause errors because of items clients can't see
	if (CLIENT) then return; end
	
	local door = ents.GetByIndex(tonumber(args[1]));
	
	if(CAKE.IsDoor(door)) then
		-- IF unowned
		if( door:GetNWString("owner") == "" ) then
			if(tonumber(ply:GetNWString("money")) >= 50) then
				
				-- Enough money to start off, let's start the rental.
				ply:SetField("money", tonumber(ply:GetNWString("money")) - 50);
				-- set owner, title, clear coowners.
				door:SetNWString("owner", ply:SteamID() );
				door:SetNWString("title", "Door");
				door.coowners = {};
				
				if ( table.HasValue(Doors, tonumber(args[1]) ) )then
				
				else
					table.insert(Doors, tonumber(args[1]));
				end
				
				/* THIS IS COMMENTED OUT, RENTING IS BAD.
				local function Rental(ply, doornum)
					
					if(!ply:IsValid() or !ply:IsPlayer()) then return; end

					local door = ents.GetByIndex(tonumber(doornum));
					
					if(door.owner == ply) then
						if(tonumber(ply:GetNWString("money")) >= 50) then
							CAKE.Notify(ply, "Doortest");
							ply:SetField("money", tonumber(ply:GetNWString("money")) - 0);
							-- Start the timer again
							CAKE.DayLog(ply:Name() .. " [" .. ply:SteamID() .. "] has been charged for door " .. doornum);
							timer.Simple(900, Rental, ply, doornum); -- 15 minutes hoo rah
						else
							CAKE.Notify(ply, "You have lost a door due to insufficient funds.");
							door.owner = nil;
						end
					end
				
				end
				
				timer.Simple(900, Rental, ply, tonumber(args[1]));
				*/
				
			end
		-- IF player is the owner
		elseif(door:GetNWString("owner") == ply:SteamID() ) then
		
			-- coowners exist
			if( table.Count( door.coowners ) > 0 ) then
			
				-- I hope to god this works, its so much better than breaking a for loop.
				-- Finds highest existing index in the coowners table, sets that as new owner value
				-- then sets that value in coowners to nil.
				door:SetNWString("owner", table.remove(door.coowners, 1) );
				return;
			else
				door:SetNWString("owner", "" );
				door:SetNWString("title", "Unowned");
			end
			CAKE.Notify(ply, "Door unowned.");
			CAKE.DayLog(ply:Name() .. " [" .. ply:SteamID() .. "] unowned door " .. args[1] .. door:GetNWString("owner") .. " in possession.");
		-- If unowner is a player
		elseif( table.HasValue(door.coowners, ply:SteamID() ) ) then
		
			for k, v in pairs( door.coowners ) do
			
				if ( v == ply:SteamID() ) then
				
					table.remove(door.coowners, k);
				
				end
			
			end
		
			CAKE.Notify(ply, "Door unowned.");
			CAKE.DayLog(ply:Name() .. " [" .. ply:SteamID() .. "] unowned door " .. args[1]);
		
		else
			CAKE.Notify(ply, "You do not own this door.");
		end
	
	end
	
end
concommand.Add("rp_purchasedoor", ccPurchaseDoor);

function ccLockDoor(ply, cmd, args)
	
	-- server only, would cause errors because of items clients can't see
	if (CLIENT) then return; end
	
	local entity = ents.GetByIndex(tonumber(args[1]));
	
	if( entity.coowners == nil ) then
		entity.coowners = {};
	end

	if(CAKE.IsDoor(entity)) then
	
		if(entity:GetNWString("owner") == ply:SteamID() or table.HasValue(entity.coowners, ply:SteamID() ) ) then
			entity:Fire("lock", "", 0);
		end
		
	end

end
concommand.Add("rp_lockdoor", ccLockDoor);

function ccUnLockDoor(ply, cmd, args)

	-- server only, would cause errors because of items clients can't see
	if (CLIENT) then return; end

	local entity = ents.GetByIndex(tonumber(args[1]));
	if( entity.coowners == nil ) then
		entity.coowners = {};
	end

	if(CAKE.IsDoor(entity)) then
	
		if(entity:GetNWString("owner") == ply:SteamID() or table.HasValue(entity.coowners, ply:SteamID() ) ) then
			entity:Fire("unlock", "", 0);
			return;
		end
		
	end

end
concommand.Add("rp_unlockdoor", ccUnLockDoor);

function ccOpenDoor(ply, cmd, args)

	local entity = ply:GetEyeTrace().Entity;
	
	if( entity.coowners == nil ) then
		entity.coowners = {};
	end
	
	-- server only, would cause errors because of items clients can't see
	if (CLIENT) then return; end

	if(entity != nil and entity:IsValid() and CAKE.IsDoor(entity) and ply:GetPos():Distance(entity:GetPos()) < 200) then
	
		if( entity:GetNWString("owner") == ply:SteamID() or table.HasValue(entity.coowners, ply:SteamID() ) ) then
		
			entity:Fire("toggle", "", 0);
			return;
		
		end
		
	end
	
end
concommand.Add("rp_opendoor", ccOpenDoor);

function ccAddDoorOwner(ply, cmd, args)
	local door = ents.GetByIndex(tonumber(args[1]));
		
	if(CAKE.IsDoor(door)) then

		-- if owner, check to add coowner
		if(door:GetNWString("owner") == ply:SteamID() ) then
		
			local target = CAKE.SearchForPlayer(1, args[1]); -- checking by name
			if ( target != nil ) then
				table.insert(door.coowners, target:SteamID() );
				CAKE.DayLog(ply:Name() .. " [" .. ply:SteamID() .. "] added door owner " .. target:Name() .. " to door " .. args[1]);
			else
				CAKE.Notify(ply, "Target not found.");
			end
		
		-- if coowner, check to add coowner
		elseif( table.HasValue( door.coowners, ply:SteamID() ) )then
		
			local target = CAKE.SearchForPlayer(1, args[1]); -- checking by name
			if ( target != nil ) then
				table.insert(door.coowners, target:SteamID() );
				CAKE.DayLog(ply:Name() .. " [" .. ply:SteamID() .. "] added door owner " .. target:Name() .. " to door " .. args[1]);
			else
				CAKE.Notify(ply, "Target not found.");
			end
		else
			CAKE.Notify(ply, "You do not have ownership of this door.");
		end
	
	end
	
end
concommand.Add("rp_addowner", ccAddDoorOwner);