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

if isClient() then return end

require "Map/SGlobalObject"
require "Beehive/BuildingObjects/Beehive"

SBeehiveGlobalObject = SGlobalObject:derive("SBeehiveGlobalObject")

function SBeehiveGlobalObject:new(luaSystem, globalObject)
    local o = SGlobalObject.new(self, luaSystem, globalObject)
    return o
end

function SBeehiveGlobalObject:getObject()
    return self:getIsoObject()
end

function SBeehiveGlobalObject:initNew()
    self.exterior = false
    self.honeyAmount = -10
    self.honeyMax = Beehive.HoneyMax
    self.pollenAmount = 0
end

function SBeehiveGlobalObject:stateFromIsoObject(isoObject)
    self.exterior = isoObject:getSquare():isOutside()
    self.honeyMax = isoObject:getModData().honeyMax
    self.honeyAmount = isoObject:getModData().honeyAmount
    self.pollenAmount = isoObject:getModData().pollenAmount

    isoObject:getModData().honeyMax = self.honeyMax
    self:changeSprite()
    isoObject:transmitModData()
end

function SBeehiveGlobalObject:stateToIsoObject(isoObject)
    -- Sanity check
    if not self.honeyAmount then self.honeyAmount = 0 end
    if not self.pollenAmount then self.pollenAmount = 0 end
    if not self.honeyMax then self.honeyMax = Beehive.HoneyMax end

    self.exterior = isoObject:getSquare():isOutside()

    isoObject:getModData().honeyMax = self.honeyMax
    self:changeSprite()
    isoObject:transmitModData()
end

function SBeehiveGlobalObject:canHarvest()
    return self.honeyAmount >= Beehive.week_unit
end

function SBeehiveGlobalObject:harvest()
    local combs = math.floor(self.honeyAmount/Beehive.week_unit)
    self.honeyAmount = self.honeyAmount - combs*Beehive.week_unit
    self:saveData()
end

function SBeehiveGlobalObject:addPollen()
    self.pollenAmount = self.pollenAmount + Beehive.week_unit
    local isoObject = self:getIsoObject()
    isoObject:transmitModData()
    self:saveData()
    self:changeSprite()
end

function SBeehiveGlobalObject:syncAll()
    self:saveData()
end

function SBeehiveGlobalObject:addObject()
    if self:getObject() then return end
    local square = self:getSquare()
    if not square then return end
    local isoObject = IsoObject.new(square, "hcBuildingBeehive_00_0", "beehive_global")
    isoObject:setOutlineOnMouseover(true)
    self:toModData(isoObject:getModData())
    square:AddTileObject(isoObject)
end

function SBeehiveGlobalObject:removeObject()
    if self:getObject() then return end
    local square = self:getSquare()
    if not square then return end
    local isoObject = IsoObject.new(square, "camping_01_6", "Campfire")
    isoObject:setOutlineOnMouseover(true)
    self:toModData(isoObject:getModData())
    square:AddTileObject(isoObject)
end


function SBeehiveGlobalObject:fromModData(modData)
    self.exterior = modData.exterior
    self.honeyAmount = modData.honeyAmount
    self.honeyMax = modData.honeyMax
    self.pollenAmount = modData.pollenAmount
end

function SBeehiveGlobalObject:toModData(modData)
    modData.exterior = self.exterior
    modData.honeyAmount = self.honeyAmount
    modData.honeyMax = self.honeyMax
    modData.pollenAmount = self.pollenAmount
end

function SBeehiveGlobalObject:fromObject(isoObject)
    self:fromModData(isoObject:getModData())
end

function SBeehiveGlobalObject:syncIsoObject()
    self.transmitObject = false
    if not self:getIsoObject() then
        self:addObject()
        self.transmitObject = true
    end
end

function SBeehiveGlobalObject:syncModData()
    local isoObject = self:getIsoObject()
    if isoObject then
        self:toModData(isoObject:getModData())
    end
end

function SBeehiveGlobalObject:saveData()
    local isoObject = self:getObject()
    if isoObject then
        self:toModData(isoObject:getModData())
        isoObject:transmitModData()
    end
end

function SBeehiveGlobalObject:changeSprite()
    local isoObject = self:getIsoObject()
    if not isoObject then return end
    local spriteName = nil
    if self.honeyAmount < 0  then
            spriteName = "hcBuildingBeehive_00_0"
    elseif self.honeyAmount >= 0  then
            spriteName = "hcBuildingBeehive_01_0"
    end
    if self.honeyAmount == self.honeyMax then
            spriteName = "hcBuildingBeehive_02_0"
    end

    if spriteName and (not isoObject:getSprite() or spriteName ~= isoObject:getSprite():getName()) then
        self:noise('sprite changed to '..spriteName..' at '..self.x..','..self.y..','..self.z)
        isoObject:setSprite(spriteName)
        isoObject:transmitUpdatedSpriteToClients()
    end
end

