--***********************************************************
--**                    THE INDIE STONE                    **
--***********************************************************

require "ISBaseObject"

ISCampfire = ISBaseObject:derive("ISCampfire")

function ISCampfire:new(x, y, z)
	local o = {}
	setmetatable(o, self)
	self.__index = self
	o.x = x
	o.y = y
	o.z = z
	o.exterior = true
	o.isLit = false
	o.fuelAmt = 0
	o.radius = -1
	o.spriteName = "camping_01_6"
	return o
end

function ISCampfire:getSquare()
	return getCell():getGridSquare(self.x, self.y, self.z)
end

function ISCampfire:getObject()
	local square = self:getSquare()
	if square then
		return camping.findCampfireObject(square)
	end
	return nil
end

function ISCampfire:getContainer()
	local object = self:getObject()
	if not object then return nil end
	return object:getContainer()
end

function ISCampfire:getFireObject()
	local square = self:getSquare()
	if square then
		for i = 0,square:getObjects():size()-1 do
			local fireObj = square:getObjects():get(i)
			if instanceof(fireObj, 'IsoFire') and fireObj:isPermanent() then
				return fireObj
			end
		end
	end
	return nil
end

function ISCampfire:addObject()
	if self:getObject() then return end
	local square = self:getSquare()
	if not square then return end
	local object = IsoObject.new(square, "camping_01_6", "Campfire")
	object:setOutlineOnMouseover(true)
	self:toModData(object:getModData())
	square:AddTileObject(object)
	camping.noise('added campfire IsoObject at index='..object:getObjectIndex())
end

function ISCampfire:removeObject()
	local square = self:getSquare()
	local object = self:getObject()
	if not square or not object then return end
	square:transmitRemoveItemFromSquare(object)
end

function ISCampfire:setSpriteName(spriteName)
	if spriteName == self.spriteName then return end
	camping.noise('changed campfire sprite from='..self.spriteName..' to '..spriteName..' '..self.x..','..self.y..','..self.z)
	self.spriteName = spriteName
	local object = self:getObject()
	if not object then return end
	object:setSprite(spriteName)
end

function ISCampfire:addContainer()
	local square = self:getSquare()
	if not square then return end
	local object = self:getObject()
	if not object then return end
	local container = object:getContainer()
	if container then return end
	container = ItemContainer.new("campfire", square, object, 1, 1)
	container:setExplored(true)
	object:setContainer(container)
end

function ISCampfire:addFireObject()
	if self:getFireObject() then return end
	local square = self:getSquare()
	if not square then return end
	-- Create a 'permanent' IsoFire object.  It won't spread to adjacent squares but will burn.
	local fireObj = IsoFire.new(getCell(), square)
	fireObj:AttachAnim("Fire", "01", 4, IsoFireManager.FireAnimDelay, -16, -78, true, 0, false, 0.7, IsoFireManager.FireTintMod)
	square:AddTileObject(fireObj)
	camping.noise('added campfire IsoFire at index='..fireObj:getObjectIndex())
end

function ISCampfire:removeFireObject()
	local fireObj = self:getFireObject()
	if not fireObj then return end
	local square = self:getSquare()
	camping.noise('removed campfire IsoFire at index='..fireObj:getObjectIndex())
	square:transmitRemoveItemFromSquare(fireObj)
	square:getProperties():UnSet(IsoFlagType.burning)
end

function ISCampfire:transferItemsToGround()
	local square = self:getSquare()
	local object = self:getObject()
	if not square or not object then return end
	local container = object:getContainer()
	if container then
		while container:getItems():size() > 0 do
			local item = container:getItems():get(0)
			container:DoRemoveItem(item)
			square:AddWorldInventoryItem(item, 0.0, 0.0, 0.0)
		end
	end
end

function ISCampfire:fromModData(modData)
	self.exterior = modData.exterior
	self.isLit = modData.isLit or false
	self.fuelAmt = modData.fuelAmt
end

function ISCampfire:toModData(modData)
	modData.exterior = self.exterior
	modData.isLit = self.isLit
	modData.fuelAmt = self.fuelAmt
end

function ISCampfire:fromObject(object)
	self:fromModData(object:getModData())
end

