local UINpcTaskBubble = BaseClass("UINpcTaskBubble")
local icon_path = "Go/Bg/Icon"
local bg_path = "Go/Bg"
local trigger_path = "Go/Trigger"
local OnCreate = function(self, go)
  if go ~= nil then
    self.request = go
    self.gameObject = go.gameObject
    self.transform = go.gameObject.transform
  end
  self:ComponentDefine()
  self:DataDefine()
end
local OnDestroy = function(self)
  self:ComponentDestroy()
  self:DataDestroy()
end
local ComponentDefine = function(self)
  if not self.defend then
    self.icon_sprite = self.transform:Find(icon_path):GetComponent(typeof(CS.SpriteMeshRenderer))
    self.bg_sprite = self.transform:Find(bg_path):GetComponent(typeof(CS.SpriteMeshRenderer))
    self.bg = self.transform:Find(trigger_path):GetComponent(typeof(CS.TouchObjectEventTrigger))
    
    function self.bg.onPointerClick()
      self:OnClick()
    end
    
    self.defend = true
  end
  self:RefreshRed()
end
local ComponentDestroy = function(self)
  self.bg.onPointerClick = nil
  self.icon_sprite = nil
  self.red_sprite = nil
  self.bg_sprite = nil
end
local DataDefine = function(self)
end
local DataDestroy = function(self)
  self.defend = nil
end
local RefreshRed = function(self)
  local list = DataCenter.TaskManager:GetAllMainTask()
  if self.gameObject ~= nil then
    self.bg_sprite.gameObject:SetActive(false)
    if list and next(list) then
      for i = 1, #list do
        if list[i].state == TaskState.CanReceive then
          self.bg_sprite.gameObject:SetActive(true)
        end
      end
    end
  end
  EventManager:GetInstance():Broadcast(EventId.NPCQuestBubbleNeedRefresh)
end
local OnClick = function(self)
  if DataCenter.GuideManager:InGuide() then
    return
  end
  if self.bg_sprite ~= nil and not self.bg_sprite.gameObject.activeSelf and DataCenter.NpcQABubbleManager:IsNpcQABubbleShow() then
    return
  end
  UIManager:GetInstance():OpenWindow(UIWindowNames.UIMainTask)
end
local IsMainTaskRedShow = function(self)
  return self.bg_sprite ~= nil and self.bg_sprite.gameObject.activeSelf
end
UINpcTaskBubble.OnCreate = OnCreate
UINpcTaskBubble.OnDestroy = OnDestroy
UINpcTaskBubble.ComponentDefine = ComponentDefine
UINpcTaskBubble.ComponentDestroy = ComponentDestroy
UINpcTaskBubble.DataDefine = DataDefine
UINpcTaskBubble.DataDestroy = DataDestroy
UINpcTaskBubble.RefreshRed = RefreshRed
UINpcTaskBubble.OnClick = OnClick
UINpcTaskBubble.IsMainTaskRedShow = IsMainTaskRedShow
return UINpcTaskBubble
