---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by Robotex140.
--- DateTime: 29-Jan-22 19:03
---

ISBeehiveMenu = {};


function ISBeehiveMenu.isValidBeehive(beehive)
    if not beehive then return false end
    beehive:updateFromIsoObject()
    return beehive:getIsoObject() ~= nil
end

ISBeehiveMenu.doBeehiveMenus = function(_player, _context, _worldObjects)

    ISBeehiveMenu.materialOnGround = buildUtil.checkMaterialOnGround(getSpecificPlayer(_player):getCurrentSquare())
    local player = _player;
    local context = _context;
    local worldobjects = _worldObjects;

    -- test for new beehive in square
    beehive_lua = nil
    for i,v in ipairs(worldobjects) do
        if CBeehiveSystem.instance:isValidIsoObject(v) then
            beehive_lua = CBeehiveSystem.instance:getLuaObjectOnSquare(v:getSquare())
            local args = { x = beehive_lua.x, y = beehive_lua.y, z = beehive_lua.z}
            local playerObj = getSpecificPlayer(0)
            sendClientCommand(playerObj, 'Beehive','syncAll', args)
        end
    end

    if beehive_lua then
        local HcMenuOption = context:addOption("beehive", worldobjects);
        HcbeeSubMenu = ISContextMenu:getNew(context);
        context:addSubMenu(HcMenuOption, HcbeeSubMenu);
        ISBeehiveMenu.show_info(player, HcbeeSubMenu, worldobjects, beehive_lua)
        ISBeehiveMenu.harvest_honey(player, HcbeeSubMenu, beehive_lua)
        if getDebug() then
            ISBeehiveMenu.add_honey(player, HcbeeSubMenu, beehive_lua)
        end
        ISBeehiveMenu.add_pollen(player, HcbeeSubMenu, beehive_lua)
    end
end


ISBeehiveMenu.show_info = function(player, HcSubMenu, worldobjects, beehive)
    local option
    local tooltip
    ISBeehiveMenu.isValidBeehive(beehive)
    option = HcSubMenu:addOption("Beehive info", nil);
    tooltip = ISBeehiveMenu.toolTipcheck(option)
    tooltip:setName("Beehive info")
    tooltip.description = string.format("%s: %.1f / 12", "honey", beehive.honeyAmount / Beehive.week_unit)
    tooltip.description = tooltip.description..string.format("\n%s: %.1f / 12", "pollen", beehive.pollenAmount / Beehive.week_unit)
    if beehive.honeyAmount < 0 then
        tooltip.description = "The bees seem to be resting. I should give them a moment to rest"
    end
    if isWinter() then
        tooltip.description = tooltip.description..string.format("\n Its winter, the bees are asleep")
    end
end

ISBeehiveMenu.harvest_honey = function(player, HcSubMenu, beehive)
    local option
    local tooltip
    option = HcSubMenu:addOption("Harvest Honey", nil, function() ISBeehiveMenu.onHarvestHoney(player, beehive) end);
    tooltip = ISBeehiveMenu.toolTipcheck(option)

    tooltip:setName("Beehive info")
    tooltip.description = string.format("%d honey combs available",  math.floor(beehive.honeyAmount/Beehive.week_unit))
end

ISBeehiveMenu.check_harvest_honey_items = function(player, beehive)
    if beehive.honeyAmount >= Beehive.week_unit then
        local hats = ISBeehiveMenu.countMaterial(player, "Hydrocraft.HCBeekeeperhat")
        local gloves = ISBeehiveMenu.countMaterial(player, "Hydrocraft.HCWorkgloves")
        local smokersUse = ISBeehiveMenu.countDrainableUses(player, "Hydrocraft.HCBeesmokerfull")

        if not (hats > 0) then getPlayer():Say("missing beekeeper hat"); return false end
        if not (gloves > 0) then getPlayer():Say("missing work gloves") return false end
        if not (smokersUse > 0) then getPlayer():Say("missing bee smoker uses") return false end
        return true
    else
        getPlayer():Say("hmmm. there isnt enough honey");
    end
    return false
end

ISBeehiveMenu.onHarvestHoney = function (player, beehive)
    if not ISBeehiveMenu.isValidBeehive(beehive) then return end
    if ISBeehiveMenu.check_harvest_honey_items(player, beehive) then
        local playerObj = getSpecificPlayer(player)
        local square = getCell():getGridSquare(beehive.x, beehive.y, beehive.z)
        ISBeehiveMenu.walkToObject(playerObj, square)
        ISTimedActionQueue.add(ISHarvestHoneyAction:new(playerObj, beehive, 200))
    end
end

ISBeehiveMenu.add_honey = function(player, HcSubMenu, beehive)
    local option
    local tooltip
    option = HcSubMenu:addOption("Add Honey", nil, function() ISBeehiveMenu.onAddHoney(player, beehive) end);
    tooltip = ISBeehiveMenu.toolTipcheck(option)
    tooltip:setName("Add honey cheat")
    tooltip.description = "add 1008 honey, enough for one comb"
end

ISBeehiveMenu.onAddHoney = function (player, beehive)
    if not ISBeehiveMenu.isValidBeehive(beehive) then return end
    beehive.honeyAmount = beehive.honeyAmount + 1008
end

ISBeehiveMenu.add_pollen = function(player, HcSubMenu, beehive)
    local option
    local tooltip
    option = HcSubMenu:addOption("Add Pollen", nil, function() ISBeehiveMenu.onAddPollen(player, beehive) end);
    tooltip = ISBeehiveMenu.toolTipcheck(option)
    tooltip:setName("Add pollen")
    tooltip.description = string.format("%.1f pollen",  beehive.pollenAmount / Beehive.week_unit)
end

ISBeehiveMenu.onAddPollen = function (player, beehive)
    if not ISBeehiveMenu.isValidBeehive(beehive) then return end
    local inv = getSpecificPlayer(player):getInventory()
    local pollen = inv:getItemCount("Hydrocraft.HCPollen")
    if not((beehive.pollenAmount + Beehive.week_unit) <= beehive.honeyMax) then
        getPlayer():Say("beehive is full on pollen");
        return
    end
    if pollen > 0 then
        local playerObj = getSpecificPlayer(player)
        local square = getCell():getGridSquare(beehive.x, beehive.y, beehive.z)
        ISBeehiveMenu.walkToObject(playerObj, square)
        ISTimedActionQueue.add(ISAddPollenAction:new(playerObj, beehive, 100))
    else
        getPlayer():Say("hmmm. Im missing pollen");
    end
end


ISBeehiveMenu.countMaterial = function(player, fullType)
    local inv = getSpecificPlayer(player):getInventory()
    local count = inv:getItemCount(fullType)
    local type = string.split(fullType, "\\.")[2]
    for k,v in pairs(ISBeehiveMenu.materialOnGround) do
        if k == type then count = count + v end
    end
    return count
end

ISBeehiveMenu.countDrainableUses = function(player, fullType)
    local inv = getSpecificPlayer(player):getInventory()
    local smokers = inv:getItemsFromFullType(fullType)
    local uses = 0
    for x=0, smokers:size()-1 do
        local item = smokers:get(x)
        uses = uses + item:getDrainableUsesInt()
    end
    return uses
end

function ISBeehiveMenu.walkToObject(playerObj, square)
    if ISBeehiveMenu.cheat or playerObj:isTimedActionInstant() then
        return true;
    end
    if AdjacentFreeTileFinder.isTileOrAdjacent(playerObj:getCurrentSquare(), square) then
        return true
    end
    local adjacent = AdjacentFreeTileFinder.Find(square, playerObj)
    if adjacent == nil then return false end
    ISTimedActionQueue.add(ISWalkToTimedAction:new(playerObj, adjacent))
    return true
end


ISBeehiveMenu.toolTipcheck = function(option)

    local _tooltip = ISToolTip:new()
    _tooltip:initialise()
    _tooltip:setVisible(false)
    option.toolTip = _tooltip

    return _tooltip
end


local function func_Init()
    Events.OnPreFillWorldObjectContextMenu.Add(ISBeehiveMenu.doBeehiveMenus)
end

Events.OnGameStart.Add(func_Init)