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

function module.bind()
  return {
    wardrobeList = {
      moduleName = "pages/mod/module/wardrobeSelectItem"
    },
    colorList = {
      moduleName = "pages/mod/module/moduleWardrobeColorSelect"
    }
  }
end

function module.methods()
  return {
    wardrobeList = {
      selectWardrobe = function(self, avatarId)
        L_ModStore:selectModWardrobeId(self.wardrobeType, avatarId)
        if self.wardrobeType == L_ModStore.modType.tryOn or self.wardrobeType == L_ModStore.modType.cloth then
          self.parent:playIdle()
        end
        for i = 1, #self.bind.wardrobeList do
          local value = self.bind.wardrobeList:getValue(i)
          self.bind.wardrobeList:change(i, {
            isSelect = value.wardrobeId == avatarId
          })
        end
        self:refreshColorListUI()
        L_ModStore:setIsChanged(true)
      end
    }
  }
end

function module:refreshColorListUI()
  local dataList = {}
  if self.wardrobeType == L_ModStore.modType.eye then
    table.insert(dataList, {
      colorText = L_WordsTpl:getValue("ui_surface_01"),
      colorIndex = 1,
      wardrobeType = self.wardrobeType
    })
    table.insert(dataList, {
      colorText = L_WordsTpl:getValue("ui_surface_02"),
      colorIndex = 2,
      wardrobeType = self.wardrobeType
    })
  elseif self.wardrobeType == L_ModStore.modType.cloth then
    table.insert(dataList, {
      colorText = L_WordsTpl:getValue("ui_surface_03"),
      colorIndex = 1,
      wardrobeType = self.wardrobeType
    })
  elseif self.wardrobeType == L_ModStore.modType.tryOn then
    table.insert(dataList, {
      colorText = L_WordsTpl:getValue("ui_surface_04"),
      colorIndex = 1,
      wardrobeType = self.wardrobeType
    })
  elseif self.wardrobeType == L_ModStore.modType.hair then
    table.insert(dataList, {
      colorText = L_WordsTpl:getValue("ui_surface_05"),
      colorIndex = 1,
      wardrobeType = self.wardrobeType
    })
  elseif self.wardrobeType == L_ModStore.modType.eyelash then
  elseif self.wardrobeType == L_ModStore.modType.eyebrow then
    table.insert(dataList, {
      colorText = L_WordsTpl:getValue("ui_surface_06"),
      colorIndex = 1,
      wardrobeType = self.wardrobeType
    })
  end
  if #dataList == #self.modules.colorList then
    for i, data in ipairs(dataList) do
      local module = self.modules.colorList[i]
      module.bind.colorText = data.colorText
      module.bind.colorIndex = data.colorIndex
      module.bind.wardrobeType = self.wardrobeType
      module:refreshUI()
    end
  else
    self.bind.colorList:clear()
    self.bind.colorList:insert_array(dataList)
  end
  if self.isBind then
    L_GameUtil.forceRebuildLayout(self.bindComponents.Content)
    L_GameUtil.forceRebuildLayout(self.bindComponents.ColorContent)
  end
end

function module:refreshUI()
  local ids = _AvatarTpl:getAllAvatarIdByType(self.wardrobeType, L_ModStore:getModData().Sex)
  local isFindSelect = false
  local hasDefault = false
  local tmp = {}
  for i, v in pairs(ids) do
    local tpl = _AvatarTpl:getTplById(v)
    local isWearWardrobeId = L_ModStore:isTempWearWardrobeId(self.wardrobeType, v)
    isFindSelect = isFindSelect or isWearWardrobeId
    hasDefault = hasDefault or _AvatarTpl:getInitial(tpl) == 1
    table.insert(tmp, {
      wardrobeId = v,
      initial = _AvatarTpl:getInitial(tpl),
      isSelect = isWearWardrobeId,
      icon = _AvatarTpl:getIcon(tpl)
    })
  end
  if not hasDefault then
    table.insert(tmp, {
      wardrobeId = -1,
      initial = 1,
      isSelect = not isFindSelect,
      icon = ""
    })
  end
  table.sort(tmp, function(a, b)
    return a.wardrobeId < b.wardrobeId
  end)
  self.bind.wardrobeList:clear()
  self.bind.wardrobeList:insert_array(tmp)
  self:refreshColorListUI()
end

function module:open()
  L_ModStore:listenCallFunc(L_ModStore.event.refreshUI, self.refreshUI, self)
end

function module:close()
  L_ModStore:unListenCallFunc(L_ModStore.event.refreshUI, self.refreshUI)
end

function module:setModuleData(wardrobeType)
  self.wardrobeType = wardrobeType
  self:refreshUI()
end

return module
