local CallerUnit = require("Scene.CityVisitor.Visitors.VisitorUnit")
local base = CallerUnit
local VisitorGift = BaseClass("VisitorGift", CallerUnit)
local Const = require("Scene.CityVisitor.Const")

function VisitorGift:FinishVisitor()
  local buildDataList = DataCenter.BuildManager:GetAllBuildingByItemIdWithoutPickUp(BuildingTypes.FUN_BUILD_MAIN)
  local pos = SceneUtils.TileIndexToWorld(buildDataList[1].pointId)
  self.isFinish = true
  self:SetFinishTargetPos()
  self:PlayAni(Const.animation.Walk)
  if self.questionIcon then
    self.questionIcon.gameObject:SetActive(false)
  elseif self.visitorData then
    Logger.LogError(string.format("visitor questionIcon cpt is nil. eventId:%s modelPath:%s", self.visitorData.eventId, self.visitorData.modelPath))
  end
  local plot = 0
  local showPlot = false
  if self.param and self.param.finishPlotList then
    local count = #self.param.finishPlotList
    if 0 < count then
      local random = math.random(1, count)
      plot = self.param.finishPlotList[random]
    end
  end
  if 0 < plot and not IsNull(self.transform) then
    if self.plotDelay then
      self.plotDelay:Stop()
    end
    local duration = 0
    local plotData = LocalController:instance():getLine("lw_plot", plot)
    if plotData then
      duration = plotData.duration or 0
    end
    if 0 < duration then
      self.plotDelay = TimerManager:GetInstance():DelayInvoke(function()
        self.plotDelay = nil
        if self.smilingFaceIcon and self.smilingFaceIcon.gameObject then
          self.smilingFaceIcon.gameObject:SetActive(true)
          if self.tipRoot then
            self.tipRoot.gameObject:SetActive(true)
          end
        end
        if self.tipRoot then
          self.delay = TimerManager:GetInstance():DelayInvoke(function()
            if self.tipRoot then
              self.tipRoot.gameObject:SetActive(false)
            end
          end, Const.showEmojiTime)
        end
      end, duration)
      showPlot = true
      if self.tipRoot then
        self.tipRoot.gameObject:SetActive(false)
      end
      local bubbleParams = {}
      bubbleParams.plotId = plot
      bubbleParams.anchor = Vector3.New(0, 3, 0)
      bubbleParams.mode = "3DFollow"
      bubbleParams.followTarget = self.transform
      EventManager:GetInstance():Broadcast(EventId.PlayPlotBubble, bubbleParams)
    end
  end
  if not showPlot then
    if self.smilingFaceIcon then
      self.smilingFaceIcon.gameObject:SetActive(true)
    end
    if self.tipRoot then
      self.tipRoot.gameObject:SetActive(true)
    end
    self.delay = TimerManager:GetInstance():DelayInvoke(function()
      if self.tipRoot then
        self.tipRoot.gameObject:SetActive(false)
      end
    end, Const.showEmojiTime)
  end
  if self.modelTrigger then
    self.modelTrigger.onPointerClick = nil
  end
  if self.trigger then
    self.trigger.onPointerClick = nil
  end
  DataCenter.CityVisitorManager:DeleteVisitor(self.visitorData.uid, self.visitorData.type)
  EventManager:GetInstance():Broadcast(EventId.RefreshVisitorBtnState)
end

function VisitorGift:OnTriggerClick()
  if not self.param then
    return
  end
  if self.param.isNewLogic then
    base.OnTriggerClick(self)
  else
    self:OnConfirmClick()
  end
end

function VisitorGift:OnConfirmClick()
  self:OnDelayClick(function()
    if self.visitorData then
      SFSNetwork.SendMessage(MsgDefines.VisitorOperateMessage, self.visitorData.uid, 1)
    else
      UIManager.Instance:DestroyWindow(UIWindowNames.UICityVisitor, {
        anim = false,
        UIMainAnim = UIMainAnimType.AllShow
      })
    end
  end)
end

function VisitorGift:CancelClick()
  self:OnDelayClick(function()
    if self.visitorData then
      SFSNetwork.SendMessage(MsgDefines.VisitorOperateMessage, self.visitorData.uid, 0)
    else
      UIManager.Instance:DestroyWindow(UIWindowNames.UICityVisitor, {
        anim = false,
        UIMainAnim = UIMainAnimType.AllShow
      })
    end
  end)
end

function VisitorGift:OnFinish()
  DataCenter.CityVisitorManager.RemoveDeleteCacheListToUid(self.uid)
end

function VisitorGift:ClearData()
  if self.plotDelay then
    self.plotDelay:Stop()
    self.plotDelay = nil
  end
  base.ClearData(self)
end

return VisitorGift
