HDManagementDialog = {}
local HDManagementDialog_mt = Class(HDManagementDialog, YesNoDialog)

function HDManagementDialog.new(target, customMt)
    local self = YesNoDialog.new(target, customMt or HDManagementDialog_mt)
    self.isInitializing = false
    return self
end

function HDManagementDialog.register()
    local dialog = HDManagementDialog.new()
    local path = Utils.getFilename("gui/HDManagementDialog.xml", HDUpgrade.MOD_DIR)
    g_gui:loadGui(path, "HDManagementDialog", dialog)
    HDManagementDialog.INSTANCE = dialog
end

function HDManagementDialog.show(frame, placeable, production, farmId, visitButtonInfo)
    local dialog = rawget(HDManagementDialog, "INSTANCE")
    if dialog == nil then
        return
    end
    dialog.frame = frame
    dialog.placeable = placeable
    dialog.production = production
    dialog.farmId = farmId
    dialog.visitButtonInfo = visitButtonInfo
    if visitButtonInfo ~= nil and visitButtonInfo.inputAction ~= nil then
        dialog.visitButton:setInputAction(visitButtonInfo.inputAction)
    end
    dialog:setTitle(placeable:getName())
    dialog:updateValues()
    g_gui:showDialog("HDManagementDialog")
end

function HDManagementDialog:updateValues()
    local spec = self.placeable[HDPlaceableUpgrade.SPEC_TABLE_NAME]
    local canUpgrade = HDPlaceableUpgrade.canUpgrade(self.placeable, self.farmId)
    self.visitButton:setVisible(self.visitButtonInfo ~= nil)
    self.upgradeButton:setVisible(canUpgrade)
    if canUpgrade then
        local price = HDPlaceableUpgrade.getUpgradePrice(self.placeable)
        self.upgradeButton:setText(string.format(
            g_i18n:getText("hd_upgrade_menu_button"),
            spec.level + 1,
            g_i18n:formatMoney(price, 0, true, true)
        ))
    end

    local texts = {}
    local state = 1
    local current = HDPlaceableUpgrade.getProductivity(self.placeable, self.production.id)
    for index, productivity in ipairs(HDUpgrade.PRODUCTIVITY_OPTIONS) do
        texts[index] = string.format("%d%%", productivity)
        if productivity == current then
            state = index
        end
    end
    self.isInitializing = true
    self.productivityOption:setTexts(texts)
    self.productivityOption:setState(state, true)
    self.isInitializing = false
end

function HDManagementDialog.refresh(placeable)
    local dialog = rawget(HDManagementDialog, "INSTANCE")
    if dialog ~= nil and dialog.placeable == placeable then
        dialog:updateValues()
    end
end

function HDManagementDialog:onClickUpgrade()
    HDBuildingUpgradeEvent.sendRequest(self.placeable)
    self:updateValues()
end

function HDManagementDialog:onClickProductivity(state)
    if self.isInitializing then
        return
    end
    local productivity = HDUpgrade.PRODUCTIVITY_OPTIONS[state]
    if productivity ~= nil then
        HDProductivityEvent.sendRequest(self.placeable, self.production.id, productivity)
    end
end

function HDManagementDialog:onClickVisit()
    local buttonInfo = self.visitButtonInfo
    self:close()
    if buttonInfo == nil or buttonInfo.callback == nil then
        return
    end
    local target = buttonInfo.callbackTarget or buttonInfo.target
    if target ~= nil then
        buttonInfo.callback(target)
    else
        buttonInfo.callback()
    end
end

function HDManagementDialog:onClickBack()
    self:close()
end

HDManagementDialog.register()
