require 'luautils'

HCPlaceItem = {};

function HCPlaceItem.isBarricade(object)
	if not object then return false end
	return object:getObjectName() == "Barricade" or object:getObjectName() == "Barricade2";
end

function HCPlaceItem.isStonePillar(object)
	if not object then return false end
	return object:getObjectName() == "Stone Pillar";
end

function HCPlaceItem.findBarricade(square)
	if not square then return nil end
	for i=0,square:getObjects():size()-1 do
		local object = square:getObjects():get(i);
		if HCPlaceItem.isBarricade(object) then
			return object;
		end
	end
	return nil;
end

function HCPlaceItem.findStonePillar(square)
	if not square then return nil end
	for i=0,square:getObjects():size()-1 do
		local object = square:getObjects():get(i);
		if HCPlaceItem.isStonePillar(object) then
			return object;
		end
	end
	return nil;
end

function HCPlaceItem.placeStonePillar(grid, sprite)
	if not grid then return end
	if HCPlaceItem.findStonePillar(grid) then return end
	
	local pillar = IsoObject.new(grid, sprite, "ISSimpleFurniture");
	grid:AddTileObject(pillar);
	pillar:transmitCompleteItemToClients();
end

function HCPlaceItem.placeBarricade(grid, sprite)
	if not grid then return end
	if HCPlaceItem.findBarricade(grid) then return end
	
	local barricade = IsoObject.new(grid, sprite, "HCBarricade");
	grid:AddTileObject(barricade);
	barricade:transmitCompleteItemToClients();
	
	sprite = sprite:gsub('Front', 'Back');
	local dx = 0;
	local dy = 0;
	if sprite:find('North') then
		dy = -1;
	else
		dx = -1;
	end
	grid = getCell():getGridSquare(grid:getX() + dx, grid:getY() + dy, grid:getZ());
	barricade = IsoObject.new(grid, sprite, "HCBarricade");
	grid:AddTileObject(barricade);
	barricade:transmitCompleteItemToClients();
	
	
	return barricade;
end


function HCPlaceItem.OnClientCommand(module, command, player, args)
	if module ~= 'Hydrocraft' then return end
	if command == 'placeBarricade' then
		local gs = getCell():getGridSquare(args.x, args.y, args.z);
		if gs then
			HCPlaceItem.placeBarricade(gs, args.sprite);
		end
	elseif command == 'placeStonePillar' then
		local gs = getCell():getGridSquare(args.x, args.y, args.z);
		if gs then
			HCPlaceItem.placeStonePillar(gs, args.sprite);
		end
	end
end

function HCPlaceItem.removeStonePillar(pillar)
	if not pillar then return end
	local grid = pillar:getSquare();
	if not grid then return end
	grid:transmitRemoveItemFromSquare(pillar);
	grid:RemoveTileObject(pillar);
end

function HCPlaceItem.removeBarricade(barricade)
	if not barricade then return end
	local grid = barricade:getSquare();
	if not grid then return end
	grid:transmitRemoveItemFromSquare(barricade);
	grid:RemoveTileObject(barricade);
	
	local dx = 0;
	local dy = 0;
	if barricade:getSpriteName():find('North') then
		if barricade:getSpriteName():find('Front') then
			dy = -1;
		else
			dy = 1;
		end
	else
		if barricade:getSpriteName():find('Front') then
			dx = -1;
		else
			dx = 1;
		end
	end
	grid = getCell():getGridSquare(grid:getX()+dx, grid:GetY()+dy, grid:getZ());
	barricade = HCPlaceItem.findBarricade(grid);
	if not barricade then return end
	grid:transmitRemoveItemFromSquare(barricade);
	grid:RemoveTileObject(barricade);
end

if isServer() then
	Events.OnClientCommand.Add(HCPlaceItem.OnClientCommand);
end