---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by Robotex140.
--- Based on Zomboid RainCollectorBarrel
--- DateTime: 16-Jan-22 04:08
---

require "BuildingObjects/ISBuildingObject"

-- this class extend ISBuildingObject, it's a class to help you drag around/place an item in the world
Beehive = ISBuildingObject:derive("Beehive");
-- 7 days in 10m intervals: 7*24*6 = 1008 checks
Beehive.week_unit = 1008
Beehive.HoneyMax = 12 * Beehive.week_unit

function Beehive:create(x, y, z, north, sprite)
    local cell = getWorld():getCell();
    self.sq = cell:getGridSquare(x, y, z);
    self.javaObject = IsoThumpable.new(cell, self.sq, sprite, north, self);
    buildUtil.setInfo(self.javaObject, self);
    buildUtil.consumeMaterial(self);
    self.javaObject:setMaxHealth(self:getHealth());
    self.javaObject:setHealth(self.javaObject:getMaxHealth());
    self.javaObject:setBreakSound("BreakObject");

    -- add the item to the ground
    self.sq:AddSpecialObject(self.javaObject);
    -- IsoObjects with 'honeyAmount'
    self.javaObject:getModData()["honeyMax"] = self.honeyMax;
    self.javaObject:getModData()["honeyAmount"] = -10;
    self.javaObject:getModData()["pollenAmount"] = 0;

    self.javaObject:setSpecialTooltip(true)
    self.javaObject:transmitCompleteItemToServer();
    triggerEvent("OnObjectAdded", self.javaObject)
end

function Beehive:new(player, sprite, honeyMax)
    
    -- OOP stuff
    -- we create an item (o), and return it
    local o = {};
    setmetatable(o, self);
    self.__index = self;
    o:init();
    -- the number of sprites can be up to 4, one for each direction, you ALWAYS need at least 2 sprites, south (Sprite) and north (NorthSprite)
    -- here we're not gonna be able to rotate our building (it's a barrel, so every face are the same), so we set that the south sprite = north sprite
    o:setSprite(sprite);
    o:setNorthSprite(sprite);
    o.name = "beehive_global";
    o.honeyMax = honeyMax;
    o.player = player;
    o.dismantable = false;
    o.canBarricade = false;
    o.blockAllTheSquare = true;
    return o;
end


function Beehive:getHealth()
    return 200 + buildUtil.getWoodHealth(self);
end

-- our Beehive can be placed on this square ?
-- this function is called everytime you move the mouse over a grid square, you can for example not allow building inside house..
function Beehive:isValid(square)
    if not square then return false end
    if square:isSolid() or square:isSolidTrans() then return false end
    if square:HasStairs() then return false end
    if square:HasTree() then return false end
    if not square:getMovingObjects():isEmpty() then return false end
    if not square:TreatAsSolidFloor() then return false end
    if not self:haveMaterial(square) then return false end
    for i=1,square:getObjects():size() do
        local obj = square:getObjects():get(i-1)
        if self:getSprite() == obj:getTextureName() then return false end
    end
    if buildUtil.stairIsBlockingPlacement( square, true ) then return false; end
    if square:isVehicleIntersecting() then return false end
    return true
end

-- called after render the ghost objects
-- the ISBuildingObject only render 1 sprite (north, south...), for example for stairs I can render the 2 others tile for stairs here
-- if I return false, the ghost render will be in red and I couldn't build the item
function Beehive:render(x, y, z, square)
    ISBuildingObject.render(self, x, y, z, square)
end

