local this = class("modulePetNewHomeTags", G_UIModuleBase)
local _homeTalentTpl = L_GameTpl:getHomeTalentTpl()
local _petHomeSizeTpl = L_GameTpl:getPetHomeSizeTpl()
local C_HomeManager = AzurWorld.HomeMgr
local C_PetStore = C_HomeManager:GetPetStore()
local C_TDPetHomeTalentTable = CS.Azur.Gameplay.Table.TDPetHomeTalentTable
local _petHomeTalentTpl = L_GameTpl:getPetHomeTalentTpl()
local maxTipHeight = 1002

function this.bind()
  return {
    list_petHomeLaborTag = {
      moduleName = "pages/Pet/cellTagPetHomeLabor"
    },
    active_tip = false,
    active_tipArrow = false,
    list_tipLaborList = {
      moduleName = "pages/Pet/cellPetTipHomeLabor"
    },
    size_tipBoard = C_Vector2(750, 1002),
    text_petSize = ""
  }
end

function this.methods()
  return {
    onClick_showTip = function(self)
      if #self.bind.list_tipLaborList < 1 then
        return
      end
      self:showTip()
      self:emit("onCLickPetNewHomeTag")
    end,
    onClick_closeTip = function(self)
      self.bind.active_tip = false
    end
  }
end

function this:showTip()
  self.bind.active_tip = true
  self:refreshView()
  Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.TalentList)
  if self.bindComponents.TalentList.sizeDelta.y + 160 > maxTipHeight then
    self.bind.size_tipBoard = C_Vector2(750, maxTipHeight + 5)
    self.bind.active_tipArrow = true
  else
    self.bind.size_tipBoard = C_Vector2(750, self.bindComponents.TalentList.sizeDelta.y + 160)
    self.bind.active_tipArrow = false
  end
end

function this:closeTip()
  self.bind.active_tip = false
end

function this:setGuid(guid)
  self._guid = guid
  self:refreshView()
end

function this:setConfigId(configId)
  self._configId = configId
  self:refreshPreviewInfo()
end

function this:open()
  self:refreshView()
  self._ESyncMountRouletteServerDataHandler = handler(self, self.refreshView)
  AzurWorld.mountMgr:RegisterEvent(C_EMountEvent.ESyncMountRouletteServerData, self._ESyncMountRouletteServerDataHandler)
  L_SatietyStore:listenCallFunc(L_SatietyStore.event.satiety_value_refresh, self.refreshView, self)
end

function this:close()
  AzurWorld.mountMgr:UnregisterEvent(C_EMountEvent.ESyncMountRouletteServerData, self._ESyncMountRouletteServerDataHandler)
  L_SatietyStore:unListenCallFunc(L_SatietyStore.event.satiety_value_refresh, self.refreshView, self)
end

function this:hide()
  self.bind.active_tip = false
end

function this:refreshView()
  if not self._guid then
    return
  end
  local pet = L_PetStore:getPetItem(self._guid)
  local petId = pet.id
  local petHomeTalentTpl = _petHomeTalentTpl:getTplById(petId)
  local petSize = _petHomeTalentTpl:getSizeType(petHomeTalentTpl)
  local petHomeSizeTpl = _petHomeSizeTpl:getTplById(petSize)
  self.bind.text_petSize = _petHomeSizeTpl:getSizeName(petHomeSizeTpl)
  self:refreshPetHomeLaborTagListByPetGuid(self._guid)
end

function this:refreshPreviewInfo()
  if not self._configId then
    return
  end
  local petId = self._configId
  local petHomeTalentTpl = _petHomeTalentTpl:getTplById(petId)
  local petSize = _petHomeTalentTpl:getSizeType(petHomeTalentTpl)
  local petHomeSizeTpl = _petHomeSizeTpl:getTplById(petSize)
  self.bind.text_petSize = _petHomeSizeTpl:getSizeName(petHomeSizeTpl)
  self:refreshPetHomeLaborTagListByPetConfigId(self._configId)
end

function this:refreshPetHomeLaborTagListByPetGuid(petGuid)
  local C_PetData = C_PetStore:GetPet(petGuid)
  local C_LaborInfoList = C_PetData:GetLaborInfoList()
  local showParamsList = {}
  for i = 0, C_LaborInfoList.Count - 1 do
    local C_LaborInfo = C_LaborInfoList[i]
    local C_TalentIds = C_PetData.talentIds
    local talentId
    if 0 < C_TalentIds.Count then
      local tmpTalentId = C_TalentIds[0]
      local homeTalentTpl = _homeTalentTpl:getTplById(tmpTalentId)
      local laborOfTmpTalentId = _homeTalentTpl:getLaborTypeIdOfTalent(homeTalentTpl)
      if laborOfTmpTalentId == C_LaborInfo.laborId then
        talentId = tmpTalentId
      end
    end
    local showParams = {
      petId = C_PetData.configId,
      laborId = C_LaborInfo.laborId,
      laborGrade = C_LaborInfo.laborGrade,
      talentId = talentId
    }
    table.insert(showParamsList, showParams)
  end
  self:refreshPetHomeLaborTagList(showParamsList)
end

function this:refreshPetHomeLaborTagListByPetConfigId(petConfigId)
  local C_TDPetHomeTalent = C_TDPetHomeTalentTable.GetData(petConfigId)
  local showParamsList = {}
  if C_TDPetHomeTalent then
    local C_LaborConfigInfoList = C_TDPetHomeTalent:GetLaborConfigInfoList()
    for i = 0, C_LaborConfigInfoList.Count - 1 do
      local C_LaborConfigInfo = C_LaborConfigInfoList[i]
      local showParams = {
        petId = petConfigId,
        laborId = C_LaborConfigInfo.laborTypeId,
        laborGrade = C_LaborConfigInfo.minLaborGrade
      }
      table.insert(showParamsList, showParams)
    end
  end
  self:refreshPetHomeLaborTagList(showParamsList)
end

function this:refreshPetHomeLaborTagList(showParamsList)
  self.bind.list_petHomeLaborTag:clear()
  self.bind.list_petHomeLaborTag:insert_array(showParamsList)
  self.bind.list_tipLaborList:clear()
  self.bind.list_tipLaborList:insert_array(showParamsList)
end

return this
