local base = UIBaseContainer
local LWMainUIActivityAlarmClockObj = BaseClass("LWMainUIActivityAlarmClockObj", base)
local Localization = CS.GameEntry.Localization
local closeY = 30
local icon_path = "Icon"
local closeBtn_path = "CloseBtn"
local desText_path = "DesText"
local subIcon_path = "SubIcon"
local jumpToBtn_path = "JumpToBtn"
local simpleAnim_path = ""
local OnCreate = function(self)
  base.OnCreate(self)
  self:ComponentDefine()
  self:DataDefine()
end
local OnDestroy = function(self)
  self:DataDestroy()
  self:ComponentDestroy()
  base.OnDestroy(self)
end
local OnEnable = function(self)
  base.OnEnable(self)
end
local OnDisable = function(self)
  base.OnDisable(self)
end
local ComponentDefine = function(self)
  self.icon = self:AddComponent(UIImage, icon_path)
  self.closeBtn = self:AddComponent(UIButton, closeBtn_path)
  self.desText = self:AddComponent(UIText, desText_path)
  self.subIcon = self:AddComponent(UIImage, subIcon_path)
  self.jumpToBtn = self:AddComponent(UIEventTrigger, jumpToBtn_path)
  self.simpleAnim = self:AddComponent(UISimpleAnimation, simpleAnim_path)
  self.closeBtn:SetOnClick(function()
    self:CloseBtnClick()
  end)
  self.jumpToBtn:OnPointerClick(function(eventData)
    self:JumpToBtnClick(eventData)
  end)
  self.jumpToBtn:OnBeginDrag(function(eventData)
    self:OnBeginDrag(eventData)
  end)
  self.jumpToBtn:OnEndDrag(function(eventData)
    self:OnEndDrag(eventData)
  end)
  self.jumpToBtn:OnDrag(function(eventData)
    self:OnDrag(eventData)
  end)
end
local ComponentDestroy = function(self)
  self.icon = nil
  self.closeBtn = nil
  self.desText = nil
  self.subIcon = nil
  self.jumpToBtn = nil
  self.simpleAnim = nil
end
local DataDefine = function(self)
  self.curShowActivityAlarmClockInfo = nil
  self.isUpdate = false
  self.isShow = false
  self.name = ""
  self.dragPosY = nil
  self.inAnimTimer = nil
  self.outAnimTimer = nil
  self.intAnimFinshReInit = false
end
local DataDestroy = function(self)
  self.curShowActivityAlarmClockInfo = nil
  self.isUpdate = nil
  self.isShow = nil
  self.name = nil
  self.dragPosY = nil
  self.inAnimTimer = nil
  self.outAnimTimer = nil
  self.intAnimFinshReInit = nil
end
local OnAddListener = function(self)
  base.OnAddListener(self)
  self:AddUIListener(EventId.AllianceQuitOK, self.OnAllianceQuitSuccess)
end
local OnRemoveListener = function(self)
  self:RemoveUIListener(EventId.AllianceQuitOK, self.OnAllianceQuitSuccess)
  base.OnRemoveListener(self)
end
local OnAllianceQuitSuccess = function(self)
  self.isUpdate = false
  self:SetActiveState(false)
end
local Update1000MS = function(self)
  if not self.isUpdate then
    return
  end
  if self.curShowActivityAlarmClockInfo then
    local curTime = UITimeManager:GetInstance():GetServerTime()
    if curTime >= self.curShowActivityAlarmClockInfo.showMainUITopStartTime and curTime <= self.curShowActivityAlarmClockInfo.showMainUITopEndTime then
      self:ShowView()
    elseif self.isShow then
      self:CloseBtnClick()
    end
  end
end
local ReInit = function(self, data)
  self.curShowActivityAlarmClockInfo = data
  self.isUpdate = true
  self.dragPosY = nil
  local curTime = UITimeManager:GetInstance():GetServerTime()
  self:SetCurActivityAlarmClockInfo()
  if self.curShowActivityAlarmClockInfo and curTime >= self.curShowActivityAlarmClockInfo.showMainUITopStartTime and curTime <= self.curShowActivityAlarmClockInfo.showMainUITopEndTime then
    self:ShowView()
  else
    self:SetActiveState(false)
  end
end
local ShowView = function(self)
  if not self.isShow then
    self:SetActiveState(true)
    self:RefreshTimeView()
    if not self.inAnimTimer or not not self.inAnimTimer:IsOver() then
      local _, inTime = self.simpleAnim:PlayAnimationReturnTime("in")
      self.inAnimTimer = TimerManager:GetInstance():DelayInvoke(function()
      end, inTime)
    end
  else
    self:RefreshTimeView()
  end
end
local SetActiveState = function(self, isShow)
  self.isShow = isShow
  self.gameObject:SetActive(isShow)
end
local SetCurActivityAlarmClockInfo = function(self)
  if not self.curShowActivityAlarmClockInfo then
    return
  end
  local template = self.curShowActivityAlarmClockInfo.template
  if template then
    self.name = self.curShowActivityAlarmClockInfo:GetName()
    local iconPath = self.curShowActivityAlarmClockInfo:GetIconPath()
    self.icon:LoadSprite(iconPath)
    if string.IsNullOrEmpty(template.sub_icon) then
      self.subIcon:SetActive(false)
    else
      self.subIcon:LoadSprite(template.sub_icon)
    end
  end
end
local RefreshTimeView = function(self)
  local curTime = UITimeManager:GetInstance():GetServerTime()
  local surplusTime = self.curShowActivityAlarmClockInfo.showMainUITopEndTime - curTime
  if surplusTime <= 0 then
    self:CloseBtnClick()
  else
    local template = self.curShowActivityAlarmClockInfo.template
    if template then
      local timeStr = UITimeManager:GetInstance():MilliSecondToFmtString(surplusTime)
      if template.type == ActivityAlarmClockType.KingdomPosition then
        self.desText:SetLocalText(template.timer_dialog, timeStr, self.name)
      elseif template.type == ActivityAlarmClockType.AllianceTrain then
        self.desText:SetLocalText(template.timer_dialog, timeStr)
      elseif template.type == ActivityAlarmClockType.AllianceStar then
        self.desText:SetLocalText(template.timer_dialog, timeStr)
      elseif template.type == ActivityAlarmClockType.Meteorite then
        self.desText:SetLocalText(template.timer_dialog, timeStr)
      elseif template.type == ActivityAlarmClockType.KillZombie then
        self.desText:SetLocalText(template.timer_dialog, timeStr)
      elseif template.type == ActivityAlarmClockType.MonsterInvasion then
        self.desText:SetLocalText(template.timer_dialog, timeStr)
      elseif template.type == ActivityAlarmClockType.QueenOfBlood then
        self.desText:SetLocalText(template.timer_dialog, timeStr)
      elseif template.type == ActivityAlarmClockType.AllyDrillBooking then
        self.desText:SetLocalText(template.timer_dialog, timeStr)
      else
        self.desText:SetLocalText(template.timer_dialog, self.name, timeStr)
      end
    else
      self.desText:SetText("")
    end
  end
end
local InnerClose = function(self)
  self.isUpdate = false
  self:SetActiveState(false)
  self.dragPosY = nil
  DataCenter.LWActivityAlarmClockManager:RecordLocalAlarmClockData(self.curShowActivityAlarmClockInfo)
  DataCenter.LWActivityAlarmClockManager:RemoveNeedShowMainUITopAlarmClock(self.curShowActivityAlarmClockInfo)
  local newInfo = DataCenter.LWActivityAlarmClockManager:GetNeedShowMainUITopAlarmClockData()
  if newInfo then
    self:ReInit(newInfo)
  else
    self.curShowActivityAlarmClockInfo = nil
  end
end
local CloseBtnClick = function(self)
  if not self.outAnimTimer or not not self.outAnimTimer:IsOver() then
    local _, outTime = self.simpleAnim:PlayAnimationReturnTime("out")
    self.outAnimTimer = TimerManager:GetInstance():DelayInvoke(function()
      InnerClose(self)
    end, outTime)
  end
end
local JumpToBtnClick = function(self)
  if self:IsPlayAnim() then
    return
  end
  if self.isUpdate and self.dragPosY == nil then
    DataCenter.LWActivityAlarmClockManager:JumpTo(self.curShowActivityAlarmClockInfo)
  end
end
local OnBeginDrag = function(self, eventData)
  if self:IsPlayAnim() then
    return
  end
  if self.isUpdate then
    self.dragPosY = eventData.position.y
  end
end
local OnEndDrag = function(self, eventData)
  if self:IsPlayAnim() then
    return
  end
  self.dragPosY = nil
end
local OnDrag = function(self, eventData)
  if self:IsPlayAnim() then
    return
  end
  if self.isUpdate and self.dragPosY and eventData.position.y - self.dragPosY >= closeY then
    self:CloseBtnClick()
  end
end
local IsPlayAnim = function(self)
  local isPlay = false
  if not (not self.inAnimTimer or self.inAnimTimer:IsOver()) or self.outAnimTimer and not self.outAnimTimer:IsOver() then
    isPlay = true
  end
  return isPlay
end
LWMainUIActivityAlarmClockObj.OnCreate = OnCreate
LWMainUIActivityAlarmClockObj.OnDestroy = OnDestroy
LWMainUIActivityAlarmClockObj.OnEnable = OnEnable
LWMainUIActivityAlarmClockObj.OnDisable = OnDisable
LWMainUIActivityAlarmClockObj.ComponentDefine = ComponentDefine
LWMainUIActivityAlarmClockObj.ComponentDestroy = ComponentDestroy
LWMainUIActivityAlarmClockObj.DataDefine = DataDefine
LWMainUIActivityAlarmClockObj.DataDestroy = DataDestroy
LWMainUIActivityAlarmClockObj.OnAddListener = OnAddListener
LWMainUIActivityAlarmClockObj.OnRemoveListener = OnRemoveListener
LWMainUIActivityAlarmClockObj.OnAllianceQuitSuccess = OnAllianceQuitSuccess
LWMainUIActivityAlarmClockObj.Update1000MS = Update1000MS
LWMainUIActivityAlarmClockObj.ReInit = ReInit
LWMainUIActivityAlarmClockObj.ShowView = ShowView
LWMainUIActivityAlarmClockObj.SetActiveState = SetActiveState
LWMainUIActivityAlarmClockObj.RefreshTimeView = RefreshTimeView
LWMainUIActivityAlarmClockObj.CloseBtnClick = CloseBtnClick
LWMainUIActivityAlarmClockObj.JumpToBtnClick = JumpToBtnClick
LWMainUIActivityAlarmClockObj.OnBeginDrag = OnBeginDrag
LWMainUIActivityAlarmClockObj.OnEndDrag = OnEndDrag
LWMainUIActivityAlarmClockObj.OnDrag = OnDrag
LWMainUIActivityAlarmClockObj.IsPlayAnim = IsPlayAnim
LWMainUIActivityAlarmClockObj.SetCurActivityAlarmClockInfo = SetCurActivityAlarmClockInfo
return LWMainUIActivityAlarmClockObj
