local this = class("pagePlayerTitle", G_UIPageBase)

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {
    selectType = 1,
    selectId = 0,
    preTitle = {},
    sufTitle = {},
    preTitleId = 0,
    sufTitleId = 0
  }
  self.titleIdList = {}
end

function this.bind()
  return {
    allCount = "",
    selText = "",
    buttonText = "",
    lockText = "",
    nowTitle = "",
    preSelect = false,
    sufSelect = true,
    unlockbutton = true,
    lockbutton = false,
    hasCount = "",
    scrollList_title = {
      moduleName = "pages/PlayerCard/cellPlayerTitle"
    },
    forconIndex = 0
  }
end

function this.methods()
  return {
    scrollList_title = {
      onClickSelect = function(self, titleId)
        for i, v in ipairs(self.titleIdList) do
          if v == titleId then
            self.bind.scrollList_title:change(i, {choice = true})
          else
            self.bind.scrollList_title:change(i, {choice = false})
          end
        end
        if self.data.selectType == 1 then
          self.data.preTitleId = titleId
        elseif self.data.selectType == 2 then
          self.data.sufTitleId = titleId
        end
        self:rightInfoData()
      end
    },
    prefixButton = function(self)
      self.data.selectType = 1
      self:setTitleList()
      self:rightInfoData()
    end,
    suffixButton = function(self)
      self.data.selectType = 2
      self:setTitleList()
      self:rightInfoData()
    end,
    changeButton = function(self)
      local isHasTitle = false
      local id = 0
      if self.data.selectType == 1 then
        id = self.data.preTitleId
        isHasTitle = self.data.preTitle[self.data.preTitleId] ~= nil
      elseif self.data.selectType == 2 then
        id = self.data.sufTitleId
        isHasTitle = self.data.sufTitle[self.data.sufTitleId] ~= nil
      end
      if not isHasTitle then
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("msg_palyertitle_nothastitle"))
        return
      end
      L_PlayerStore:req_changePlayerTitle({
        type = self.data.selectType,
        id = self.data.selectType == 1 and self.data.preTitleId or self.data.sufTitleId
      }, function()
        self:setNowTitle()
        self:rightInfoData()
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("msg_palyertitle_changesuccess"))
      end)
    end,
    closeButton = function(self)
      L_UI:close(self.pageName)
    end,
    mainButton = function(self)
      L_UI:backToRoot()
    end
  }
end

function this:preOpen(options)
  self.data.preTitleId = L_PlayerStore:getPreTitle()
  self.data.sufTitleId = L_PlayerStore:getLaterTitle()
  self._readList = {}
  L_PlayerStore:req_getPlayerTitle(function(rspData)
    self:setNowTitle()
    self:setPlayerTitle(rspData)
    self:setTitleList()
    self:rightInfoData()
  end)
  L_ReddotManager:registerReddot(self.bindComponents.reddotPre, L_ReddotManager.DotDef.TitlePre)
  L_ReddotManager:registerReddot(self.bindComponents.reddotSuf, L_ReddotManager.DotDef.TitleSuf)
end

function this:setNowTitle()
  local preTitle = L_PlayerStore:getPreTitle()
  local sufTitle = L_PlayerStore:getLaterTitle()
  local titleA, titleB = self:getAllTitle(preTitle, sufTitle)
  self.bind.nowTitle = table.concat({titleA, titleB})
end

function this:setPlayerTitle(rspData)
  if rspData == nil then
    return
  end
  if rspData.preffix_title then
    for _, v in ipairs(rspData.preffix_title) do
      self.data.preTitle[v] = v
    end
  end
  if rspData.suffix_title then
    for _, v in ipairs(rspData.suffix_title) do
      self.data.sufTitle[v] = v
    end
  end
end

function this:setTitleList()
  local playerTitleTpl = L_GameTpl:getPlayerTitleTpl()
  self.titleIdList = {}
  self.titleIdList = playerTitleTpl:getAllId(self.titleIdList, self.data.selectType)
  table.sort(self.titleIdList, function(a, b)
    local unLockA = 0
    local unLockB = 0
    if self.data.preTitle[a] ~= nil or self.data.sufTitle[a] ~= nil then
      unLockA = 1
    end
    if self.data.preTitle[b] ~= nil or self.data.sufTitle[b] ~= nil then
      unLockB = 1
    end
    if unLockA ~= unLockB then
      return unLockA > unLockB
    end
    return a < b
  end)
  local titleData = {}
  self.hasCount = 0
  self.allCount = 0
  local selectIndex = 0
  for i, v in pairs(self.titleIdList) do
    local data = L_ItemManager:parsePlayerTitleData(v)
    local dataCell = {}
    dataCell.titleId = v
    dataCell.id = v
    dataCell.unLockTitle = data.name
    dataCell.unLockDesc = data.desc
    dataCell.title = data.name
    dataCell.desc = data.desc
    if self.data.selectType == 1 then
      selectIndex = self.data.preTitleId == v and i or selectIndex
      dataCell.choice = self.data.preTitleId == v
      dataCell.isLock = self.data.preTitle[v] == nil
      dataCell.isUnLock = self.data.preTitle[v] ~= nil
      dataCell.isSelUnlock = self.data.preTitle[v] ~= nil
    elseif self.data.selectType == 2 then
      selectIndex = self.data.sufTitleId == v and i or selectIndex
      dataCell.choice = self.data.sufTitleId == v
      dataCell.isLock = self.data.sufTitle[v] == nil
      dataCell.isUnLock = self.data.sufTitle[v] ~= nil
      dataCell.isSelUnlock = self.data.sufTitle[v] ~= nil
    end
    self.allCount = self.allCount + 1
    if self.data.preTitle[v] ~= nil or self.data.sufTitle[v] ~= nil then
      self.hasCount = self.hasCount + 1
    end
    table.insert(titleData, dataCell)
  end
  self.bind.hasCount = table.concat({
    self.hasCount,
    "/",
    self.allCount
  })
  self.bind.scrollList_title:clear()
  self.bind.scrollList_title:insert_array(titleData)
  self.bind.forconIndex = selectIndex
end

function this:rightInfoData()
  local titleA, titleB = self:getAllTitle(self.data.preTitleId, self.data.sufTitleId)
  local preText = self.data.preTitle[self.data.preTitleId] ~= nil and titleA or L_GameUtil.fillColor(titleA, "#8B8C8F")
  local suText = self.data.sufTitle[self.data.sufTitleId] ~= nil and titleB or L_GameUtil.fillColor(titleB, "#8B8C8F")
  self.bind.preSelect = self.data.selectType == 1
  self.bind.sufSelect = self.data.selectType == 2
  self.bind.selText = table.concat({
    preText,
    "   ",
    suText
  })
  local preTitle = L_PlayerStore:getPreTitle()
  local sufTitle = L_PlayerStore:getLaterTitle()
  if self.data.selectType == 1 then
    if self.data.preTitleId == preTitle then
      self:showUnlockButton(false, L_WordsTpl:getValue("ui_palyertitle_nowselecttitle"))
      return
    end
    if self.data.preTitle[self.data.preTitleId] == nil then
      self:showUnlockButton(false, L_WordsTpl:getValue("ui_palyertitle_notunlocktitle"))
      return
    end
    self:showUnlockButton(true, L_WordsTpl:getValue("ui_palyertitle_changpretitle"))
  elseif self.data.selectType == 2 then
    if self.data.sufTitleId == sufTitle then
      self:showUnlockButton(false, L_WordsTpl:getValue("ui_palyertitle_nowselecttitle"))
      return
    end
    if self.data.sufTitle[self.data.sufTitleId] == nil then
      self:showUnlockButton(false, L_WordsTpl:getValue("ui_palyertitle_notunlocktitle"))
      return
    end
    self:showUnlockButton(true, L_WordsTpl:getValue("ui_palyertitle_changsuftitle"))
  end
end

function this:showUnlockButton(isShow, showText)
  self.bind.unlockbutton = isShow
  self.bind.lockbutton = not isShow
  self.bind.lockText = showText
  self.bind.buttonText = showText
end

function this:getAllTitle(preId, sufId)
  local aTitle, bTitle
  local dataA = L_ItemManager:parsePlayerTitleData(preId)
  local dataB = L_ItemManager:parsePlayerTitleData(sufId)
  if dataA.special == 1 and dataB.special == 1 then
    aTitle = ""
    bTitle = L_WordsTpl:getValue("ui_palyertitle_titlenotselect")
  elseif dataA.special == 1 and dataB.special ~= 1 then
    aTitle = ""
    bTitle = dataB.name
  elseif dataA.special ~= 1 and dataB.special == 1 then
    aTitle = dataA.name
    bTitle = ""
  else
    aTitle = dataA.name
    bTitle = dataB.name
  end
  return aTitle, bTitle
end

function this:setRead(id)
  self._readList = self._readList or {}
  self._readList[id] = true
end

function this:close(options)
  for id, _ in pairs(self._readList) do
    L_ReddotManager:clearNew(L_ReddotManager.DotDef.Title, id)
  end
  table.clear(self._readList)
end

return this
