local MailDetailUI = class("MailDetailUI", BaseUI)

function MailDetailUI:isFullScreen()
    return false
end

function MailDetailUI:getPrefabPath()
    return "assets/prefabs/ui/mail/mail_detail_ui.prefab"
end

function MailDetailUI:ctor(params)
    self.entity = DataManager.MailData:getMails()[params.id]
end

function MailDetailUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
    self:_bind()

    self.sid = self:scheduleGlobal(function()
		self:updateTime()
	end, 1)
    self:updateTime()
end

function MailDetailUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["mail_detail_ui.bg.btn_claimed.btn_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
    uiMap["mail_detail_ui.bg.tx_1"]:setText(self.entity:getMailName())
    uiMap["mail_detail_ui.bg.tx_4"]:setText(I18N:getGlobalText(I18N.GlobalConst.EXTRA_REWARDS))
    uiMap["mail_detail_ui.bg.btn_claimed"]:setActive(not self.entity:isReceived())

    local content = uiMap["mail_detail_ui.bg.tx_3"]
    content:setText(self.entity:getMailDesc())
    content:setSizeDeltaY(content:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredHeight)
    content:setAnchoredPositionY(0)

    local haveReward = self.entity:haveReward()
    uiMap["mail_detail_ui.bg.img_bg"]:setVisible(haveReward)
    local contentScroll = uiMap["mail_detail_ui.bg.scrollrect"]
    if haveReward then
        contentScroll:setSizeDeltaY(180)
    else
        contentScroll:setSizeDeltaY(351)
    end

    self:refreshScrollRect()
end

function MailDetailUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["mail_detail_ui.bg.btn_claimed"]:addClickListener(function()
        ModuleManager.MailManager:claimMail({self.entity:getId()})
    end)

    uiMap["mail_detail_ui.bg.btn_close"]:addClickListener(function()
        local haveReward = self.entity:haveReward()
        if haveReward then
            ModuleManager.MailManager:claimMail({self.entity:getId()})
            self:closeUI()
        else
            self:closeUI()
        end
    end)
end

function MailDetailUI:_bind()
    self:bind(DataManager.MailData, "isDirty", function()
        local uiMap = self.root:genAllChildren()
        uiMap["mail_detail_ui.bg.btn_claimed"]:setActive(not self.entity:isReceived())
        self:refreshScrollRect()
    end)
end

function MailDetailUI:refreshScrollRect()
    if not self.entity then
        return
    end

    if self.scrollRect then
        self.scrollRect:updateAllCell()
        return
    end

    local uiMap = self.root:genAllChildren()
    local scrollView = uiMap["mail_detail_ui.bg.img_bg.scrollrect"]
    self.scrollRect = scrollView:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRect:addInitCallback(function()
		return GConst.TYPEOF_LUA_CLASS.REWARD_CELL
	end)
	self.scrollRect:addRefreshCallback(function(index, cell)
		cell:refreshByConfig(self.entity:getRewards()[index], self.entity:isReceived(), self.entity:isReceived())
	end)
    self.scrollRect:clearCells()
    self.scrollRect:refillCells(#self.entity:getRewards())
end

function MailDetailUI:updateTime()
    local uiMap = self.root:genAllChildren()
    uiMap["mail_detail_ui.bg.tx_2"]:setText(self.entity:getRemainCdStr())
end


return MailDetailUI