local MailUI = class("MailUI", BaseUI)

local MAIL_CELL = "app/ui/mail/cell/mail_cell"

function MailUI:isFullScreen()
    return false
end

function MailUI:getPrefabPath()
    return "assets/prefabs/ui/mail/mail_ui.prefab"
end

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

function MailUI:ctor()
    self.mailList = {}
end

function MailUI:onClose()
    DataManager.MailData:setLastMailId()
end

function MailUI:dealData()
    local list = {}
    local count = 0
    for id, entity in pairs(DataManager.MailData:getMails()) do
        if not entity:isOver() then
            table.insert(list, entity)
            count = count + 1
        end
    end

    table.sort(list, function(a, b)
        local aCan = a:canDelete()
        local bCan = b:canDelete()
        if aCan == bCan then
            local aCd = a:getRemainCd()
            local bCd = b:getRemainCd()
            if aCd == bCd then
                return a:getId() > b:getId()
            else
                return aCd < bCd
            end
        else
            return not aCan
        end
    end)

    if count ~= #self.mailList then
        self.needRefills = true
    end
    self.mailList = list
end

function MailUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()

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

function MailUI:_display()
    local uiMap = self.root:genAllChildren()
	self.root:addClickListener(function()
		self:closeUI()
	end)
    uiMap["mail_ui.bg.title_bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.MAIL_TITLE))
    uiMap["mail_ui.bg.btn_delect.btn_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_DELETE_ALL_READ_MAIL))
    uiMap["mail_ui.bg.btn_claimed.btn_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM_ALL))

    self.emptyBg = uiMap["mail_ui.bg.empty"]
end

function MailUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["mail_ui.bg.btn_close"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["mail_ui.bg.btn_delect"]:addClickListener(function()
        ModuleManager.MailManager:deleteMail()
    end)

    uiMap["mail_ui.bg.btn_claimed"]:addClickListener(function()
        ModuleManager.MailManager:claimMail()
    end)
end

function MailUI:refreshScrollRect()
    if self.scrollRect then
        if self.needRefills then
            self.scrollRect:clearCells()
            self.scrollRect:refillCells(#self.mailList)
            if #self.mailList > 0 then
                self.emptyBg:setVisible(false)
            else
                self.emptyBg:setVisible(true)
            end
        else
            self.scrollRect:updateAllCell()
        end
        self.needRefills = false
        return
    end

    local uiMap = self.root:genAllChildren()
    local scrollView = uiMap["mail_ui.bg.img_2.scrollrect"]
    self.scrollRect = scrollView:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRect:addInitCallback(function()
		return MAIL_CELL
	end)
	self.scrollRect:addRefreshCallback(function(index, cell)
		cell:refresh(self.mailList[index])
	end)
    self.scrollRect:clearCells()
    self.scrollRect:refillCells(#self.mailList)

    if #self.mailList > 0 then
        self.emptyBg:setVisible(false)
    else
        self.emptyBg:setVisible(true)
    end
    self.needRefills = false
end

function MailUI:updateTime()
    self:dealData()
    self:refreshScrollRect()
end

return MailUI