local this = class("moduleMythicalPet", G_UIModuleBase)
local petText = "notice_world_rare_kibo_discovered" or "notice_moduleMythicalPet"
local delay = L_GameTpl:getGameConstTpl():getValue("WORLD_KIBO_DISCOVERED_TIP_TIME", true, 5)
local worldFilterMarkId = 9910

function this.bind()
  return {
    show = false,
    txt_mythicalPet = "\233\153\132\232\191\145\229\135\186\231\142\176\228\186\134\231\168\128\230\156\137\229\165\135\230\179\162<???>"
  }
end

function this.methods()
  return {
    onClick_text = function(self)
      if not self.pos then
        return
      end
      L_UI:open("pageBigMap", {showMythical = true})
    end
  }
end

function this:open(options)
  this.super.open(self, options)
  self.KibossEnterRangeHandle = self.KibossEnterRangeHandle or L_CommonUtil.handle(self.onEvent_KibossEnterRange, self)
  self.KibossExitRangeHandle = self.KibossExitRangeHandle or L_CommonUtil.handle(self.onEvent_KibossExitRange, self)
  self.KibossEnterMapRangeHandle = self.KibossEnterMapRangeHandle or L_CommonUtil.handle(self.onEvent_KibossEnterMapRange, self)
  self.KibossExitMapRangeHandle = self.KibossExitMapRangeHandle or L_CommonUtil.handle(self.onEvent_KibossExitMapRange, self)
  C_WorldEvent.instance:Listen(C_EWorldEvent.OnKibossEnterRange, self.KibossEnterRangeHandle)
  C_WorldEvent.instance:Listen(C_EWorldEvent.OnKibossExitRange, self.KibossExitRangeHandle)
  C_WorldEvent.instance:Listen(C_EWorldEvent.OnKibossEnterMapRange, self.KibossEnterMapRangeHandle)
  C_WorldEvent.instance:Listen(C_EWorldEvent.OnKibossExitMapRange, self.KibossExitMapRangeHandle)
end

function this:close(options)
  this.super.close(self, options)
  C_WorldEvent.instance:Cancel(C_EWorldEvent.OnKibossEnterRange, self.KibossEnterRangeHandle)
  C_WorldEvent.instance:Cancel(C_EWorldEvent.OnKibossExitRange, self.KibossExitRangeHandle)
  C_WorldEvent.instance:Cancel(C_EWorldEvent.OnKibossEnterMapRange, self.KibossEnterMapRangeHandle)
  C_WorldEvent.instance:Cancel(C_EWorldEvent.OnKibossExitMapRange, self.KibossExitMapRangeHandle)
  self:onEvent_KibossExitMapRange()
  self:onEvent_KibossExitRange()
end

function this:onEvent_KibossEnterRange(varList)
  self.bind.show = true
  local petName = "???"
  self.bind.txt_mythicalPet = L_WordsTpl:getValue(petText, {
    [0] = petName
  })
  if self._timer then
    self._timer:reset()
  else
    self._timer = L_GameTimer.new(function()
      self.bind.show = false
    end, delay, 0)
  end
  self._timer:start()
end

function this:onEvent_KibossExitRange()
  self.bind.show = false
  if self._timer then
    self._timer:stop()
  end
end

function this:onEvent_KibossEnterMapRange(eventId, varList)
  local entityId = varList:GetInt(0)
  local csEntity = C_EntityManager.GetEntity(entityId)
  self.pos = csEntity.transform.position
  local pet = {
    id = worldFilterMarkId,
    entityId = entityId,
    sceneId = AzurWorldInstance.CurWorldId,
    pos = self.pos
  }
  L_MapStore:setMythicalPet(pet)
end

function this:onEvent_KibossExitMapRange()
  self.pos = nil
  L_MapStore:setMythicalPet(nil)
end

function this:activate()
  self:open()
  self:openModules()
  self:show()
  self:showModules()
end

function this:deActivate()
  self:hide()
  self:hideModules()
  self:close()
  self:closeModules()
end

return this
