local base = require("UI.UIActivityCenterTable.Component.ActivityContentBase")
local LockhartActivityMain = BaseClass("LockhartActivityMain", base)
local Localization = CS.GameEntry.Localization
local taskId = "2400000"

function LockhartActivityMain:OnCreate()
  base.OnCreate(self)
  self:ComponentDefine()
end

function LockhartActivityMain:OnDestroy()
  self:ComponentDestroy()
  base.OnDestroy(self)
end

local title_path = "RightView/Top/title"
local sub_title_path = "RightView/Top/subTitle"
local info_btn_path = "RightView/Top/btn/InfoBtn"
local time_title_path = "RightView/Bottom/TimeTitle"
local remain_time_path = "RightView/Bottom/TimeBg/remainTime"
local task_title_path = "RightView/Bottom/GameObject/TaskTitle"
local task_desc_path = "RightView/Bottom/GameObject/TaskDesc"
local item_path = "RightView/Bottom/GameObject/UICommonResItem"
local btn_one_get_path = "RightView/Bottom/GameObject/Btn_OneGet"
local txt_one_get_path = "RightView/Bottom/GameObject/Btn_OneGet/Txt_OneGet"
local gift_btn_path = "RightView/Bottom/GiftBtn"
local btn_go_path = "RightView/Bottom/BtnGo"
local go_text_path = "RightView/Bottom/BtnGo/GoText"
local text_path = "RightView/Bottom/BtnGo/text"
local gift_text_path = "RightView/Bottom/GiftBtn/GiftIcon/GiftText"
local red_point_path = "RightView/Bottom/GameObject/Btn_OneGet/RedPoint"
local gray_sprite_path = "RightView/Bottom/GameObject/GraySprite"
local hero_btn_path = "RightView/Top/PlayePreviewButton"
local bg_rawImage_path = "BgMask/BgRawImage"
local icon_rawImage_path = "BgMask/IconRawImage"

function LockhartActivityMain:ComponentDefine()
  self.bg_rawImage = self:AddComponent(UIRawImage, bg_rawImage_path)
  self.icon_rawImage = self:AddComponent(UIRawImage, icon_rawImage_path)
  self.title = self:AddComponent(UIText, title_path)
  self.sub_title = self:AddComponent(UIText, sub_title_path)
  self.info_btn = self:AddComponent(UIButton, info_btn_path)
  self.hero_btn = self:AddComponent(UIButton, hero_btn_path)
  self.time_title = self:AddComponent(UIText, time_title_path)
  self.remain_time = self:AddComponent(UIText, remain_time_path)
  self.task_title = self:AddComponent(UIText, task_title_path)
  self.task_desc = self:AddComponent(UIText, task_desc_path)
  self.item = self:AddComponent(UICommonResItem, item_path)
  self.btn_one_get = self:AddComponent(UIButton, btn_one_get_path)
  self.txt_one_get = self:AddComponent(UIText, txt_one_get_path)
  self.red_point = self:AddComponent(UIImage, red_point_path)
  self.gray_sprite = self:AddComponent(UIImage, gray_sprite_path)
  self.gift_btn = self:AddComponent(UIButton, gift_btn_path)
  self.btn_go = self:AddComponent(UIButton, btn_go_path)
  self.go_text = self:AddComponent(UIText, go_text_path)
  self.text = self:AddComponent(UIText, text_path)
  self.gift_text = self:AddComponent(UIText, gift_text_path)
  self.hero_btn:SetOnClick(function()
    self:OnHeroBtnClick()
  end)
  self.info_btn:SetOnClick(function()
    self:OnHelpBtnClick()
  end)
  self.btn_one_get:SetOnClick(function()
    self:OnTaskBtnClick()
  end)
  self.gift_btn:SetOnClick(function()
    self:OnShowGiftBtnClick()
  end)
  self.btn_go:SetOnClick(function()
    self:OnBtnGoClick()
  end)
end

function LockhartActivityMain:ComponentDestroy()
  self.bg_rawImage = nil
  self.icon_rawImage = nil
end

function LockhartActivityMain:OnAddListener()
  base.OnAddListener(self)
  self:AddUIListener(EventId.MainTaskSuccess, self.OnUpdateTask)
end

function LockhartActivityMain:OnRemoveListener()
  self:RemoveUIListener(EventId.MainTaskSuccess, self.OnUpdateTask)
  base.OnRemoveListener(self)
end

function LockhartActivityMain:OnUpdateTask()
  self:RefreshUI()
  EventManager:GetInstance():Broadcast(EventId.RefreshActivityRedDot)
end

function LockhartActivityMain:SetData(activityId)
  base.SetData(self, activityId)
  self.activityId = activityId
  if not self.activityId then
    return
  end
  self.activityData = DataCenter.ActivityListDataManager:GetActivityDataById(self.activityId)
  self.title:SetLocalText(self.activityData.bannerTittle)
  self.sub_title:SetLocalText(self.activityData.desc_info)
  self.time_title:SetLocalText("2010114")
  self.task_title:SetLocalText("2010102")
  self.go_text:SetLocalText(self.activityData.para_2)
  self.gift_text:SetLocalText("2010104")
  if not string.IsNullOrEmpty(self.activityData.activity_pic) then
    local picPath = string.format(UIAssets.UILockhartTexturePath, self.activityData.activity_pic)
    self.bg_rawImage:LoadSprite(picPath)
  end
  if not string.IsNullOrEmpty(self.activityData.para_4) then
    self.icon_rawImage:LoadSprite(self.activityData.para_4)
    self.icon_rawImage:SetNativeSize()
  end
  self:RefreshUI()
  CS.GameEntry.Setting:SetBool("OpenedLockhartActivity_" .. LuaEntry.Player.uid, true)
  EventManager:GetInstance():Broadcast(EventId.RefreshActivityRedDot)
end

function LockhartActivityMain:RefreshUI()
  self.red_point:SetActive(false)
  self.gray_sprite:SetActive(false)
  local mgr = DataCenter.ActivityListDataManager
  if self.activityData.tableInfo == TableName.Activity_Stage then
    local stageTemplate = DataCenter.ActivityStageTemplateManager:GetTemplateByStageType(self.activityData.subType)
    if stageTemplate then
      local quests = stageTemplate:GetQuests()
      if not table.IsNullOrEmpty(quests) then
        taskId = quests[1]
      end
    end
  end
  local taskInfo = DataCenter.TaskManager:FindTaskInfo(taskId)
  if taskInfo ~= nil then
    local metaQuest = DataCenter.QuestTemplateManager:GetQuestTemplate(taskInfo.id)
    local maxCount = metaQuest.para2 or 20
    local desc = metaQuest.desc or "2010113"
    local rewardList = taskInfo.rewardList
    if rewardList ~= nil and 0 < #rewardList then
      local reward = rewardList[1]
      if reward ~= nil and reward.itemId ~= nil then
        self.item:ReInit({
          count = reward.count,
          rewardType = RewardType.GOODS,
          itemId = reward.itemId
        })
      else
        self.item:ReInit({
          count = 10,
          rewardType = RewardType.GOODS,
          itemId = 2270012
        })
      end
    else
      self.item:ReInit({
        count = 10,
        rewardType = RewardType.GOODS,
        itemId = 2270012
      })
    end
    self.taskData = taskInfo
    if taskInfo.state == TaskState.NoComplete then
      self.txt_one_get:SetLocalText("170004")
      CS.UIGray.SetGray(self.btn_one_get.transform, true, false)
      if CommonUtil.IsArabic() then
        self.task_desc:SetLocalText(desc, "<color=#f53c3d>" .. taskInfo.num, maxCount .. "</color>")
      else
        self.task_desc:SetLocalText(desc, "<color=#f53c3d>" .. taskInfo.num .. "</color>", maxCount)
      end
    elseif taskInfo.state == TaskState.CanReceive then
      self.txt_one_get:SetLocalText("170004")
      CS.UIGray.SetGray(self.btn_one_get.transform, false, true)
      if CommonUtil.IsArabic() then
        self.task_desc:SetLocalText(desc, "<color=#5fef87>" .. taskInfo.num, maxCount .. "</color>")
      else
        self.task_desc:SetLocalText(desc, "<color=#5fef87>" .. taskInfo.num .. "</color>", maxCount)
      end
      self.red_point:SetActive(true)
    elseif taskInfo.state == TaskState.Received then
      self.txt_one_get:SetLocalText("170003")
      CS.UIGray.SetGray(self.btn_one_get.transform, true, false)
      if CommonUtil.IsArabic() then
        self.task_desc:SetLocalText(desc, "<color=#5fef87>" .. taskInfo.num, maxCount .. "</color>")
      else
        self.task_desc:SetLocalText(desc, "<color=#5fef87>" .. taskInfo.num .. "</color>", maxCount)
      end
      self.gray_sprite:SetActive(true)
    end
  else
    CS.UIGray.SetGray(self.btn_one_get.transform, true, false)
    self.txt_one_get:SetLocalText("170004")
    print("Error, Not Found Config")
    if CommonUtil.IsArabic() then
      self.task_desc:SetLocalText("2010113", "<color=#f53c3d>" .. mgr:GetExtraData(KILL_LOCK_HART_BOSS_LEADER, 0), 20 .. "</color>")
    else
      self.task_desc:SetLocalText("2010113", "<color=#f53c3d>" .. mgr:GetExtraData(KILL_LOCK_HART_BOSS_LEADER, 0) .. "</color>", 20)
    end
    self.item:ReInit({
      count = 10,
      rewardType = RewardType.GOODS,
      itemId = 2270012
    })
  end
  self:RefreshNum()
  self:Update1000MS()
end

function LockhartActivityMain:RefreshNum()
  local itemCount = DataCenter.ItemData:GetItemCount("710001")
  if type(itemCount) == "number" and 0 < itemCount then
    self.text:SetText(itemCount .. "/1")
  else
    self.text:SetText(CommonUtil.IsArabic() and "<color=\"red\">0/1</color>" or "<color=\"red\">0</color>/1")
  end
end

function LockhartActivityMain:Update1000MS()
  if self.activityData ~= nil then
    local curTime = UITimeManager:GetInstance():GetServerTime()
    local remainTime = self.activityData.endTime - curTime
    if 0 < remainTime then
      self.remain_time:SetText(UITimeManager:GetInstance():MilliSecondToFmtString(remainTime))
    else
      self.remain_time:SetText("00:00:00")
    end
  end
end

function LockhartActivityMain:OnTaskBtnClick()
  SFSNetwork.SendMessage(MsgDefines.TaskRewardGet, {id = taskId})
end

function LockhartActivityMain:OnShowGiftBtnClick()
  if self.activityData ~= nil then
    local param = {}
    param.activityData = self.activityData
    UIManager:GetInstance():OpenWindow(UIWindowNames.UIActivityLockhartDetailPopup, {anim = true}, param)
  end
end

function LockhartActivityMain:OnBtnGoClick()
  SFSNetwork.SendMessage(MsgDefines.SummonLockhartBoss, 0, false)
end

function LockhartActivityMain:OnHelpBtnClick()
  if self.activityData ~= nil and self.activityData.story ~= nil then
    local param = {}
    param.activityId = self.activityId
    param.activityRulesStr = Localization:GetString(self.activityData.story)
    UIManager:GetInstance():OpenWindow(UIWindowNames.UIActivityDetailPopup, {anim = true}, param)
  end
end

function LockhartActivityMain:OnHeroBtnClick()
  local heroId = 0
  if not string.IsNullOrEmpty(self.activityData.para_3) then
    local heroIdArr = string.split(self.activityData.para_3, ";")
    for i = 1, #heroIdArr do
      heroId = tonumber(heroIdArr[i])
      local hero_data = DataCenter.HeroDataManager:GetHeroByHeroId(heroId)
      if hero_data ~= nil then
        UIManager:GetInstance():OpenWindow(UIWindowNames.UIHeroDetailPanel, {anim = false}, hero_data.uuid, {
          hero_data.uuid
        })
        return
      end
    end
  end
  if heroId ~= 0 then
    local meta = DataCenter.HeroTemplateManager:GetTemplate(heroId)
    if meta ~= nil and 0 < meta.fragId then
      UIManager:GetInstance():OpenWindow(UIWindowNames.UIHeroDetailPanel, {anim = false}, meta.fragId, {
        meta.fragId
      })
    end
  end
end

function LockhartActivityMain.GetEventCanRewardCount()
  local taskInfo = DataCenter.TaskManager:FindTaskInfo(taskId)
  if taskInfo ~= nil and taskInfo.state == TaskState.CanReceive then
    return 1
  end
  return 0
end

return LockhartActivityMain
