require "BuildingObjects/ISBuildingObject"

HCStonePillar = ISBuildingObject:derive("HCStonePillar");
HCStonePillar.pillars = {};

function HCStonePillar:create(x, y, z, north, sprite)
	local cell = getWorld():getCell();
	self.sq = cell:getGridSquare(x, y, z);
	self:setInfo(self.sq, sprite, "Pillar", self);
	
	if self.sq:getZone() then
		self.sq:getZone():setHaveConstruction(true);
	end
	if self.item == 0 then
		ISTimedActionQueue.add(ISPlaceStonePillarAction:new(self.character, sq, self.character:getInventory():FindAndReturn("HCStonepillar"), sprite, 0));
	end
end

function HCStonePillar:setInfo(square, sprite, name)
	local thumpable = IsoThumpable.new(square:getCell(), square, sprite, self.north, self);
	buildUtil.setInfo(thumpable, self);
	thumpable:setName(name);
	thumpable:setCanBarricade(false);
	thumpable:setIsDismantable(false);
	thumpable:setMaxHealth(1000);
	thumpable:setBreakSound("breakdoor");
	thumpable:getSprite():getProperties():Set(IsoFlagType.collideN);
	thumpable:getSprite():getProperties():Set(IsoFlagType.collideW);
	square:AddSpecialObject(thumpable);
	thumpable:setModData(copyTable(self.modData));
	thumpable:transmitCompleteItemToServer();
end

function HCStonePillar.OnDestroy(thump, character)
	self:removeFromGround(thump:getSquare());
	local result = ZombRand(11)
	if result == 0 then
		for x=0, 14 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 1 then
		for x=0, 15 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 2 then
		for x=0, 16 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 3 then
		for x=0, 17 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 4 then
		for x=0, 18 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 5 then
		for x=0, 19 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 6 then
		for x=0, 20 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 7 then
		for x=0, 21 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 8 then
		for x=0, 22 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 9 then
		for x=0, 23 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	elseif result == 11 then
		for x=0, 24 do
			thump:getSquare():AddWorldInventoryItem("Base.Stone");
		end
	end
end

function HCStonePillar:removeFromGround(square)
	for i=0, square:getSpecialObjects():size() do
		local thump = square:setSpecialObjects():get(i);
		if instanceof(thump, "IsoThumpable") then
			square:getSquare():RemoveTileObject(thump);
			square:transmitRemoveItemFromSquare(thump);
			square:getSpecialObjects():remove(thump);
			square:getObjects():remove(thump);
		end
	end
end

function HCStonePillar:new(character, item)
	local o = {};
	setmetatable(o, self);
	self.__index = self;
	o:init();
	o:setSprite("media/textures/Object_HCStonepillar.png");
	o:setNorthSprite("media/textures/Object_HCStonepillar.png");
	o.name = "Pillar";
	o.canBarricade = false;
	o.dismantable = false;
	o.blockAllTheSquare = true;
	o.canBeAlwaysPlaced = false;
	o.noNeedHammer = true;
	o.character = character;
	o.item = item;
	return o;
end

function HCStonePillar:render(x, y, z, square)
	ISBuildingObject.render(self, x, y, z, square);
end

function HCStonePillar:isValid(square)
	local valid = self:isSquareFree(square);
	if valid and not self.character:getInventory():contains("HCStonepillar") then
		valid = false;
	end
	return valid;
end

function HCStonePillar:isSquareFree(square)
	if not square or not buildUtil.canBePlace(self, square) then return false end
	if square:getMovingObjects():size() > 0 then return false end
	if square:getStaticMovingObjects():size() > 0 then return false end
	for i=0, square:getObjects():size()-1 do
		local object = square:getObjects():get(i);
		if object:getSprite() and not object:getSprite():getProperties():Is(IsoFlagType.solidfloor) then
			if object:getType() == IsoObjectType.tree or object:getType() == IsoObjectType.wall then
				return false;
			end
			return not square:isSolidTrans();
		end
	end
	return true;
end

function HCStonePillar.isPillar(obj)
	return obj ~= nil and obj:getName() == "Pillar"
end

function HCStonePillar.LoadGridsquare(square)
	if isClient() then return end
	for i=0,square:getSpecialObjects():size()-1 do
		local obj = square:getSpecialObjects():get(i);
		if HCStonePillar.isPillar(obj) then
			HCStonePillar.loadPillar(obj);
			break;
		end
	end
end

function HCStonePillar.loadGridSquareJavaHook(sq, obj)
	if isClient() then return end
	HCStonePillar.loadPillar(obj);
end

function HCStonePillar.loadPillar(bc)
	if not bc or not bc:getSquare() then return end
	local square = bc:getSquare();
	local pillar = nil;
	for i,v in ipairs(HCStonePillar.pillars) do
		if v.x == square:getX() and v.y == square:getY() and v.z == square:getZ() then
			pillar = v;
			break
		end
	end
	if not pillar then
		pillar = {}
		pillar.x = square:getX();
		pillar.y = square:getY();
		pillar.z = square:getZ();
		table.insert(HCStonePillar.pillars, pillar);
	end
	
	bc:setSprite("media/textures/Object_HCStonepillar.png");
	bc:getSprite():getProperties():Set(IsoFlagType.collideN);
	bc:getSprite():getProperties():Set(IsoFlagType.collideW);
	bc:transmitUpdatedSpriteToClients();
end

function HCStonePillar.OnObjectAdded(obj)
	if isClient() then return end
	if HCStonePillar.isPillar(obj) then
		HCStonePillar.loadPillar(obj);
	end
end

Events.LoadGridsquare.Add(HCStonePillar.LoadGridsquare);
Events.OnObjectAdded.Add(HCStonePillar.OnObjectAdded);
Events.OnDestroyIsoThumpable.Add(HCStonePillar.onDestroy);