local this = class("modulePetCard", G_UIModuleBase)
local _petTpl = L_GameTpl:getPetTpl()
local _elementTypeTpl = L_GameTpl:getElementTypeTpl()
local _petCatchItemTpl = L_GameTpl:getPetCatchItemTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()

function this.bind()
  return {
    frame = "",
    back = "",
    glow = "",
    bgEffect = "",
    petCard = "",
    frontEffect = "",
    elementIcon = "",
    elementFrame = "",
    subElementFrame = "",
    subElementIcon = "",
    bgIcon = "",
    bgExtends = "",
    show_shiningToggle = false,
    show_frontEffect = true,
    show_bgEffect = true,
    show_petCard = true,
    show_bg = true,
    show_tips = false,
    show_element = true,
    show_subElement = false,
    show_cardRoot = true,
    show_shiningFx = false,
    show_shiningFrame = false
  }
end

function this:open()
  if L_DeviceTpl:getIsPc() then
    self.updateHandle = handler(self, self.update)
    C_UpdateSource.AddUpdateEventHandler(self.updateHandle)
  end
end

function this:close()
  if self.updateHandle then
    C_UpdateSource.RemoveUpdateEventHandler(self.updateHandle)
  end
  L_TimerManager:clearTimer(self)
end

function this:setGuid(guid)
  local pet = L_PetStore:getPetItem(guid)
  self.pet = pet
  local tpl = _petTpl:getTplById(pet.id)
  if tpl then
    self.init = true
    self.bind.show_shiningFx = pet:hasFlashGene()
    self.bind.show_shiningFrame = pet:hasFlashGene()
    self.bind.show_shiningToggle = pet:hasFlashGene()
    local petCardPath = _petTpl:getKiboBoxCardIcon(tpl, 5, pet:isSpecialPet())
    if string.isEmpty(petCardPath) then
      self.bind.show_tips = true
      self.bind.show_element = false
      self.bind.show_bg = false
      self.bind.show_petCard = false
      self.bind.show_frontEffect = false
      self.bind.show_bgEffect = false
      self.bind.frame = "UI/Atlas/PetBoxCard/tex_pet_card_element_0.png"
      self.bind.back = "UI/Atlas/PetBoxCard/tex_icon_petcard_quality_1.png"
      self.bind.glow = "UI/Atlas/PetBoxCard/tex_icon_petcard_quality_1.png"
    else
      self:initCommonBack()
      self:initCommonInfo(tpl)
    end
  end
end

function this:update()
  if not self.init or not self.isBind then
    return
  end
  if not self.isPlayAnim and L_DeviceTpl:getIsPc() then
    local percent = math.clamp(Unity.Input.mousePosition.x / Unity.Screen.width, 0, 1)
    local rot = math.lerp(10, -10, percent)
    local rotZ = math.lerp(0, 3.3, percent)
    self.bindComponents.frameTrans.localRotation = C_Quaternion.Euler(C_Vector3(0, rot, rotZ))
    local x = math.lerp(-10, 30, percent)
    local y = math.lerp(0, 0.2, percent)
    local scale = math.lerp(1, 1.03, percent)
    self.bindComponents.frontEffectTrans.localPosition = L_Vector3.new(x, y, 0)
    self.bindComponents.frontEffectTrans.localScale = L_Vector3.new(scale, scale, scale)
  end
end

function this:setInitRdy()
  self.init = true
end

function this:setPidWithAnim(petId, isSpecial, isFlash)
  self.isPlayAnim = false
  local tpl = _petTpl:getTplById(petId)
  if tpl then
    self.pet = {
      isSpecialPet = function()
        return isSpecial
      end
    }
    self.bind.show_shiningFx = isFlash
    self.bind.show_shiningFrame = isFlash
    self.bind.show_shiningToggle = isFlash
    self.bindComponents.anim:Stop()
    self.bindComponents.anim:Play("anim_pet_card_switch")
    L_AudioUtil.playSound("Play_SFX_System_UI_QiboHouse_Select")
    self.isPlayAnim = true
    local petCardPath = _petTpl:getKiboBoxCardIcon(tpl, 5, isSpecial or false)
    if string.isEmpty(petCardPath) then
      L_TimerManager:clearTimer(self)
      self.bind.show_tips = true
      self.bind.show_element = false
      self.bind.show_bg = false
      self.bind.show_petCard = false
      self.bind.show_frontEffect = false
      self.bind.show_bgEffect = false
      self.bind.frame = "UI/Atlas/PetBoxCard/tex_pet_card_element_0.png"
      self.bind.back = "UI/Atlas/PetBoxCard/tex_icon_petcard_quality_1.png"
      self.bind.glow = "UI/Atlas/PetBoxCard/tex_icon_petcard_quality_1.png"
    else
      self:initCommonBack()
      L_TimerManager:newOrResetTimer(self, "delayInitInfo", function()
        self:initCommonInfo(tpl)
        self.isPlayAnim = false
      end, 0.15)
    end
  end
end

function this:setGuidWithAnim(guid)
  local pet = L_PetStore:getPetItem(guid)
  self.pet = pet
  self.isPlayAnim = false
  local tpl = _petTpl:getTplById(pet.id)
  if tpl then
    self.bindComponents.anim:Stop()
    self.bindComponents.anim:Play("anim_pet_card_switch")
    L_AudioUtil.playSound("Play_SFX_System_UI_QiboHouse_Select")
    self.isPlayAnim = true
    self.bind.show_shiningFx = pet:hasFlashGene()
    self.bind.show_shiningFrame = pet:hasFlashGene()
    self.bind.show_shiningToggle = pet:hasFlashGene()
    local petCardPath = _petTpl:getKiboBoxCardIcon(tpl, 5, pet:isSpecialPet())
    if string.isEmpty(petCardPath) then
      L_TimerManager:clearTimer(self)
      self.bind.show_tips = true
      self.bind.show_element = false
      self.bind.show_bg = false
      self.bind.show_petCard = false
      self.bind.show_frontEffect = false
      self.bind.show_bgEffect = false
      self.bind.frame = "UI/Atlas/PetBoxCard/tex_pet_card_element_0.png"
      self.bind.back = "UI/Atlas/PetBoxCard/tex_icon_petcard_quality_1.png"
      self.bind.glow = "UI/Atlas/PetBoxCard/tex_icon_petcard_quality_1.png"
    else
      self:initCommonBack()
      L_TimerManager:newOrResetTimer(self, "delayInitInfo", function()
        self:initCommonInfo(tpl)
        self.isPlayAnim = false
      end, 0.15)
    end
  end
end

function this:initCommonBack()
  self.bind.show_tips = false
  self.bind.show_element = true
  self.bind.show_bg = true
  self.bind.show_petCard = true
  local catchItemId = self.pet.catch_item
  if math.isEmpty(catchItemId) then
    self.bind.back = "UI/Atlas/PetBoxCard/tex_icon_petcard_quality_1.png"
    self.bind.glow = "UI/Atlas/PetBoxCard/tex_icon_petcard_quality_1.png"
  else
    local commonItemTpl = _commonItemTpl:getTplById(catchItemId)
    local subId = _commonItemTpl:getSubId(commonItemTpl)
    local catchItemTpl = _petCatchItemTpl:getTplById(subId)
    self.bind.back = _petCatchItemTpl:getCardBgImg(catchItemTpl)
    self.bind.glow = _petCatchItemTpl:getCardBgImg(catchItemTpl)
  end
end

function this:initCommonInfo(tpl)
  local petElement = _petTpl:getElement(tpl)
  local elementTpl = _elementTypeTpl:getTplById(petElement[1])
  self.bind.frame = _elementTypeTpl:getKiBoCardElementBg(elementTpl, 3)
  self.bind.elementFrame = _elementTypeTpl:getKiBoCardElementBg(elementTpl, 4)
  self.bind.elementIcon = _elementTypeTpl:getKiBoCardIcon(elementTpl)
  if 1 < #petElement then
    self.bind.show_subElement = true
    local subElementTpl = _elementTypeTpl:getTplById(petElement[2])
    self.bind.subElementFrame = _elementTypeTpl:getKiBoCardElementBg(subElementTpl, 4)
    self.bind.subElementIcon = _elementTypeTpl:getKiBoCardIcon(subElementTpl)
  else
    self.bind.show_subElement = false
  end
  self.bindComponents.petCard:LoadSprite(_petTpl:getKiboBoxCardIcon(tpl, 5, self.pet:isSpecialPet()), false)
  self.bindComponents.PetShining:LoadSprite(_petTpl:getKiboBoxCardIcon(tpl, 5, self.pet:isSpecialPet()), false)
  self.bindComponents.bgIcon:LoadSprite(_petTpl:getKiboBoxCardIcon(tpl, 3, self.pet:isSpecialPet()), false)
  self.bindComponents.bgExtends:LoadSprite(_petTpl:getKiboBoxCardIcon(tpl, 4, self.pet:isSpecialPet()), false)
  local showBgEffect = C_LuaUtility.ExistsAsset(_petTpl:getKiboBoxCardIcon(tpl, 1))
  self.bind.show_bgEffect = showBgEffect
  if self.bind.show_bgEffect then
    self.bindComponents.bgEffect:LoadSprite(_petTpl:getKiboBoxCardIcon(tpl, 1), false)
  end
  local showFrontEffect = C_LuaUtility.ExistsAsset(_petTpl:getKiboBoxCardIcon(tpl, 2))
  self.bind.show_frontEffect = showFrontEffect
  if self.bind.show_frontEffect then
    self.bindComponents.frontEffect:LoadSprite(_petTpl:getKiboBoxCardIcon(tpl, 2), false)
  end
end

function this:enableCard(bool)
  self.bind.show_cardRoot = bool
end

return this
