local this = class("cellPetStationedManage", G_UIModuleBase)
local _petTpl = L_GameTpl:getPetTpl()
local _homeFreeWorkPositionTpl = L_GameTpl:getHomeFreeWorkPositionTpl()
local PetGifIconPath = "UI/Icon/PetPixelIcon/tex_icon_pet_%s_%d.png"
local MaxPetGifFrame = 8

function this.bind()
  return {
    module_cellPixelPet = {
      moduleName = "pages/pet/cellPixelPet"
    },
    tab_stationed = 1,
    active_img_designated = false,
    text_lockedDesc = L_WordsTpl:getValue("ui_cellNoviceTaskItem_02")
  }
end

function this.methods()
  return {
    onClick_pet = function(self)
      self:onClickPet()
    end
  }
end

function this:close()
end

function this:refreshData(petGuid, freeWorkPosId, isUnlock)
  self.UIData = {}
  self.UIData.petGuid = petGuid
  self.UIData.isUnlock = isUnlock
  self.UIData.curPetGifFrame = 1
  if self.UIData.petGuid ~= 0 then
    local csPetData = L_PetStore:getCsPetData(petGuid)
    local petId = L_PetStore:getPetConfigId(csPetData)
    local petGuid = self.UIData.petGuid
    local petTpl = _petTpl:getTplById(petId)
    local petItem = L_PetStore:getPetItem(petGuid)
    local isSpecial = petItem:isSpecialPet()
    local initFrameIcon = _petTpl:getPetPixelIcon(petTpl, isSpecial)
    self.UIData.petGifPixelIconIdStr = string.match(initFrameIcon, "%a*%d+")
    self.UIData.isDesignated = L_PetStore:getPetIsDesignated(csPetData)
  else
    self.UIData.petGifPixelIconIdStr = ""
    self.UIData.isDesignated = false
  end
  self.UIData.freeWorkPosId = freeWorkPosId
  local unlockCondition = _homeFreeWorkPositionTpl:getUnlockConditionListByFreeWorkPositionId(self.UIData.freeWorkPosId)[1]
  self.UIData.unlockConditionDescString = L_WordsTpl:getValue("ui_task_unlock_text") .. L_ConditionManager:getSingleDesc(unlockCondition)
end

function this:refreshUI()
  if self.UIData.petGuid ~= 0 then
    self.bind.tab_stationed = 3
  elseif self.UIData.isUnlock then
    self.bind.tab_stationed = 2
  else
    self.bind.tab_stationed = 1
  end
  self.bind.active_img_designated = self.UIData.isDesignated
  self.bind.text_lockedDesc = self.UIData.unlockConditionDescString
  self:refeshPixelIcon()
end

function this:refeshPixelIcon()
  if not self.isBind then
    return
  end
  if self.UIData.petGuid ~= 0 then
    local pixelPet = self.modules.module_cellPixelPet
    pixelPet.bind.choice = true
    pixelPet:setPetDataContent(self.UIData.petGuid, {
      filterType = L_PetConst.filterPetBox.labor
    })
    pixelPet:refreshView()
  else
  end
end

function this:playPetGif()
  local singlePetIconPath = string.format(PetGifIconPath, self.UIData.petGifPixelIconIdStr, self.UIData.curPetGifFrame)
  self.bind.img_pet = singlePetIconPath
  self.UIData.curPetGifFrame = self.UIData.curPetGifFrame % MaxPetGifFrame + 1
end

function this:onClickPet()
  self:emit("onClickStationedPet", self.UIData.petGuid)
end

function this:setPetStationed(petGuid)
  local csPetData = L_PetStore:getCsPetData(petGuid)
  local isFreeWork = L_PetStore:getPetIsFreeWork(csPetData)
  local isWorking = L_PetStore:getPetIsWorking(csPetData)
  if isWorking then
    L_FlyMsgManager:showNormalMsgByKey("ui_cellPetStationedManage")
    return
  end
  if isFreeWork then
    L_HomeStore:reqPetExitHomeHub(petGuid)
  else
    L_HomeStore:reqPetStationHomeHub(petGuid)
  end
end

return this
