local this = L_BevTree:registClass("guide.showInfoWindow", L_BevTree.action)

function this:param()
  self.guidePic = ""
  self.tutorialId = 0
  self.showTime = 0
end

function this:parse()
  self.guidePic = self:getParam("guidePic")
  self.tutorialId = self:getParam("tutorialId", true) or 0
  self.showTime = self:getParam("showTime", true) or 0
end

function this:onEnter()
  self.isDestroy = false
  L_GuideManager:addListener(G_GuideClasses.GuideEvent.TutotialGuideEnd, self.onTutotialGuideEnd, self)
  if string.isEmpty(self.guidePic) and self.tutorialId == 0 then
    self.result = L_BevTree.taskResult.Failure
  else
    self.result = L_BevTree.taskResult.Running
  end
end

function this:execute()
  if self.isDestroy then
    return self.result
  end
  if self.result == L_BevTree.taskResult.Running then
    if self.waitGuideInfoClose then
      return self.result
    end
    if self.waitTutorial then
      return self.result
    end
    if self.tutorialId ~= 0 then
      self:dealTutorial()
      return self.result
    end
    if not string.isEmpty(self.guidePic) then
      self:dealOpenGuideInfoWindow()
    end
  end
  return self.result
end

function this:onTutotialGuideEnd()
  self:disposeTimer()
  if not self.isDestroy then
    self:dealOpenGuideInfoWindow()
  end
  self.waitTutorial = false
end

function this:dealTutorial()
  self.waitTutorial = true
  L_GuideManager:internalSendEvent(G_GuideClasses.GuideEvent.TutotialGuideStart, {
    tutorialId = self.tutorialId,
    showTime = self.showTime
  })
  local tutorialTpl = L_GameTpl:getTutorialTpl()
  local tutorialConfig = tutorialTpl:getTplById(self.tutorialId)
  self.showInfoGuidPic = tutorialTpl:getGuidePicId(tutorialConfig)
  self:disposeTimer()
  self.tutorialTimer = Timer.once(self.showTime, function()
    self.result = L_BevTree.taskResult.Success
    self.waitTutorial = false
  end)
end

function this:dealOpenGuideInfoWindow()
  local status = L_UI:getPageStatus("pageGuideInfoWindow")
  if not status or status == "closed" then
    L_GuideManager:setWorldPause(true)
    local pic = string.split(self.guidePic, "|")
    if self.showInfoGuidPic ~= nil then
      pic = self.showInfoGuidPic
    end
    L_UI:open("pageGuideInfoWindow", {
      pics = pic,
      finishCallback = function()
        self.waitGuideInfoClose = false
        self.result = L_BevTree.taskResult.Success
      end
    })
    self.waitGuideInfoClose = true
  end
end

function this:doDestroy()
  self:dispose()
  L_GuideManager:internalSendEvent(G_GuideClasses.GuideEvent.TutotialGuideEnd)
end

function this:onDestroy()
  self:doDestroy()
end

function this:onReset()
  self:doDestroy()
end

function this:onFinish()
  self:dispose()
end

function this:dispose()
  self.isDestroy = true
  if self.waitGuideInfoClose and L_UI:checkPageOpen("pageGuideInfoWindow") then
    L_UI:close("pageGuideInfoWindow")
  end
  L_GuideManager:removeListener(G_GuideClasses.GuideEvent.TutotialGuideEnd, self.onTutotialGuideEnd, self)
  L_GuideManager:setWorldPause(false)
  self:disposeTimer()
end

function this:disposeTimer()
  if self.tutorialTimer ~= nil then
    Timer.remove(self.tutorialTimer)
    self.tutorialTimer = nil
  end
end

return this
