local SideBarBaseCellComp = require "app/ui/main_city/cell/side_bar_base_cell"
local SideBarGoldPigCell = class("SideBarGoldPigCell", SideBarBaseCellComp)

function SideBarGoldPigCell:getModuleKey()
    return ModuleManager.MODULE_KEY.GOLD_PIG
end

function SideBarGoldPigCell:getIsOpen()
    return DataManager.GoldPigData:getIsOpen()
end

function SideBarGoldPigCell:getSpineName()
    return "ui_main_btn_pig"
end

function SideBarGoldPigCell:onClick()
    ModuleManager.ActivityManager:showGoldPigUI(BIReport.PAY_UI_SHOW_TYPE.CLICK_SHOW)
end

function SideBarGoldPigCell:onRefresh()
    local isFull = DataManager.GoldPigData:getIsFull()
    if isFull then
        self:showGoldPigDesc()
    else
        self:hideGoldPigDesc()
    end
end

function SideBarGoldPigCell:showGoldPigDesc()
    if self.descBgVisible then
        return
    end
    self.descBgVisible = true
    self.descBg:setVisible(true)
    self.timeBg:setVisible(true)

    local gemEntity = DataManager.BagData.ItemData:getItemById(GConst.ItemConst.ITEM_ID_GEM)
    self.descIcon:setSprite(gemEntity:getIconRes())
    self.descTx:setText(I18N:getGlobalText(I18N.GlobalConst.SIDE_BAR_FULL))
    GFunc.centerImgAndTx(self.descIcon, self.descTx, 0, -3)

    local endTime = DataManager.GoldPigData:getEndTime()
    local remainTime = endTime - Time:getServerTime()
    if remainTime >= 0 then
        self.timeTx:setText(GFunc.getTimeStrWithHMS(remainTime))
    else
        self.timeTx:setText(GConst.EMPTY_STRING)
    end
end

function SideBarGoldPigCell:hideGoldPigDesc()
    if self.descBgVisible == false then
        return
    end
    self.descBgVisible = false
    self.descBg:setVisible(false)
    self.timeBg:setVisible(false)
end

function SideBarGoldPigCell:updateTime()
    if self:getIsOpen() then
        local isFull = DataManager.GoldPigData:getIsFull()
        if isFull then
            self:showGoldPigDesc()
            local endTime = DataManager.GoldPigData:getEndTime()
            local remainTime = endTime - Time:getServerTime()
            if remainTime >= 0 then
                self.timeTx:setText(GFunc.getTimeStrWithHMS(remainTime))
            end
        else
            self:hideGoldPigDesc()
        end
    else
        self:closeBtn()
    end
end

return SideBarGoldPigCell