local this = class("moduleScanning", G_UIModuleBase)

function this.bind()
  return {
    active_content = false,
    active_cdMask = false,
    toggle_disableScanning = false,
    fill_cdMask = 0
  }
end

function this.methods()
  return {
    onClick_startScan = function(self)
      self:startScan()
    end
  }
end

function this:open(...)
  this.super.open(self, ...)
  self:initSystem()
  self.scanningCd = L_GameTpl:getGameConstTpl():getValue("SCAN_CD", true) / 10000
  L_ShortCutManager:addListener(L_ShortCutConst.event.StartScanning, self.startScan, self)
  L_ShortCutManager:addShortKeyFlag(self, L_Const.settingType.scanning, L_Const.AnchorType.Bottom, self.bindComponents.rTransRider, L_Vector3.new(0, -10))
end

function this:close(...)
  this.super.close(self, ...)
  self:disposeScanCdTimer()
  self.disableScanning = false
  L_ShortCutManager:removeListener(L_ShortCutConst.event.StartScanning, self.startScan, self)
  L_ShortCutManager:clearShortKeyFlag(self)
end

function this:hide()
  self:hideModules()
end

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

function this:deActivate()
  self:hide()
  self:hideModules()
  self:close()
  self:closeModules()
  L_BattleDataManager:sendEvent(L_BattleDataManager.event.refreshPageMainLeftBottomLayout)
end

function this:initSystem()
  self.systemId = L_SystemConst.enum.scanning
  self.locker = L_SystemUnlockManager:tryGenerateLocker(self.systemId)
  self:refreshSystem(self.systemId, self.locker == nil)
  if self.locker then
    self.locker:setCallback(self.refreshSystem, self, self.gameObject)
  end
end

function this:refreshSystem(systemId, isUnlock)
  if systemId ~= self.systemId then
    return
  end
  self.bind.active_content = isUnlock
  self.isUnlock = isUnlock
  L_BattleDataManager:sendEvent(L_BattleDataManager.event.refreshPageMainLeftBottomLayout)
end

function this:startScan()
  if not self.isUnlock then
    return
  end
  if self.cd and self.cd > 0 then
    return
  end
  if self.disableScanning then
    self:disposeScanCdTimer()
    return
  end
  AzurWorld.ScanMgr:StartScan()
  self:disposeScanCdTimer()
  self.bind.active_cdMask = true
  self.cd = self.scanningCd
  self.scanCdTimer = Timer.repeated(0.1, function()
    if self.cd <= 0 then
      self:disposeScanCdTimer()
      return
    end
    self.cd = self.cd - 0.1
    self.bind.fill_cdMask = self.cd / self.scanningCd
  end, self)
end

function this:disposeScanCdTimer()
  if self.scanCdTimer ~= nil then
    Timer.remove(self.scanCdTimer)
    self.scanCdTimer = nil
  end
  self:resetScanCdTimerInfo()
end

function this:resetScanCdTimerInfo()
  self.cd = 0
  self.bind.active_cdMask = false
end

return this
