DiseaseHUD = { lastFarmlandId = nil }

local function getFieldByFarmlandId(farmlandId)
    if g_fieldManager and g_fieldManager.getFieldById then
        return g_fieldManager:getFieldById(farmlandId)
    end
    return nil
end

local function formatAreaHaFromField(field)
    local areaHa = 0
    if field ~= nil then
        areaHa = field.areaHa or (field.getAreaHa and field:getAreaHa()) or field.fieldArea or 0
    end
    return g_i18n:formatArea(areaHa, 2), areaHa
end

local function addGreenCureHints(box, def)
    if def == nil or def.cures == nil then return end
    local mapKey = {
        sprayer = "bmp_hint_cure_sprayer",
        weeder = "bmp_hint_cure_weeder",
        cultivator = "bmp_hint_cure_cultivator",
        plow = "bmp_hint_cure_plow",
        roller = "bmp_hint_cure_roller",
        mulcher = "bmp_hint_cure_mulcher"
    }
    for _, action in ipairs(def.cures) do
        local key = mapKey[action]
        if key ~= nil then
            box:addLine(g_i18n:getText(key), nil, true)
        end
    end
end

local function getProtectionText(value)
    local none = g_i18n:getText("bmp_dis_none") or "-"
    local pests = g_i18n:getText("bmp_mapLayerPests") or "Pests"
    local diseases = g_i18n:getText("bmp_mapLayerDiseases") or "Diseases"
    if value == 1 then return pests end
    if value == 2 then return diseases end
    if value == 3 then return pests .. " + " .. diseases end
    return none
end

function DiseaseHUD:fieldAddFarmland(_, data, box)
    if not data or data.farmlandId == nil then return end
    self.lastFarmlandId = data.farmlandId

    local field = getFieldByFarmlandId(data.farmlandId)
    if field == nil then return end

    box:addLine(g_i18n:getText("bmp_field_area"), select(1, formatAreaHaFromField(field)))

    if DiseaseCore and DiseaseCore.getOrCreateEntry then
        DiseaseCore:getOrCreateEntry(data.farmlandId)
    end

    local localThreat = DiseaseCore and DiseaseCore.getLocalThreatState and DiseaseCore:getLocalThreatState(data.farmlandId) or nil
    local diseaseStr = DiseaseCore and DiseaseCore:getDiseaseLabel(data.farmlandId) or "-"
    box:addLine(g_i18n:getText("bmp_disease_title"), diseaseStr)
    local protection = DiseaseCore and DiseaseCore.getLocalProtectionState
        and DiseaseCore:getLocalProtectionState(data.farmlandId) or 0
    box:addLine(g_i18n:getText("bmp_protection_title"), getProtectionText(protection))
end

local function computePotentialFS25(fieldState, field)
    if fieldState == nil or field == nil or g_fruitTypeManager == nil then return nil, nil end
    local ftIndex, growth = fieldState.fruitTypeIndex, fieldState.growthState
    if not ftIndex or not growth then return nil, nil end
    local ft = g_fruitTypeManager:getFruitTypeByIndex(ftIndex)
    if ft == nil or not (ft.minHarvestingGrowthState <= growth and growth <= ft.maxHarvestingGrowthState) then return nil, nil end

    local _, areaHa = formatAreaHaFromField(field)
    if areaHa <= 0 then return nil, nil end

    local mult = (fieldState.getHarvestScaleMultiplier and fieldState:getHarvestScaleMultiplier()) or 1
    local literPerSqm = ft.literPerSqm or 0
    if literPerSqm <= 0 then return nil, nil end

    local potentialLiters = literPerSqm * (areaHa * 10000) * mult
    local fillType = g_fruitTypeManager:getFillTypeByFruitTypeIndex(ft.index)
    local massPerLiter = (fillType and fillType.massPerLiter) or 1.0
    local totalMassKg = potentialLiters * massPerLiter
    local areaUnitVal = g_i18n:getArea(areaHa)
    local denom = (areaUnitVal and areaUnitVal > 0) and areaUnitVal or 1
    local yieldTPerUnit = (totalMassKg / 1000) / denom
    return potentialLiters, yieldTPerUnit
end

function DiseaseHUD:fieldAddField(_, fieldState, box)
    if self.lastFarmlandId == nil then return end
    local field = getFieldByFarmlandId(self.lastFarmlandId)
    if field == nil then return end

    local potentialLiters, yieldTPerUnit = computePotentialFS25(fieldState, field)
    if potentialLiters ~= nil then
        local baseText = g_i18n:formatVolume(potentialLiters, 0)
        box:addLine(g_i18n:getText("bmp_potential_harvest"), baseText)
    end

    if yieldTPerUnit ~= nil then
        box:addLine(g_i18n:getText("bmp_potential_yield"), string.format("%1.2f T/%s", yieldTPerUnit, tostring(g_i18n:getAreaUnit())))
    end

    if DiseaseCore ~= nil then
        local spray, plow, lime, weed, stubble, roller = 1, 1, 1, 1, 1, 1
        if fieldState.getHarvestScaleFactors ~= nil then
            spray, plow, lime, weed, stubble, roller = fieldState:getHarvestScaleFactors()
        end
        if DiseaseCore.ingestCareSnapshot then
            DiseaseCore:ingestCareSnapshot(self.lastFarmlandId, fieldState.fruitTypeIndex, {
                spray = spray, plow = plow, lime = lime, weed = weed, stubble = stubble, roller = roller
            })
        end
    end

    if DiseaseCore and DiseaseCore.fields then
        local localThreat = DiseaseCore.getLocalThreatState and DiseaseCore:getLocalThreatState(self.lastFarmlandId) or nil
        if localThreat and (localThreat.severity or 0) > 0 then
            local def = DiseaseCore.getDiseaseDef and DiseaseCore:getDiseaseDef(localThreat.threatKey) or nil
            addGreenCureHints(box, def)
        end
    end
end
