local module = class("moduleWardrobeColorSelect", G_UIModuleBase)
local _AvatarTpl = L_GameTpl:getWardrobeAvatarTpl()
local _ColorTpl = L_GameTpl:getWardrobeColorTpl()
local _WardrobeTpl = L_GameTpl:getWardrobeTpl()

function module.bind()
  return {
    colorList = {
      moduleName = "pages/MOD/Module/modColorSelectItem"
    }
  }
end

function module.methods()
  return {
    colorList = {
      selectColor = function(self, colorId, avatarId)
        L_ModStore:selectSkinWardrobeId(avatarId)
        for i = 1, #self.bind.colorList do
          local value = self.bind.colorList:getValue(i)
          self.bind.colorList:change(i, {
            isSelect = value.wardrobeId == avatarId
          })
        end
        L_ModStore:setIsChanged(true)
      end
    }
  }
end

function module:refreshUI()
  local allSkinList = _AvatarTpl:getAllAvatarIdByType(L_ModStore.modType.skin, L_ModStore:getModData().Sex)
  self.bind.colorList:clear()
  local isFindSelect = false
  local haveData = false
  for i = 1, #allSkinList do
    local skinId = allSkinList[i]
    local currTpl = _AvatarTpl:getTplById(skinId)
    local colorId = _AvatarTpl:getColorId(currTpl)[1][1]
    local colorTpl = _ColorTpl:getTplById(colorId)
    if colorTpl then
      local isSelectColorId = L_ModStore:getTempAvatarWardrobe(L_ModStore.modType.skin) == skinId
      haveData = true
      isFindSelect = isFindSelect or isSelectColorId
      self.bind.colorList:insert(i, {
        wardrobeId = skinId,
        colorId = colorId,
        colorValue = _ColorTpl:getIconColor(colorTpl),
        colorName = _ColorTpl:getName(colorTpl),
        isSelect = isSelectColorId
      })
    end
  end
  if not haveData then
    self.bind.colorList:insert(1, {
      colorValue = "#FFFFFF",
      colorName = L_WordsTpl:getValue("ui_moduleQuickRoulette"),
      colorId = -1,
      isSelect = not isFindSelect
    })
  end
end

function module:open()
  self:refreshUI()
end

function module:close()
end

return module
