local this = class("modulePetPixelIcon", G_UIModuleBase)
local MaxFrame = 8
local IconPath = "UI/Icon/PetPixelIcon/tex_icon_pet_%s_%d.png"
local GifPath = "UI/Texture/PetPixelIcon/tex_icon_pet_%s_%d.png"
local BasePath = "UI/Texture/PetPixelBase/tex_bg_pixelbase_%s.png"
local _petGradeTpl = L_GameTpl:getPetGradeTpl()
local _petTpl = L_GameTpl:getPetTpl()
local _petFeedItemTpl = L_GameTpl:getPetFeedItemTpl()
local _battleInfoTpl = L_GameTpl:getBattleInfoTpl()
local _gameConstTpl = L_GameTpl:getGameConstTpl()

function this.bind()
  return {
    img_icon = nil,
    color_icon = C_Color(1, 1, 1, 1),
    active_baseShadow = false,
    img_base = nil,
    color_base = C_Color(1, 1, 1, 1),
    img_icon_outline = nil,
    img_base_outline = nil,
    selectRoot = false,
    imgTransActive = true,
    modulePetFlashEft = {
      moduleName = "pages/pet/modulePetFlashEft"
    },
    active_frameImage_back_petUseItemEffect = false,
    textureName_frameImage_back_petUseItemEffect = "",
    frameCountPerRow_frameImage_back_petUseItemEffect = nil,
    frameCountPerCol_frameImage_back_petUseItemEffect = nil,
    frameCount_frameImage_back_petUseItemEffect = nil,
    frameAnimParamsComplete_frameImage_back_petUseItemEffect = false,
    active_frameImage_front_petUseItemEffect = false,
    textureName_frameImage_front_petUseItemEffect = "",
    frameCountPerRow_frameImage_front_petUseItemEffect = nil,
    frameCountPerCol_frameImage_front_petUseItemEffect = nil,
    frameCount_frameImage_front_petUseItemEffect = nil,
    frameAnimParamsComplete_frameImage_front_petUseItemEffect = false,
    img_glow_petUseItemEffect = nil,
    active_img_glow_petUseItemEffect = false
  }
end

function this.methods()
  return {}
end

function this:open()
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPet, self.onEvent_refreshPet, self)
end

function this:refresh()
  if self.bind.isLongTap ~= true then
    self:setIconAlpha(1)
  end
end

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

function this:refreshView()
  if not self.isBind then
    return
  end
  L_TimerManager:clearTimer(self)
  self.bind.img_icon = ""
  self.bind.img_base = ""
  self.bind.img_icon_outline = ""
  self.bind.img_base_outline = ""
  self.bind.selectRoot = false
  if self.petShowInfo then
    self.modules.modulePetFlashEft:setModulePetHasFlashEft(self.petShowInfo.special == L_Const.PetSpecialType.PST_FLASH or self.petShowInfo.special == L_Const.PetSpecialType.PST_FLASH_COLOR)
  else
    self.modules.modulePetFlashEft:setModulePetFlashEftData(self.bind.guid)
  end
  if math.isEmpty(self.bind.guid) then
    return
  end
  local tpl, isSpecial, gradeTpl
  if self.petShowInfo then
    tpl = _petTpl:getTplById(self.petShowInfo.id)
    isSpecial = self.petShowInfo.color
    gradeTpl = _petGradeTpl:getTplById(L_PetStore:calGradeRank(self.petShowInfo.grade))
  else
    local pet = L_PetStore:getPetItem(self.bind.guid)
    tpl = _petTpl:getTplById(pet.id)
    isSpecial = pet:isSpecialPet()
    local grade, gradeRank = L_PetStore:getPetGradeNew(self.bind.guid)
    gradeTpl = _petGradeTpl:getTplById(gradeRank)
  end
  self._initFrameIcon = _petTpl:getPetPixelIcon(tpl, isSpecial)
  self._curFrame = 1
  self._defaultPixelId = string.match(self._initFrameIcon, "%a*%d+")
  local iconPath = string.format(GifPath, self._defaultPixelId, self._curFrame)
  self.bind.img_icon = self._initFrameIcon
  self.bind.active_baseShadow = not string.isEmpty(self._initFrameIcon)
  self.bindComponents.img_icon_outline:LoadSprite(iconPath, false)
  self.bind.img_base = _petGradeTpl:getPetPixelBase(gradeTpl)
  local baseOutline = string.match(_petGradeTpl:getPetPixelBase(gradeTpl), "%a*%d")
  self.basePath = string.format(BasePath, baseOutline)
  self.bindComponents.img_base_outline:LoadSprite(self.basePath, false)
  local offSetPos = _petTpl:getPetPixelPosition(tpl, isSpecial)
  if offSetPos then
    self.bindComponents.rect_img_icon.anchoredPosition = L_Vector3.new(offSetPos[1], offSetPos[2])
    self.bindComponents.rect_icon_outline.anchoredPosition = L_Vector3.new(offSetPos[1], offSetPos[2])
  end
  if self.bind.autoPlay then
    self:playPixelGif()
  end
end

function this:setPetInfo(info)
  self.petShowInfo = info
end

function this:setGuid(guid, autoPlay)
  self.bind.guid = guid
  self.bind.autoPlay = autoPlay
  self:refreshView()
end

function this:refreshPreviewPet()
  if not self.isBind then
    return
  end
  L_TimerManager:clearTimer(self)
  self.bind.img_icon = ""
  self.bind.img_base = ""
  self.bind.img_icon_outline = ""
  self.bind.img_base_outline = ""
  self.bind.selectRoot = false
  if math.isEmpty(self.bind.configId) then
    return
  end
  local tpl = _petTpl:getTplById(self.bind.configId)
  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.img_icon = self._initFrameIcon
  self.bind.active_baseShadow = not string.isEmpty(self._initFrameIcon)
  self.bindComponents.img_icon_outline:LoadSprite(iconPath, false)
  local gradeTpl = _petGradeTpl:getTplById(1)
  self.bind.img_base = _petGradeTpl:getPetPixelBase(gradeTpl)
  local baseOutline = string.match(_petGradeTpl:getPetPixelBase(gradeTpl), "%a*%d")
  self.basePath = string.format(BasePath, baseOutline)
  self.bindComponents.img_base_outline:LoadSprite(self.basePath, false)
  local offSetPos = _petTpl:getPetPixelPosition(tpl, 0)
  self.bindComponents.rect_img_icon.anchoredPosition = L_Vector3.new(offSetPos[1], offSetPos[2])
  self.bindComponents.rect_icon_outline.anchoredPosition = L_Vector3.new(offSetPos[1], offSetPos[2])
  if self.bind.autoPlay then
    self:playPixelGif()
  end
end

function this:setConfigId(configId, autoPlay)
  self.bind.configId = configId
  self.bind.autoPlay = autoPlay
  self:refreshPreviewPet()
end

function this:playPixelGif()
  self.bind.selectRoot = true
  self.bindComponents.img_base_outline:LoadSprite(self.basePath, false)
  L_TimerManager:newOrResetTimer(self, "playGif", function()
    if self._curFrame == nil then
      self._curFrame = 1
    end
    self._curFrame = self._curFrame % MaxFrame + 1
    local iconPath = string.format(IconPath, self._defaultPixelId, self._curFrame)
    self.bind.img_icon = iconPath
    self.bind.img_glow_petUseItemEffect = self.bind.img_icon
    local gifPath = string.format(GifPath, self._defaultPixelId, self._curFrame)
    self.bindComponents.img_icon_outline:LoadSprite(gifPath, false)
  end, 0.1, -1)
end

function this:outLineShow(isShow)
  self.bind.selectRoot = isShow == true
  if isShow then
    local iconPath = string.format(IconPath, self._defaultPixelId, 1)
    self.bind.img_icon = iconPath
    local gifPath = string.format(GifPath, self._defaultPixelId, 1)
    self.bindComponents.img_icon_outline:LoadSprite(gifPath, false)
  end
end

function this:stopPixelGif()
  L_TimerManager:stopTimer(self, "playGif")
  self.bind.img_icon = self._initFrameIcon
  self.bind.selectRoot = false
end

function this:setIconAlpha(alpha)
  self.bindComponents.img_icon.color = C_Color(1, 1, 1, alpha)
end

function this:onEvent_refreshPet(entity)
  if not self.isBind then
    return
  end
  if entity.guid == self.bind.guid then
    self:refreshView()
  end
end

function this:setIconBlack(isBlack)
  if isBlack then
    self.bind.selectRoot = true
    self.bind.imgTransActive = false
    self.bind.color_icon = C_Color(0.2549, 0.2588, 0.2352, 1)
    self.bind.color_base = C_Color(0.2549, 0.2588, 0.2352, 0.6)
  else
    self.bind.selectRoot = false
    self.bind.imgTransActive = true
    self.bind.color_icon = C_Color(1, 1, 1, 1)
    self.bind.color_base = C_Color(1, 1, 1, 1)
  end
end

function this:playPetFeedItemEffect(params)
  if self.isPlayingEffect == true then
    return
  end
  params = params or {}
  local pixelEffectPathList, frameTextureConfigInfos = self:getPetEatItemEffectConfigInfo(params)
  if pixelEffectPathList and frameTextureConfigInfos then
    local frontPixelEffectPath = pixelEffectPathList.frontPixelEffectPath
    local backPixelEffectPath = pixelEffectPathList.backPixelEffectPath
    local frontFrameTextureConfigInfo = frameTextureConfigInfos.frontFrameTextureConfigInfo
    local backFrameTextureConfigInfo = frameTextureConfigInfos.backFrameTextureConfigInfo
    self.bind.textureName_frameImage_back_petUseItemEffect = backPixelEffectPath
    self.bind.frameCountPerRow_frameImage_back_petUseItemEffect = backFrameTextureConfigInfo.colNum
    self.bind.frameCountPerCol_frameImage_back_petUseItemEffect = backFrameTextureConfigInfo.rowNum
    self.bind.frameCount_frameImage_back_petUseItemEffect = backFrameTextureConfigInfo.iconNum
    self.bind.textureName_frameImage_front_petUseItemEffect = frontPixelEffectPath
    self.bind.frameCountPerRow_frameImage_front_petUseItemEffect = frontFrameTextureConfigInfo.colNum
    self.bind.frameCountPerCol_frameImage_front_petUseItemEffect = frontFrameTextureConfigInfo.rowNum
    self.bind.frameCount_frameImage_front_petUseItemEffect = frontFrameTextureConfigInfo.iconNum
    self.imgGlowMaterial = self.imgGlowMaterial or self.bindComponents.img_glow_petUseItemEffect.material
    self.bind.frameAnimParamsComplete_frameImage_back_petUseItemEffect = true
    self.bind.frameAnimParamsComplete_frameImage_front_petUseItemEffect = true
    self.bind.active_frameImage_back_petUseItemEffect = true
    self.bind.active_frameImage_front_petUseItemEffect = true
    self.bind.active_img_glow_petUseItemEffect = true
    L_TimerManager:newOrResetTimer(self, "playPetFeedItemEffect", function()
      self:updatePetFeedItemEffect()
    end, 0.05, -1)
    self.isPlayingEffect = true
    self:updatePetFeedItemEffect()
  end
end

function this:updatePetFeedItemEffect()
  self.frameCountHasPlayed2AlphaOfImgGlow = self.frameCountHasPlayed2AlphaOfImgGlow or {
    [1] = 0.25,
    [2] = 0.5,
    [3] = 1
  }
  self.frontFrameImage = self.frontFrameImage or self.bindComponents.frameImage_front_petUseItemEffect
  local curFrameIndex = self.frontFrameImage.curFrameIndex
  local alphaOfImgGlow = self.frameCountHasPlayed2AlphaOfImgGlow[curFrameIndex] or 0
  self.imgGlowMaterial:SetFloat("_Alpha", alphaOfImgGlow)
  if curFrameIndex == self.bind.frameCount_frameImage_front_petUseItemEffect - 1 then
    L_TimerManager:stopTimer(self, "playPetFeedItemEffect")
    self.bind.frameAnimParamsComplete_frameImage_back_petUseItemEffect = false
    self.bind.frameAnimParamsComplete_frameImage_front_petUseItemEffect = false
    self.bind.active_frameImage_back_petUseItemEffect = false
    self.bind.active_frameImage_front_petUseItemEffect = false
    self.bind.active_img_glow_petUseItemEffect = false
    self.isPlayingEffect = false
  end
end

function this:getPetEatItemEffectConfigInfo(params)
  params = params or {}
  local isPetLevelUp = params.isPetLevelUp
  local attrIdList = params.attrIdList or {}
  local petFruitPixelEffectPathInfo = {}
  petFruitPixelEffectPathInfo.frontPixelEffectPath = _gameConstTpl:getValue("PET_EXP_EFFECT_RESOURCEFR")
  petFruitPixelEffectPathInfo.backPixelEffectPath = _gameConstTpl:getValue("PET_EXP_EFFECT_RESOURCEBK")
  local frameTextureConfigInfos = {}
  local rawFrameTextureConfigInfos = _gameConstTpl:getValue("PET_EXP_EFFECT_FRAME")
  for i, rawFrameTextureConfigInfo in pairs(rawFrameTextureConfigInfos) do
    local frameTextureConfigInfo = {}
    frameTextureConfigInfo.rowNum = rawFrameTextureConfigInfo[1]
    frameTextureConfigInfo.colNum = rawFrameTextureConfigInfo[2]
    frameTextureConfigInfo.iconNum = rawFrameTextureConfigInfo[3]
    if i == 1 then
      frameTextureConfigInfos.frontFrameTextureConfigInfo = frameTextureConfigInfo
    else
      frameTextureConfigInfos.backFrameTextureConfigInfo = frameTextureConfigInfo
    end
  end
  return petFruitPixelEffectPathInfo, frameTextureConfigInfos
end

return this
