local this = class("cellPixelPetFormation", G_UIModuleBase)

function this.bind()
  return {
    txt_petName = "",
    go_posIndex = false,
    txt_posIndex = "",
    module_cellPixelPet = {
      moduleName = "pages/pet/cellPixelPet"
    },
    go_petGameplayStatus = false,
    module_petGameplayStatus = {
      moduleName = "pages/pet/modulePetGameplayStatus"
    },
    activeAdd = false,
    activeAddMaskChongwu = false,
    activeAddMaskTaijie = false,
    activeAddlev = false,
    activeAddelement = false
  }
end

function this.methods()
  return {}
end

function this:open()
  self._cellPixelPet = self.modules.module_cellPixelPet
  self:initCell()
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPet, self.onEvent_refreshPet, self)
end

function this:close()
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshPet, self.onEvent_refreshPet, self)
end

function this:initCell()
  if not self.isBind then
    return
  end
  self:refreshView()
end

function this:refresh()
  self:refreshView()
end

function this:refreshView()
  if not self.isBind then
    return
  end
  local isEmpty = math.isEmpty(self.bind.guid)
  self.bind.go_petGameplayStatus = not isEmpty
  if isEmpty then
    self._cellPixelPet:setPetDataContent(self.bind.guid, {})
    self:setSelect(false)
    self.bind.txt_petName = ""
    self:setPosIndex()
    return
  end
  local serverData = L_PetStore:getPetItem(self.bind.guid)
  self.bind.txt_petName = serverData.pet_name
  local petGameplayStatus = self.modules.module_petGameplayStatus
  petGameplayStatus:setData(self.bind.guid)
  if L_FormationManager:getRuntimeFormationType() then
    local pos
    pos = L_FormationStore:getPetPlaceInTeam(L_FormationManager:getRuntimeFormationType(), L_FormationManager:getRuntimeFormationIndex(), self.bind.guid)
    local editPos = L_FormationManager:getFocusIndex()
    self.bind.go_select = pos == editPos
    self:setPosIndex(pos)
  end
  local sel = self.bind.type == L_Const.cellIconUnitType.quickTeamUp and self.bind.posIndex > 0
  self._cellPixelPet:setPetDataContent(self.bind.guid, {
    choice = self.bind.go_select,
    filterType = L_PetConst.filterPetBox.level,
    callback = self.bind.callback,
    pointEnterCallback = self.bind.pointEnterCallback,
    pointExitCallback = function(cell)
      if self.bind.pointExitCallback then
        self.bind.pointExitCallback(cell)
      end
    end
  })
  self:setSelect(self.bind.go_select)
end

function this:setSelect(selected)
  if self.bind.type or self.bind.type == L_Const.cellIconUnitType.quickTeamUp then
    if self.bind.posIndex and self.bind.posIndex > 0 and self.bind.type == L_Const.cellIconUnitType.quickTeamUp then
      self.bind.activeAdd = true
      self.bind.activeAddMaskChongwu = true
      self.bind.activeAddMaskTaijie = true
      self.bind.activeAddlev = true
      self.bind.activeAddelement = true
      self.bind.go_select = false
      if self.isBind then
        if self.bind.posIndex == self.parent.SelPetPosIndex then
          self.modules.module_cellPixelPet:refreshSel(true)
        else
          self.modules.module_cellPixelPet:refreshSel(false)
        end
        self.modules.module_cellPixelPet:setGifPlay(self.bind.go_select)
      end
    else
      self.bind.activeAdd = false
      self.bind.activeAddMaskChongwu = false
      self.bind.activeAddMaskTaijie = selected
      self.bind.activeAddlev = false
      self.bind.activeAddelement = false
      self.bind.go_select = selected
      if self.isBind then
        self.modules.module_cellPixelPet:refreshSel(selected)
        self.modules.module_cellPixelPet:setGifPlay(self.bind.go_select)
      end
    end
  else
    self.bind.activeAdd = false
    self.bind.go_select = selected
    self.bind.activeAddMaskChongwu = false
    self.bind.activeAddMaskTaijie = selected
    self.bind.activeAddlev = false
    self.bind.activeAddelement = false
    if not self.isBind then
      return
    end
    local cellPixelPet = self.modules.module_cellPixelPet
    cellPixelPet:setGifPlay(self.bind.go_select)
  end
end

function this:setIfInTeam()
  if not self.isBind then
    return
  end
  if self.bind.type == L_Const.cellIconUnitType.selectPet then
    local type = L_FormationManager:getRuntimeFormationType()
    if type == nil then
      return
    end
    local index = L_FormationManager:getRuntimeFormationIndex()
    local posData = L_FormationStore:getFormationPosData(type, index)
    self.bind.activeAddMaskTaijie = false
    for i, guid in ipairs(posData) do
      local pet_guid = L_FormationStore:getFormationPosPetGuid(type, index, i)
      if self.bind.guid == pet_guid then
        self.bind.activeAddMaskTaijie = true
        break
      end
    end
  end
end

function this:setPosIndex(index)
  local needShowFormationData = index ~= nil and 0 < index or false
  self.bind.posIndex = index
  if needShowFormationData then
    self.bind.txt_posIndex = tostring(index)
  end
  self.bind.go_posIndex = needShowFormationData
end

function this:getRaycastGo()
  if not self.isBind then
    return
  end
  return self._cellPixelPet:getRaycastGo()
end

function this:onEvent_refreshPet(pet)
  if pet.guid == self.bind.guid then
    self:refreshView()
  end
end

return this
