local Component = require (GConst.TYPEOF_LUA_CLASS.LUA_COMPONENT)
local CurrencyBar = class("CurrencyBar", Component)

CurrencyBar.cellWidth = 162
CurrencyBar.cellHeight = 40

local OFFSET_X = -20
local OFFSET_Y = -40

function CurrencyBar:init()
	self.currVisible = true
	self.showType = nil
	self.sid = nil
	local uiMap = self.baseObject:genAllChildren()
	self.currencyList = {}
	for i = 1, 4 do
		self.currencyList[i] = uiMap["currency_bar.currency_bg.cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.CURRENCY_BAR_CELL)
		self.currencyList[i].idx = i
	end

	local titleBg = uiMap["currency_bar.currency_bg.title_bg"]
	self.currencyBg = uiMap["currency_bar.currency_bg"]

	self.currencyFlyImgList = {} -- 表现用飞行imgs

	-- 刘海屏
	local notchHeight = SafeAreaManager:getNotchScreenHeight()
	self.currencyBg:setAnchoredPositionY(-notchHeight)
	self.titleBg = titleBg
	self:setVisible(false)
end

function CurrencyBar:getFlyImgs()
	if self.currencyFlyImgList and #self.currencyFlyImgList > 0 then
		local obj = table.remove(self.currencyFlyImgList)
		return obj
	else
		local uiMap = self.baseObject:genAllChildren()
		local img = uiMap["currency_bar.fly_img"]
		local gameObject = CS.UnityEngine.Object.Instantiate(img.gameObject)
		local UIPrefabObject = require "app/bf/unity/uiprefab_object"
		local obj = UIPrefabObject:create()
		obj:initWithPrefab(nil, gameObject)
		obj:initPrefabHelper()
		obj:setParent(self.baseObject, false)
		return obj
	end
end

function CurrencyBar:putbackImg(img)
	if self.currencyFlyImgList then
		table.insert(self.currencyFlyImgList, img)
	end
end

--------- param {UIManager.CURRENCY_TYPE.XXX, ...} ----------------------
function CurrencyBar:show(params, showBg, hidAddImg)
	self.params = params or {}
	if not self.params.itemIds then
		return
	end
	self.offsetX = params.offsetX or 0
	self.offsetY = params.offsetY or 0
	self:setVisible(true)
	if showBg then
		self.titleBg:setActive(true)
	else
		self.titleBg:setActive(false)
	end
	for i, cell in ipairs(self.currencyList) do
		local itemId = self.params.itemIds[i]
		if itemId then
			cell:show(itemId, hidAddImg)
		else
			cell:hide()
		end
	end
	self:showSort()
	self:refreshTextRightNow()

	if not self.sid then
		self:updateTime()
		self.sid = self.baseObject:scheduleGlobal(function ()
			self:updateTime()
		end, 1)
	end
end

function CurrencyBar:updateTime()
	for i, cell in ipairs(self.currencyList) do
		cell:updateTime()
	end
end

function CurrencyBar:showSort()
	if self.showType and self.showType == self.params.showType then
		return
	end
	local w, h = GFunc.getUIExpandScreenSize()
	local offset = (w - 720) / 2
	if offset <= 0 then
		offset = 0
	end
	if self.params.showType == GConst.CURRENCY_TYPE.HORIZONTAL then
		for i,v in ipairs(self.params.itemIds) do
			self.currencyList[i]:setAnchoredPosition(OFFSET_X - (i - 1)*self.cellWidth + self.offsetX - offset, OFFSET_Y + self.offsetY)
		end
	else
		for i,v in ipairs(self.params.itemIds) do
			self.currencyList[i]:setAnchoredPosition(OFFSET_X + self.offsetX - offset, OFFSET_Y - (i - 1)*self.cellHeight + self.offsetY)
		end
	end
end

function CurrencyBar:setVisible(visible)
	if self.currVisible == visible then
		return
	end
	self.currVisible = visible
	if visible then
		self.baseObject:setVisible(true)
	else
		self.baseObject:setVisible(false)
	end
end

function CurrencyBar:refreshText()
	if self.currencyList == nil then
		return
	end
	for i, cell in ipairs(self.currencyList) do
		cell:refreshText()
	end
end

function CurrencyBar:refreshTextRightNow()
	if self.currencyList == nil then
		return
	end
	for i, cell in ipairs(self.currencyList) do
		cell:refreshTextRightNow()
	end
end

function CurrencyBar:refreshCurrencyAddTx(itemId, num)
	if self.currencyList == nil then
		return
	end
	for i, cell in ipairs(self.currencyList) do
		cell:refreshCurrencyAddTx(itemId, num)
	end
end

function CurrencyBar:showCurrencyAction(pos, imgNum)
	imgNum = imgNum or 3

	local rect = self.baseObject:getTransform().rect
	for i = 1, 3 do
		local id = self.params.itemIds[i]
		if id and pos[id] then
			for ii = 1, imgNum do
				local img = self:getFlyImgs()
				img:setSprite(GFunc.getIconRes(id))
				local endPos = self.currencyList[i]:getAnchoredPosition()
				local posX = -rect.width*0.5 + endPos.x
				local posY = rect.height*0.5 - 22 - 66
				GFunc.imgFly(img, pos[id][ii], {x = posX, y = posY}, function ()
					self.currencyList[i]:doScaleFlyImg()
					self:putbackImg(img)
					EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.CURRENCY_BAR_FLY_OVER)
				end)
			end
		end
	end
end

return CurrencyBar