local this = class("moduleAccessoryBagContent", G_UIModuleBase)
local _tabTpl = L_GameTpl:getCommonTabTpl()
local _accessoryTpl = L_GameTpl:getAccessoryTpl()
local colNum = 5
local rowNum = 6
local Type2Tab = {
  [L_AccessoryConst.accessoryType.weapon] = 101,
  [L_AccessoryConst.accessoryType.coat] = 102,
  [L_AccessoryConst.accessoryType.pants] = 103,
  [L_AccessoryConst.accessoryType.earring] = 104,
  [L_AccessoryConst.accessoryType.ring] = 105
}

function this.bind()
  return {
    go_normal = true,
    txt_pretitle = L_WordsTpl:getValue("menu_char_equipment"),
    txt_title = "",
    go_equipBtn = false,
    go_equipBtnSingle = false,
    go_replaceBtn = false,
    go_replaceBtnSingle = false,
    go_strength_left = false,
    go_strength_right = false,
    go_equipInfo = false,
    go_selectInfo = false,
    module_equipInfo = {
      moduleName = "pages/bag/moduleBagTip"
    },
    toggle_equip = true,
    module_selectInfo = {
      moduleName = "pages/bag/moduleBagTip"
    },
    toggle_select = true,
    scrollList_accessory = {
      moduleName = "pages/accessory/cellReplaceAccessory"
    },
    module_sortFilter = L_Const.ModuleInfo.SortFilterModule,
    toggle_isEmpty = false,
    go_bg_black = false,
    go_compare_arrow = false,
    go_btn_black = true,
    go_btn_yellow = false,
    trans_equipInfo_pos = C_Vector3(0, 0, 0),
    trans_selectinfo_pos = C_Vector3(0, 0, 0)
  }
end

function this.methods()
  return {
    onClick_equip = function(self)
      L_AccessoryManager:equipAccessory(self._selectHeroGuid, self._selectGuid, function()
        if not self.isBind then
          return
        end
        self:initCurEquipAccessoryId()
        self:updateSelectGuid()
      end)
    end,
    onClick_equipSingle = function(self)
      L_AccessoryManager:equipAccessory(self._selectHeroGuid, self._selectGuid, function()
        if not self.isBind then
          return
        end
        self:initCurEquipAccessoryId()
        self:updateSelectGuid()
      end)
    end,
    onClick_replace = function(self)
      L_AccessoryManager:equipAccessory(self._selectHeroGuid, self._selectGuid, function()
        if not self.isBind then
          return
        end
        self:initCurEquipAccessoryId()
        self:updateSelectGuid()
      end)
    end,
    onClick_replaceSinlg = function(self)
      L_AccessoryManager:equipAccessory(self._selectHeroGuid, self._selectGuid, function()
        if not self.isBind then
          return
        end
        self:initCurEquipAccessoryId()
        self:updateSelectGuid()
      end)
    end,
    onClick_strength_equip = function(self)
      self.parent:moduleAccessoryClose()
      L_UI:open("pageAccessoryStrength", {
        accessoryId = self._equippedGuid
      })
    end,
    onClick_strength_select = function(self)
      self.parent:moduleAccessoryClose()
      L_UI:open("pageAccessoryStrength", {
        accessoryId = self._selectGuid
      })
    end,
    onClick_equiptment_black = function(self)
      self.isCompare = true
      self:updateSelectGuid()
    end,
    onClick_equiptment_yellow = function(self)
      self.isCompare = false
      self:updateSelectGuid()
    end,
    onClick_bg_black = function(self)
      self.parent:moduleAccessoryClose()
    end,
    scrollList_accessory = {
      onClick_item = function(self, bind)
        self._selectGuid = bind.guid
        self:updateSelectGuid()
        self:setAccessoryListActive()
      end,
      onClick_remove = function(self, bind)
        if self._equippedGuid ~= 0 then
          self:resetSelectData()
          L_AccessoryManager:removeAccessory(self._equippedGuid, function()
            if not self.isBind then
              return
            end
            self:initCurEquipAccessoryId()
            self:updateSelectGuid()
            self:setAccessoryListActive()
          end)
        end
      end
    }
  }
end

function this:open()
  if C_ResolutionUtility.CheckSuperWidthScreen() then
    self.bindComponents.gridView_accessory.gridContentConstraint = 7
    self.bindComponents.rect_accessoryBag.anchoredPosition = C_Vector2(618, self.bindComponents.rect_accessoryBag.anchoredPosition.y)
    self.bindComponents.rect_accessoryBag.sizeDelta = C_Vector2(1218, self.bindComponents.rect_accessoryBag.sizeDelta.y)
    self.bindComponents.rect_bottomNode.offsetMax = C_Vector2(-50, self.bindComponents.rect_bottomNode.offsetMax.y)
    self.bindComponents.rect_equipmentparts.anchorMax = C_Vector2(0.412, self.bindComponents.rect_equipmentparts.anchorMax.y)
    colNum = 7
  end
end

function this:setData(selectHeroGuid, selectAccessoryType)
  if self._selectType == selectAccessoryType and self._selectHeroGuid == selectHeroGuid then
    return
  end
  self._selectType = selectAccessoryType
  self._selectHeroGuid = selectHeroGuid
  L_AccessoryManager:setCurSelectHeroUid(selectHeroGuid)
  local tpl = _tabTpl:getTplById(Type2Tab[selectAccessoryType])
  self.bind.txt_title = L_Lang:get(_tabTpl:getName(tpl))
  self:initSortFilterModule()
  self:resetSelectData()
  self:updateAccessoryType()
end

function this:updateAccessoryType()
  self:initCurEquipAccessoryId()
  self:updateAccessoryList()
end

function this:initSortFilterModule()
  local callback = function()
    self:resetSelectData()
    self:updateAccessoryList()
    self:updateSelectGuid()
  end
  self._accessorySortFilter = self._accessorySortFilter or L_SortFilterManager:createSortFilter(L_SystemConst.enum.accessoryStrengthBag, callback, callback)
  self._accessorySortFilter:bindModule(self.modules.module_sortFilter)
end

function this:resetData()
  self._selectType = nil
  self._selectHeroGuid = nil
end

function this:resetSelectData()
  self._selectGuid = nil
end

function this:initOperation()
  local selectId = self._selectGuid
  local idSelectValid = not math.isEmpty(selectId)
  local idEquippedValid = not math.isEmpty(self._equippedGuid)
  self.bind.go_strength_left = idEquippedValid and not L_AccessoryStore:getIsMaxLevel(self._equippedGuid)
  self.bind.go_strength_right = idSelectValid and not L_AccessoryStore:getIsMaxLevel(self._selectGuid)
  local active_equipBtn = idSelectValid and math.isEmpty(self._equippedGuid)
  local active_replaceBtn = idSelectValid and not math.isEmpty(self._equippedGuid) and self._equippedGuid ~= selectId
  if active_replaceBtn then
    if self.bind.go_strength_right then
      self.bind.go_replaceBtn = true
      self.bind.go_replaceBtnSingle = false
    else
      self.bind.go_replaceBtnSingle = true
      self.bind.go_replaceBtn = false
    end
  else
    self.bind.go_replaceBtnSingle = false
    self.bind.go_replaceBtn = false
  end
  if active_equipBtn then
    if self.bind.go_strength_right then
      self.bind.go_equipBtn = true
      self.bind.go_equipBtnSingle = false
    else
      self.bind.go_equipBtnSingle = true
      self.bind.go_equipBtn = false
    end
  else
    self.bind.go_equipBtnSingle = false
    self.bind.go_equipBtn = false
  end
end

function this:initCurEquipAccessoryId()
  local heroAccessory = L_HeroStore:getHeroAccessoryGuidList(L_HeroStore:getHero(self._selectHeroGuid))
  self._equippedGuid = heroAccessory[self._selectType]
end

function this:updateSelectGuid()
  local isCompare = self.isCompare
  local isSame = self._selectGuid == self._equippedGuid
  local showSelected = not math.isEmpty(self._selectGuid) and (isCompare or not isSame)
  local showEquipped = not math.isEmpty(self._selectGuid) and isSame or isCompare and showSelected
  local bothShow = showEquipped and showSelected
  self.bind.go_bg_black = isCompare
  self.bind.go_btn_black = not isCompare
  self.bind.go_btn_yellow = isCompare
  self.bind.go_compare_arrow = bothShow
  self:initEquippedAccessoryInfo(showEquipped, showSelected)
  self:initSelectAccessoryInfo(showSelected, showEquipped)
  self:initOperation()
end

function this:initEquippedAccessoryInfo(showEquipped, showSelected)
  self.bind.go_equipInfo = showEquipped
  local moduleEquipInfo = self.modules.module_equipInfo
  if showEquipped then
    self.bind.toggle_equip = not math.isEmpty(self._equippedGuid)
    if not self.bind.toggle_equip then
      if showSelected then
        moduleEquipInfo:restSelectGuid()
      else
        moduleEquipInfo:restSelectType()
      end
      return
    end
    moduleEquipInfo:setData(L_BagConst.bagTag.accessory, self._equippedGuid, {hideFuncBtn = true})
    moduleEquipInfo:setEquippedAccessory(self._selectHeroGuid)
  elseif showSelected then
    moduleEquipInfo:restSelectGuid()
    moduleEquipInfo:setType(L_BagConst.bagTag.accessory)
  else
    moduleEquipInfo:restSelectType()
  end
end

function this:initSelectAccessoryInfo(showSelected, showEquipped)
  self.bind.go_selectInfo = showSelected
  local moduleSelectInfo = self.modules.module_selectInfo
  if showSelected then
    self.bind.toggle_select = not math.isEmpty(self._selectGuid)
    if not self.bind.toggle_select then
      if showEquipped then
        moduleSelectInfo:restSelectGuid()
      else
        moduleSelectInfo:restSelectType()
      end
      return
    end
    moduleSelectInfo:setData(L_BagConst.bagTag.accessory, self._selectGuid, {hideFuncBtn = true})
    moduleSelectInfo:setEquippedAccessory(self._selectHeroGuid, true)
  elseif showEquipped then
    moduleSelectInfo:restSelectGuid()
    moduleSelectInfo:setType(L_BagConst.bagTag.accessory)
  else
    moduleSelectInfo:restSelectType()
  end
end

function this:updateAccessoryList()
  self:getBagDataList()
  self:insertArrayDataList()
end

function this:insertArrayDataList()
  self:initCurEquipAccessoryId()
  if math.isEmpty(self._selectGuid) and not math.isEmpty(self._equippedGuid) then
    self._selectGuid = self._equippedGuid
  end
  local isEmpty = table.isEmpty(self._dataList)
  self.bind.toggle_isEmpty = isEmpty
  local tmp = isEmpty and {} or {
    {go_btn_remove = true, go_item = false}
  }
  for i, v in pairs(self._dataList or {}) do
    table.insert(tmp, {
      guid = v.guid,
      selected = self._selectGuid == v.guid
    })
  end
  local listNum = #tmp
  local emptyNum = math.max(0, (rowNum - math.ceil(listNum / colNum)) * colNum) + (listNum % colNum == 0 and 0 or colNum - listNum % colNum)
  for i = 1, emptyNum do
    table.insert(tmp, {})
  end
  self.bind.scrollList_accessory:clear()
  self.bind.scrollList_accessory:insert_array(tmp)
  self:playListCellInVFX(0.2, 0.017)
  if isEmpty then
    self._selectGuid = 0
  else
    local node, index = table.ipairsFind(self._dataList, function(value)
      return value.guid == self._selectGuid
    end)
    self._selectGuid = math.isEmpty(index) and 0 or self._selectGuid
  end
  self:setAccessoryListActive()
  self:updateSelectGuid()
end

function this:playListCellInVFX(delayTime, intervalTime)
  intervalTime = intervalTime or 0.1
  for i, v in pairs(self.modules.scrollList_accessory) do
    local cell = v
    cell:playCellOutVFX()
    L_TimerManager:stopTimer(self, string.format("VFX_showAccessoryCell_%d", i))
  end
  self._isListReady = false
  local Count = #self.modules.scrollList_accessory
  L_TimerManager:newOrResetTimer(self, "VFX_showAccessoryContent", function()
    for i, v in pairs(self.modules.scrollList_accessory) do
      L_TimerManager:newOrResetTimer(self, string.format("VFX_showAccessoryCell_%d", i), function()
        local cell = v
        cell:playCellInVFX(function()
          if i == Count then
            self._isListReady = true
          end
        end)
      end, intervalTime * (i - 1))
    end
  end, delayTime)
end

function this:getAccessoryDataList()
  local tmp = {}
  local accessory = L_AccessoryStore:getHeroAccessory()
  for _, v in pairs(accessory) do
    local tpl = _accessoryTpl:getTplById(v.accessoryId)
    if _accessoryTpl:getType(tpl) == self._selectType then
      local cell = {
        guid = v.guid,
        itemType = L_Const.resType.accessory,
        itemId = v.accessoryId
      }
      table.insert(tmp, cell)
    end
  end
  self._accessorySortFilter:sortFilterList(tmp)
  return tmp
end

function this:getBagDataList()
  self._dataList = self:getAccessoryDataList()
end

function this:setAccessoryListActive()
  for _, v in ipairs(self.modules.scrollList_accessory) do
    local cell = v
    cell:setSingleSelect(cell.bind.guid == self._selectGuid)
  end
end

function this:syncAccessory()
  self:updateSelectGuid()
end

return this
