local module = class("cellMailItem", G_UIModuleBase)

function module.bind()
  return {
    icon = "",
    title = "",
    sender = "",
    time = "",
    titleAlpha = "",
    senderAlpha = "",
    timeAlpha = "",
    iconAct = true,
    noRewardUnread = false,
    noRewardRead = false,
    canReward = true,
    noRead = false,
    readActive = false,
    rewardMask = false,
    collectIconActive = false,
    redDotActive = true,
    unselectedBgActive = true,
    selectedBgActive = false
  }
end

function module:refresh()
  if not self.isBind then
    return
  end
  if self.bind.choiceComp == nil then
    return
  end
  local mailTpl = L_GameTpl:getMailTpl()
  local mailBonus = L_GameTpl:getMailBonusCTpl()
  local time1, time2, leftTime = L_TimeUtil.getLeftDayAndHour(self.bind.effecttm + self.bind.createtm)
  if self.bind.guid ~= 0 and time1 ~= nil or self.bind.effecttm == 0 or self.bind.collect then
    local title = ""
    local sender = ""
    local tempMailTpl = mailTpl:getTplById(self.bind.template_id)
    if tempMailTpl and mailTpl:getType(tempMailTpl) == L_Const.mailType.type1 then
      title = mailBonus:getWordsById(mailTpl:getTitle(tempMailTpl))
      sender = mailBonus:getWordsById(mailTpl:getName(tempMailTpl))
    else
      title = self.bind.title
      sender = tostring(self.bind.sender_id)
    end
    local tempTime = ""
    if time1 ~= nil and time2 ~= nil then
      tempTime = time1 and L_WordsTpl:getValue("ui_colloect_day_expire", {
        [0] = time2
      }) or L_WordsTpl:getValue("ui_colloect_hour_expire", {
        [0] = time2
      })
      if leftTime < 3600 then
        tempTime = L_WordsTpl:getValue("ui_colloect_min_expire")
      end
    end
    local tempIcon = ""
    local tempIconAct = false
    local reward
    if self.bind.reward and self.bind.reward.rewards then
      reward = self.bind.reward.rewards[1]
    end
    if reward ~= nil then
      local dataReward = L_ItemManager:parseItem(reward.itemtype, reward.itemid)
      tempIcon = dataReward.icon
      tempIconAct = true
    end
    local tempNoRewardUnread = not self.bind.read and not tempIconAct
    local tempNoRewardRead = self.bind.read and not tempIconAct
    local rewardMask = tempIconAct and self.bind.fetch
    local checkmark = L_MailStore:getCheckedMail() == self.bind.guid
    if checkmark and self:getTrans() ~= nil then
      self.bind.choiceComp.followTarget = self:getTrans()
    end
    local noRead = not self.bind.read
    local read = self.bind.read
    if reward ~= nil then
      noRead = not self.bind.fetch
      read = self.bind.fetch
    end
    self.bind.redDotActive = noRead and not self.bind.collect
    self.checkMark = checkmark
    self.bind.title = title
    self.bind.titleAlpha = title
    self.bind.icon = tempIcon
    self.bind.sender = sender
    self.bind.senderAlpha = sender
    self.bind.time = tempTime
    self.bind.timeAlpha = tempTime
    self.bind.noRead = noRead
    self.bind.readActive = read
    self.bind.iconAct = tempIconAct
    self.bind.canReward = tempIconAct and not self.bind.collect
    self.bind.noRewardUnread = tempNoRewardUnread
    self.bind.noRewardRead = tempNoRewardRead
    self.bind.rewardMask = rewardMask
    self.bind.collectIconActive = self.bind.collect
  end
end

function module.methods()
  return {
    onClick_toggleBtn = function(self)
      self:emit("mailInfo", self.bind.guid)
    end
  }
end

function module:close(options)
  if self.checkMark and self.bind and self.bind.choiceComp then
    self.bind.choiceComp.followTarget = nil
  end
end

function module:getTrans()
  if self.bindComponents then
    return self.bindComponents.trans
  end
end

return module
