local this = class("cellFormationPet", G_UIModuleBase)
local petTpl = L_GameTpl:getPetTpl()
local GifPath = "UI/Texture/PetPixelIcon/tex_icon_pet_%s_%d.png"
local _elementTpl = L_GameTpl:getElementTypeTpl()
local _petTpl = L_GameTpl:getPetTpl()

function this.bind()
  return {
    txt_petLV = "0",
    img_element = "",
    img_petIcon = "",
    txt_posIndex = "1",
    activeAdd = false,
    img_icon_outline = "",
    modulePetFlashEft = {
      moduleName = "pages/pet/modulePetFlashEft"
    }
  }
end

function this.methods()
  return {}
end

function this:open()
  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)
  if not isEmpty then
    local serverData = L_PetStore:getPetItem(self.bind.guid)
    local tpl = petTpl:getTplById(serverData.id)
    self.bind.img_petIcon = _petTpl:getPetPixelIcon(tpl, petData:isSpecialPet())
    self._initFrameIcon = petTpl:getPetPixelIcon(tpl, 0)
    self._curFrame = 1
    self._defaultPixelId = string.match(self._initFrameIcon, "%a*%d+")
    local iconPath = string.format(GifPath, self._defaultPixelId, self._curFrame)
    self.bind.active_baseShadow = not string.isEmpty(self._initFrameIcon)
    self.bindComponents.img_icon_outline:LoadSprite(iconPath, false)
    local element = petTpl:getElement(tpl)
    if element[1] and _elementTpl:getTplById(element[1]) then
      self.bind.img_element = _elementTpl:getPetElem(_elementTpl:getTplById(element[1]))
    end
    self.modules.modulePetFlashEft:setModulePetFlashEftData(self.bind.guid)
  end
end

function this:setPetDataContent(guid, params)
  local petData = L_PetStore:getPetItem(guid)
  if petData then
    local tpl = _petTpl:getTplById(petData.id)
    self.bind.img_petIcon = _petTpl:getPetPixelIcon(tpl, petData:isSpecialPet())
    self.bind.txt_petLV = string.format("Lv.%2d", tostring(petData.lv))
    local tpl = petTpl:getTplById(petData.id)
    local element = petTpl:getElement(tpl)
    if element[1] and _elementTpl:getTplById(element[1]) then
      self.bind.img_element = _elementTpl:getPetElem(_elementTpl:getTplById(element[1]))
    end
    self._initFrameIcon = petTpl:getPetPixelIcon(tpl, 0)
    self._curFrame = 1
    self._defaultPixelId = string.match(self._initFrameIcon, "%a*%d+")
    local iconPath = string.format(GifPath, self._defaultPixelId, self._curFrame)
    self.bindComponents.img_icon_outline:LoadSprite(iconPath, false)
    self.modules.modulePetFlashEft:setModulePetFlashEftData(guid)
  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:onEvent_refreshPet(pet)
  if pet.guid == self.bind.guid then
    self:refreshView()
  end
end

return this
