local MailCell = class("MailCell", BaseCell)

local TITLE_ICON = {
    [GConst.MailConst.MAIL_STATE.NOT_READ] = "mail_dec_1",
    [GConst.MailConst.MAIL_STATE.READED] = "mail_dec_2",
    [GConst.MailConst.MAIL_STATE.RECEIVED] = "mail_dec_2",
}

local AD_TITLE_ICON = "mail_dec_1"

function MailCell:refresh(entity)
    if not entity then
        return
    end

    local uiMap = self:getUIMap()
    local normalNode = uiMap["mail_cell.bg_normal"]
    local adNode = uiMap["mail_cell.bg_ad"]
    local readNode = uiMap["mail_cell.bg_read"]
    local isReadType = false
    if entity:getMailType() == GConst.MailConst.MAIL_TYPE.CUSTOM then
        normalNode:setVisible(false)
        adNode:setVisible(false)
        readNode:setVisible(true)
        isReadType = true
    elseif entity:getMailType() == GConst.MailConst.MAIL_TYPE.AD then
        normalNode:setVisible(false)
        adNode:setVisible(true)
        readNode:setVisible(false)
    elseif entity:getMailType() == GConst.MailConst.MAIL_TYPE.NORMAL then
        normalNode:setVisible(true)
        adNode:setVisible(false)
        readNode:setVisible(false)
    elseif entity:getMailType() == GConst.MailConst.MAIL_TYPE.READ then
        normalNode:setVisible(false)
        adNode:setVisible(false)
        readNode:setVisible(true)
        isReadType = true
    end
    GFunc.setAdsSprite(uiMap["mail_cell.bg_ad.claim_btn.ad"])
    uiMap["mail_cell.bg_normal.claim_btn.tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
    uiMap["mail_cell.bg_read.claim_btn.tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_READ))
    uiMap["mail_cell.bg_normal.claim_btn"]:addClickListener(function()
        self:claimMail(entity)
    end)
    uiMap["mail_cell.bg_ad.claim_btn"]:addClickListener(function()
        self:claimMail(entity)
    end)
    uiMap["mail_cell.bg_read.claim_btn"]:addClickListener(function()
        BIReport:postMailClick(entity:getId())
        ModuleManager.MailManager:readMail(entity:getId())
    end)

    local titleIcon = uiMap["mail_cell.title_icon"]
    local icon = TITLE_ICON[entity:getState()] or TITLE_ICON[GConst.MailConst.MAIL_STATE.NOT_READ]
    if entity:notRead() and entity:getMailType() == GConst.MailConst.MAIL_TYPE.AD then
        icon = AD_TITLE_ICON
    end
    if self.icon ~= icon then
        titleIcon:setSprite(GConst.ATLAS_PATH.UI_MAIL, icon, function()
            titleIcon:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):SetNativeSize()
        end)
        self.icon = icon
    end
    uiMap["mail_cell.new"]:setVisible(entity:showNew())
    uiMap["mail_cell.desc"]:setText(entity:getMailName())
    uiMap["mail_cell.time"]:setText(entity:getRemainCdStr())
    local rewardBg = uiMap["mail_cell.reward_bg"]
    local mask = uiMap["mail_cell.reward_bg.mask"]
    if not self.rewardCell then
        self.rewardCell = CellManager:addCellComp(uiMap["mail_cell.reward_cell"], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    end
    local rewards = entity:getRewards()
    local showCheck = entity:isReceived()
    if rewards[2] then
        rewardBg:setVisible(true)
        self.rewardCell:getBaseObject():setVisible(false)
        mask:setVisible(showCheck)
    else
        rewardBg:setVisible(false)
        if entity:haveReward() then
            self.rewardCell:getBaseObject():setVisible(true, 0.7)
            self.rewardCell:refreshByConfig(rewards[1], showCheck, showCheck)
        else
            self.rewardCell:getBaseObject():setVisible(false)
        end
    end
    if isReadType then
        if showCheck then
            uiMap["mail_cell.bg_read"]:setImageGray(true)
            uiMap["mail_cell.bg_read.claim_btn"]:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_3")
        elseif not entity:haveReward() and entity:isRead() then
            uiMap["mail_cell.bg_read"]:setImageGray(true)
            uiMap["mail_cell.bg_read.claim_btn"]:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_3")
        else
            uiMap["mail_cell.bg_read"]:setImageGray(false)
            uiMap["mail_cell.bg_read.claim_btn"]:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_3")
        end
    end
end

function MailCell:claimMail(entity)
    if not entity:canClaim() then
        return
    end

    if entity:getIsAdMail() then
        SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.MAIL, function()
            ModuleManager.MailManager:claimMail({entity:getId()})
        end)
    elseif entity:getIsNormalMail() then
        ModuleManager.MailManager:claimMail({entity:getId()})
    end
end

return MailCell