local CommonExchangeUI = class("CommonExchangeUI", BaseUI)

function CommonExchangeUI:onPressBackspace()
	self:closeUI()
end

function CommonExchangeUI:isFullScreen()
    return false
end

function CommonExchangeUI:ctor(params)
    self.defaultNum = params.defaultNum
    self.remainNum = params.remainNum
    self.reward = params.reward
    self.cost = params.cost
    self.callback = params.callback
    local costId = GFunc.getRewardId(self.cost)
    local costNum = GFunc.getRewardNum(self.cost)
    local num = DataManager.BagData.ItemData:getItemNumById(costId)
    local maxBuyCount = num // costNum
    self.remainNum = math.min(self.remainNum, maxBuyCount)
    -- if self.remainNum <= 0 then
    --     self.remainNum = 1
    -- end
end

function CommonExchangeUI:getPrefabPath()
    return "assets/prefabs/ui/common/exchange_ui.prefab"
end

function CommonExchangeUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
end

function CommonExchangeUI:onClose()
    if self.inputComp then
        self.inputComp.onValueChanged:RemoveAllListeners()
    end
end

function CommonExchangeUI:_display()
    local uiMap = self.root:genAllChildren()
    if not self.rewardCell then
        self.rewardCell = CellManager:addCellComp(uiMap["exchange_ui.bg.reward_cell"], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    end
    self.rewardCell:refreshByConfig(self.reward)
    uiMap["exchange_ui.bg.tx_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_18))
    uiMap["exchange_ui.bg.tx_remain"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_19, self.remainNum))
    uiMap["exchange_ui.bg.ok_btn.tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_18))

    self.inputComp = uiMap["exchange_ui.bg.input"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TMP_INPUT_FIELD)
    self.inputComp.text = tostring(self.defaultNum)
    self.inputComp.onValueChanged:AddListener(function(str)
        local count = self.defaultNum
        if str and tonumber(str) then
            count = math.floor(tonumber(str) + 0.0001)
        end

        if count < 1 then
            count = 1
        end

        if count > self.remainNum then
            count = self.remainNum
        end
        self.defaultNum = count

        self:refreshCountInfo()
    end)

    self:refreshCountInfo()
end

function CommonExchangeUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["exchange_ui.bg.ok_btn"]:addClickListener(function()
        if self.defaultNum <= 0 then
            return
        end

        if self.callback then
            self.callback(self.defaultNum)
        end
        self:closeUI()
    end)

    uiMap["exchange_ui.bg.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["exchange_ui.bg.btn_reduce"]:addClickListener(function()
        self.defaultNum = self.defaultNum - 1
        self:refreshCountInfo()
    end)

    uiMap["exchange_ui.bg.btn_min"]:addClickListener(function()
        self.defaultNum = 1
        self:refreshCountInfo()
    end)

    uiMap["exchange_ui.bg.btn_add"]:addClickListener(function()
        self.defaultNum = self.defaultNum + 1
        self:refreshCountInfo()
    end)

    uiMap["exchange_ui.bg.btn_max"]:addClickListener(function()
        self.defaultNum = self.remainNum
        self:refreshCountInfo()
    end)
end

function CommonExchangeUI:refreshCountInfo()
    if self.defaultNum < 1 then
        self.defaultNum = 1
    end
    local costId = GFunc.getRewardId(self.cost)
    if self.defaultNum > self.remainNum then
        self.defaultNum = self.remainNum
    end
    local uiMap = self.root:genAllChildren()
    local costIcon = uiMap["exchange_ui.bg.ok_btn.icon"]
    costIcon:setSprite(ModuleManager.ItemManager:getItemIcon(costId))
    local singlePrice = GFunc.getRewardNum(self.cost)
    local costNum = uiMap["exchange_ui.bg.ok_btn.text"]
    costNum:setText(singlePrice * self.defaultNum)
    GFunc.centerImgAndTx(costIcon, costNum, 5)
    self.inputComp.text = tostring(self.defaultNum)
end

return CommonExchangeUI