local ParkourBonusProgressPanel = BaseClass("ParkourBonusProgressPanel", UIBaseContainer)
local GameQualitySettings = require("Util.GameQualitySettings")
local base = UIBaseContainer
local Localization = CS.GameEntry.Localization
local ParkourBonusProgressItem = require("UI.UIParkour.MainUI.Component.ParkourBonus.ParkourBonusProgressItem")
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.slider = self:AddComponent(UISlider, "Slider")
  self.imgIcon = self:AddComponent(UIImage, "Icon")
  self.compSliderItem1 = self:AddComponent(ParkourBonusProgressItem, "Slider/SliderList/SliderItem1")
  self.compSliderItem2 = self:AddComponent(ParkourBonusProgressItem, "Slider/SliderList/SliderItem2")
  self.compSliderItem3 = self:AddComponent(ParkourBonusProgressItem, "Slider/SliderList/SliderItem3")
end
local ComponentDestroy = function(self)
  self.slider = nil
  self.imgIcon = nil
  self.compSliderItem1 = nil
  self.compSliderItem2 = nil
  self.compSliderItem3 = nil
end
local DataDefine = function(self)
  self.sliderFullLen = self.slider:GetSizeDelta().x
  self.sliderNodeProgress = {}
  self.scoreItemList = {}
  for i = 1, 3 do
    local comp = self["compSliderItem" .. i]
    table.insert(self.scoreItemList, comp)
    table.insert(self.sliderNodeProgress, comp:GetAnchoredPositionX() / self.sliderFullLen)
  end
end
local DataDestroy = function(self)
  self.sliderFullLen = nil
  self.sliderNodeProgress = nil
  self.scoreItemList = nil
end
local OnAddListener = function(self)
  base.OnAddListener(self)
end
local OnRemoveListener = function(self)
  base.OnRemoveListener(self)
end
local SetData = function(self, param)
  self.progressData = param
  self.nowScore = 0
  local maxNum = self.progressData[#self.progressData].progressNum
  for i, v in ipairs(self.scoreItemList) do
    v:SetData(self.progressData[i], maxNum)
  end
  self.slider:SetValue(0)
end
local Refresh = function(self, param)
  local pic = DataCenter.ResourceManager:GetResourceIconByType(param.goodsId)
  local srcPos = CS.CSUtils.WorldPositionToUISpacePosition(param.worldPosition)
  local targetPos = self.imgIcon.transform.position
  local FlyParkourPath = "Assets/_Art/Effect/prefab/ui/Common/FlyParkour.prefab"
  if GameQualitySettings.IsLowGearQuality() then
    FlyParkourPath = "Assets/_Art/Effect/prefab/ui/Common/FlyParkourNoTrail.prefab"
  end
  local nowScore = self.nowScore + param.goodsCount
  self.nowScore = nowScore
  DataCenter.FlyController.DoFlyForLua(pic, nil, 1, srcPos, targetPos, 40, 40, function()
    if self.scoreItemList then
      for i, v in ipairs(self.scoreItemList) do
        v:Refresh(nowScore)
      end
      self.slider:SetValue(self:GetScoreProgress(nowScore))
    end
  end, FlyParkourPath, nil, -50, nil, nil)
end
local GetScoreProgress = function(self, checkScore)
  local zeroScore = 0
  local nowIndex = 0
  local score = checkScore - zeroScore
  for i = 1, #self.progressData do
    local needScore = self.progressData[i].progressNum
    if checkScore >= needScore then
      nowIndex = i
    else
      if 1 < i then
        score = checkScore - self.progressData[i - 1].progressNum
      end
      break
    end
  end
  local value = 0
  if nowIndex == #self.progressData then
    value = self.sliderNodeProgress[#self.sliderNodeProgress]
  else
    local preScore = zeroScore
    if 0 < nowIndex then
      preScore = self.progressData[nowIndex].progressNum
    end
    local nodeRatio = score / (self.progressData[nowIndex + 1].progressNum - preScore)
    local preProgress = 0
    if 0 < nowIndex then
      preProgress = self.sliderNodeProgress[nowIndex]
    end
    value = preProgress + nodeRatio * (self.sliderNodeProgress[nowIndex + 1] - preProgress)
  end
  return value
end
ParkourBonusProgressPanel.OnCreate = OnCreate
ParkourBonusProgressPanel.OnDestroy = OnDestroy
ParkourBonusProgressPanel.OnEnable = OnEnable
ParkourBonusProgressPanel.OnDisable = OnDisable
ParkourBonusProgressPanel.ComponentDefine = ComponentDefine
ParkourBonusProgressPanel.ComponentDestroy = ComponentDestroy
ParkourBonusProgressPanel.DataDefine = DataDefine
ParkourBonusProgressPanel.DataDestroy = DataDestroy
ParkourBonusProgressPanel.OnAddListener = OnAddListener
ParkourBonusProgressPanel.OnRemoveListener = OnRemoveListener
ParkourBonusProgressPanel.Refresh = Refresh
ParkourBonusProgressPanel.SetData = SetData
ParkourBonusProgressPanel.GetScoreProgress = GetScoreProgress
return ParkourBonusProgressPanel
