local this = class("modStore", G_BaseStore)
local _AvatarTpl = L_GameTpl:getWardrobeAvatarTpl()
local _ColorTpl = L_GameTpl:getWardrobeColorTpl()
math.randomseed(os.time())
this.event = {
  refreshHeight = "refreshHeight",
  refreshUI = "refreshUI"
}
this:importPartialClass(require(L_R.store .. "mod.modState"))
this:importPartialClass(require(L_R.store .. "mod.modAction"))

function this:selectSkinWardrobeId(wardrobeAvatarId)
  if self.tempAvatarCtrl then
    self.tempAvatarCtrl:SetWardrobeId(self.modType.skin, wardrobeAvatarId)
  end
end

function this:selectModWardrobeId(wardrobeType, avatarId)
  if self.tempAvatarCtrl then
    self.tempAvatarCtrl:SetWardrobeId(wardrobeType, avatarId)
  end
end

function this:setTempAvatarCtrl(ctrl)
  self.tempAvatarCtrl = ctrl
end

function this:selectModColorId(wardrobeType, colorIndex, colorId)
  if self.tempAvatarCtrl then
    colorIndex = colorIndex or -1
    colorId = colorId or -1
    self.tempAvatarCtrl:SetColorId(wardrobeType, colorId, colorIndex)
  end
end

function this:getPlayerModData(sex)
  if L_PlayerStore:getIsNewPlayer() then
    return self:getDefaultData(sex)
  end
  local modAvatar = AzurWorld.avatarMgr:GetAvatarData(L_PlayerStore:getPlayerId())
  if modAvatar then
    return modAvatar
  end
  return self:getDefaultData(sex)
end

function this:resetDefaultData(typeList)
  if self.tempAvatarCtrl then
    self.tempAvatarCtrl:ResetDefualt()
  end
  self:call(self.event.refreshUI)
end

function this:randomSkinData()
  local allSkinIds = _AvatarTpl:getAllAvatarIdByType(self.modType.skin, self.tempAvatarCtrl.Sex)
  local randomIndex = math.random(1, #allSkinIds)
  self:selectSkinWardrobeId(allSkinIds[randomIndex])
end

function this:randomDataByWardrobeTypeInter(wardrobeType)
  local ids = _AvatarTpl:getAllAvatarIdByType(wardrobeType, self.tempAvatarCtrl.Sex)
  local randomIndex = math.random(1, #ids)
  local avatarId = ids[randomIndex]
  self:selectModWardrobeId(wardrobeType, avatarId)
  local tpl = _AvatarTpl:getTplById(avatarId)
  local colorList = _AvatarTpl:getColorId(tpl)
  if not table.isEmpty(colorList) then
    if wardrobeType == L_ModStore.modType.eye then
      local randomColorIndex = math.random(1, #colorList[1])
      self:selectModColorId(wardrobeType, 1, colorList[1][randomColorIndex])
      local randomColor2Index = math.random(1, #colorList[2])
      self:selectModColorId(wardrobeType, 2, colorList[2][randomColor2Index])
    elseif wardrobeType == L_ModStore.modType.hair or wardrobeType == L_ModStore.modType.eyelash or wardrobeType == L_ModStore.modType.eyebrow or wardrobeType == L_ModStore.modType.cloth or wardrobeType == L_ModStore.modType.tryOn then
      local randomColorIndex = math.random(1, #colorList[1])
      self:selectModColorId(wardrobeType, 1, colorList[1][randomColorIndex])
    end
  else
    self:selectModColorId(wardrobeType, 1, nil)
    if wardrobeType == self.modType.eye then
      self:selectModColorId(wardrobeType, 2, nil)
    end
  end
end

function this:randomDataByWardrobeType(wardrobeType, hideRefresh)
  if wardrobeType == self.modType.skin then
    self:randomSkinData()
  else
    self:randomDataByWardrobeTypeInter(wardrobeType)
  end
  if hideRefresh then
  else
    self:call(self.event.refreshUI)
  end
end

function this:allRandomData(typeList)
  self.tempAvatarCtrl:RemoveTryOn()
  for i, v in pairs(typeList or _AvatarTpl:getTypeList()) do
    if v and v ~= self.modType.tryOn then
      self:randomDataByWardrobeType(v, true)
    end
  end
  self:call(self.event.refreshUI)
end

function this:getOldDefaultData(sex)
  if math.isEmpty(sex) then
    sex = 2
  end
  local data = require(L_R.store .. "mod.data.modAvatar").new()
  data.sex = sex
  for i, v in pairs(_AvatarTpl:getAllAvatarIdByType(self.modType.height, sex)) do
    local modConfigData = _AvatarTpl:getTplById(v)
    if modConfigData.initial == 1 and modConfigData.sex == sex then
      data.height = modConfigData.height
    end
  end
  for i, v in ipairs(_AvatarTpl:getAllAvatarIdByType(self.modType.skin, sex)) do
    local modConfigData = _AvatarTpl:getTplById(v)
    if modConfigData.initial == 1 and modConfigData.sex == sex then
      data.skinId = v
    end
  end
  for i, v in pairs(_AvatarTpl.data) do
    local modConfigData = v
    if modConfigData.initial == 1 and modConfigData.sex == sex and modConfigData.type ~= self.modType.skin and modConfigData.type ~= self.modType.height then
      local modWardrobe = require(L_R.store .. "mod.data.modWardrobe").new()
      modWardrobe.type = modConfigData.type
      modWardrobe.avatarId = i
      modWardrobe.colorIds = self:getDefaultColorIds(i)
      data.modPartData[modConfigData.type] = modWardrobe
    end
  end
  return data
end

function this:getDefaultData(sex)
  if math.isEmpty(sex) or sex < 1 then
    sex = 2
  end
  return AzurWorld.avatarMgr:GetDefaultAvatarData(sex)
end

function this:getDefaultPartData(sex, partType)
end

function this:setIsChanged(state)
  self._isChanged = state
end

function this:isModDataChange()
  return self._isChanged
end

function this:transitionDataToServer(avatarData, urlData)
  local serverData = {}
  serverData.parts = {}
  serverData.sex = avatarData.Sex
  serverData.complexion = avatarData.Complexion
  for i, v in pairs(avatarData.PartInfoDict) do
    local partInfo = {}
    partInfo.type = i
    partInfo.id = v.id
    partInfo.colors = {}
    for j, k in pairs(v:GetColorIds()) do
      local colorInfo = {}
      colorInfo.index = j
      colorInfo.color_id = k
      table.insert(partInfo.colors, colorInfo)
    end
    table.insert(serverData.parts, partInfo)
  end
  serverData.avatars = {}
  if not string.isEmpty(urlData.headImgUrl) then
    local avatarPair = {}
    avatarPair.avatar_type = L_Const.screenShotType.hearHeadImg
    avatarPair.avatar_url = urlData.headImgUrl
    table.insert(serverData.avatars, avatarPair)
  end
  if not string.isEmpty(urlData.halfBodyUrl) then
    local avatarPair = {}
    avatarPair.avatar_type = L_Const.screenShotType.halfBodyImg
    avatarPair.avatar_url = urlData.halfBodyUrl
    table.insert(serverData.avatars, avatarPair)
  end
  if not string.isEmpty(urlData.battleHeadUrl) then
    local avatarPair = {}
    avatarPair.avatar_type = L_Const.screenShotType.battleHeadImg
    avatarPair.avatar_url = urlData.battleHeadUrl
    table.insert(serverData.avatars, avatarPair)
  end
  if not string.isEmpty(urlData.halfsHeadUrl) then
    local avatarPair = {}
    avatarPair.avatar_type = L_Const.screenShotType.halfsHeadImg
    avatarPair.avatar_url = urlData.halfsHeadUrl
    table.insert(serverData.avatars, avatarPair)
  end
  if not string.isEmpty(urlData.circleHeadUrl) then
    local avatarPair = {}
    avatarPair.avatar_type = L_Const.screenShotType.circleHeadImg
    avatarPair.avatar_url = urlData.circleHeadUrl
    table.insert(serverData.avatars, avatarPair)
  end
  if not string.isEmpty(urlData.paintUrl) then
    local avatarPair = {}
    avatarPair.avatar_type = L_Const.screenShotType.paintImg
    avatarPair.avatar_url = urlData.paintUrl
    table.insert(serverData.avatars, avatarPair)
  end
  if not string.isEmpty(urlData.bodyUrl) then
    local avatarPair = {}
    avatarPair.avatar_type = L_Const.screenShotType.bodyImg
    avatarPair.avatar_url = urlData.bodyUrl
    table.insert(serverData.avatars, avatarPair)
  end
  return serverData
end

function this:serverDataToModAvatar(serverData)
  local data = require(L_R.store .. "mod.data.modAvatar").new()
  data.sex = L_PlayerStore:getSex()
  data.height = serverData.height
  data.skinId = serverData.complexion
  if serverData.avatars then
    for i, v in ipairs(serverData.avatars) do
      if v.avatar_type == L_Const.screenShotType.hearHeadImg then
        data.headImgUrl = v.avatar_url
      elseif v.avatar_type == L_Const.screenShotType.halfBodyImg then
        data.halfBodyUrl = v.avatar_url
      elseif v.avatar_type == L_Const.screenShotType.battleHeadImg then
        data.battleHeadUrl = v.avatar_url
      elseif v.avatar_type == L_Const.screenShotType.halfsHeadImg then
        data.halfsHeadUrl = v.avatar_url
      elseif v.avatar_type == L_Const.screenShotType.circleHeadImg then
        data.circleHeadUrl = v.avatar_url
      elseif v.avatar_type == L_Const.screenShotType.paintImg then
        data.paintUrl = v.avatar_url
      elseif v.avatar_type == L_Const.screenShotType.bodyImg then
        data.bodyUrl = v.avatar_url
      end
    end
  end
  if serverData.parts then
    for i, v in ipairs(serverData.parts) do
      local avatarId = _AvatarTpl:getIdByWardrobeId(v.id)
      local modWardrobe = require(L_R.store .. "mod.data.modWardrobe").new()
      local wardrobeConfig = L_Config:getConfig("wardrobe_avatar", avatarId)
      if wardrobeConfig then
        modWardrobe.type = v.type
        modWardrobe.avatarId = avatarId
        modWardrobe.colorIds = {}
        for j, k in ipairs(v.colors or {}) do
          table.insert(modWardrobe.colorIds, k.color_id)
        end
        data.modPartData[v.type] = modWardrobe
      else
        errorf("\229\144\142\231\171\175\229\143\145\233\128\129\231\154\132wardrobeid " .. v.id .. " avatar\232\161\168\233\135\140\230\178\161\230\156\137")
      end
    end
  end
  return data
end

return this
