local ShopComp = class("ShopComp", LuaComponent)

local CHAPTER_CELL = "app/ui/shop/cell/chapter_cell"
local DAILY_MAIN_CELL = "app/ui/shop/cell/daily_main_cell"
local GEM_MAIN_CELL = "app/ui/shop/cell/gem_main_cell"
local GOLD_MAIN_CELL = "app/ui/shop/cell/gold_main_cell"

function ShopComp:init()
	local uiMap = self.baseObject:genAllChildren()
	self.bg = uiMap["shop_comp.bg"]
	self.scrollrectNode = uiMap["shop_comp.bg.scrollrect"]
	self.chapterTitleBg = uiMap["shop_comp.bg.scrollrect.viewport.content.chapter_title_bg"]
	self.dailyTitleBg = uiMap["shop_comp.bg.scrollrect.viewport.content.daily_title_bg"]
	self.gemTitleBg = uiMap["shop_comp.bg.scrollrect.viewport.content.gem_title_bg"]
	self.goldTitleBg = uiMap["shop_comp.bg.scrollrect.viewport.content.gold_title_bg"]
	self.goldCd = uiMap["shop_comp.bg.scrollrect.viewport.content.gold_title_bg.cd"]
	self.content = uiMap["shop_comp.bg.scrollrect.viewport.content"]
	self.chapterContent = uiMap["shop_comp.bg.scrollrect.viewport.content.chapter_scrollrect.viewport.content"]

	uiMap["shop_comp.bg.scrollrect.viewport.content.chapter_title_bg.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_10))
	uiMap["shop_comp.bg.scrollrect.viewport.content.daily_title_bg.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_28))
	uiMap["shop_comp.bg.scrollrect.viewport.content.gem_title_bg.title"]:setText(ModuleManager.ItemManager:getItemName(GConst.ItemConst.ITEM_ID_GEM))
	uiMap["shop_comp.bg.scrollrect.viewport.content.gold_title_bg.title"]:setText(ModuleManager.ItemManager:getItemName(GConst.ItemConst.ITEM_ID_GOLD))

	self.chapterScrollrect = uiMap["shop_comp.bg.scrollrect.viewport.content.chapter_scrollrect"]
	self.dailyCell = uiMap["shop_comp.bg.scrollrect.viewport.content.daily_cell"]
	self.gemCell = uiMap["shop_comp.bg.scrollrect.viewport.content.gem_cell"]
	self.goldCell = uiMap["shop_comp.bg.scrollrect.viewport.content.gold_cell"]

	local scrollrect = uiMap["shop_comp.bg.scrollrect.viewport.content.chapter_scrollrect"]
	self.chapterScrollrectComp = scrollrect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.chapterScrollrectComp:addInitCallback(function()
		return CHAPTER_CELL
	end)
	self.chapterScrollrectComp:addRefreshCallback(function(idx, cell)
		if self.chapterList[idx] then
			cell:refresh(self.chapterList[idx], idx, #self.chapterList, function (add)
				self.chapterContent:addAnchoredPosition(add * 670)
			end)
		end
	end)
	self.chapterScrollrectComp:clearCells()

	self.dailyCells = {}
	for i = 1, 3 do
		self.dailyCells[i] = uiMap["shop_comp.bg.scrollrect.viewport.content.new_cell"]
	end

	self:bind(DataManager.ShopData, "isDirty", function()
		self:refresh(nil, true)
	end)
	-- self:bind(DataManager.HeroData, "isDirty", function()
	-- 	self:refresh(nil, true)
	-- end)

	-- self:bind(DataManager.ForceEquipData, "isDirty", function()
	-- 	self:refresh(nil, true)
	-- end)
	-- self:bind(DataManager.CollectionData, "isDirty", function()
	-- 	self:refresh(nil, true)
	-- end)
	-- self:bind(DataManager.BagData.ItemData, "isDirty", function(binder, value)
	-- 	self:refresh(nil, true)
	-- end)
	self:bind(DataManager.PaymentData, "isDirty", function()
		self:refresh(nil, true)
	end)

	-- local rect = self.baseObject:getRectSize()
	-- local height = rect.height - 390
	-- self.bgHeight = math.max(height, 1015)
	-- self.bg:setSizeDeltaY(self.bgHeight)
end

function ShopComp:refresh(showIdx, noShowIdx)
	self:refreshShop(showIdx, noShowIdx)
end

function ShopComp:updateTime()
end

function ShopComp:refreshShop(showIdx, noShowIdx)
	self.showIdx = showIdx or self.showIdx
	self:updateList(noShowIdx)

	if self.showIdx then -- 指定跳转
		if self.showIdx == GConst.ShopConst.MAIN_PAGE_TYPE.CHAPTER_GIFT then
			BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.SHOP_CHAPTER)
		elseif self.showIdx == GConst.ShopConst.MAIN_PAGE_TYPE.DAILY_STORE then
			BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.SHOP_DAILY_STORE)
		elseif self.showIdx == GConst.ShopConst.MAIN_PAGE_TYPE.GEM_STORE then
			BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.SHOP_GEM_STORE)
		elseif self.showIdx == GConst.ShopConst.MAIN_PAGE_TYPE.GOLD_STORE then
			BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.SHOP_GOLD_STORE)
		else
			BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.MAIN_SHOP)
		end
	else
		BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.MAIN_SHOP)
	end
	self.showIdx = nil
end

function ShopComp:updateList(noShowIdx)
	local height = -20
	height = self:updateChapterList(height)
	height = self:updateDailyCell(height)
	height = self:updateGemCell(height)
	height = self:updateGoldCell(height)
	height = height - 160

	self.content:setSizeDeltaY(math.abs(height))

	local scrollHeight = self.scrollrectNode:getRectHeight()
	local maxPosY = math.abs(height) - scrollHeight

	if not noShowIdx and self.beginPosY then
		self.content:setAnchoredPositionY(math.min(maxPosY, self.beginPosY))
	elseif not noShowIdx and self.rpPosY then
		self.content:setAnchoredPositionY(math.min(maxPosY, self.rpPosY - 80))
	end
	self.beginPosY = nil
	self.rpPosY = nil
end

function ShopComp:updateChapterList(offsetY)
	-- 没有新手礼包
	if CS.BF.BFMain.IsShenhe then
		self.chapterTitleBg:setActive(false)
		self.chapterScrollrect:setActive(false)
		self.chapterScrollrect:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SCROLL_RECT).enabled = false
		return offsetY
	end

	self.chapterList = DataManager.GiftPopData:getPopGiftListByGiftType(PayManager.PURCHARSE_ACT_TYPE.GIFT_POP_CHAPTER)
	if #self.chapterList <= 0 then
		self.chapterTitleBg:setActive(false)
		self.chapterScrollrect:setActive(false)
		self.chapterScrollrect:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SCROLL_RECT).enabled = false
		return offsetY
	else
		self.chapterTitleBg:setActive(true)
		self.chapterScrollrect:setActive(true)
		self.chapterScrollrect:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SCROLL_RECT).enabled = true
	end

	if self.showIdx == GConst.ShopConst.MAIN_PAGE_TYPE.CHAPTER_GIFT then
		self.beginPosY = math.abs(offsetY)
	end

	self.chapterTitleBg:setAnchoredPositionY(offsetY)
	offsetY = offsetY -75
	self.chapterScrollrect:setAnchoredPositionY(offsetY)
	offsetY = offsetY - 390

	local currCount = self.chapterScrollrectComp:getTotalCount()
	self.chapterScrollrectComp:refillCells(#self.chapterList)
	if currCount == nil or currCount <= 0 then
		self.chapterContent:setAnchoredPositionX(-(#self.chapterList-1)*670)
	end
	return offsetY
end

function ShopComp:updateDailyCell(offsetY)
	self.dailyCell:setActive(false)
	self.dailyTitleBg:setActive(false)
	if not DataManager.ShopData:isDailyOpen() then
		return offsetY
	end
	self.dailyTitleBg:setActive(true)
	self.dailyCell:setActive(true)
	if self.showIdx == GConst.ShopConst.MAIN_PAGE_TYPE.DAILY_STORE then
		self.beginPosY = math.abs(offsetY)
	end
	if self.rpPosY == nil then
		self.rpPosY = DataManager.ShopData:hasDailyRedPoint() and math.abs(offsetY) or nil
	end

	self.dailyTitleBg:setAnchoredPositionY(offsetY)
	offsetY = offsetY - 96
	self.dailyCell:setAnchoredPositionY(offsetY)
	offsetY = offsetY - 670

	local cell = self.dailyCell:addLuaComponent(DAILY_MAIN_CELL)
	cell:refresh()

	return offsetY
end

function ShopComp:updateGemCell(offsetY)
	if self.showIdx == GConst.ShopConst.MAIN_PAGE_TYPE.GEM_STORE then
		self.beginPosY = math.abs(offsetY)
	end

	self.gemTitleBg:setAnchoredPositionY(offsetY)
	offsetY = offsetY - 70
	self.gemCell:setAnchoredPositionY(offsetY)
	if CS.BF.BFMain.IsShenhe then
		offsetY = offsetY - 1762
	else
		offsetY = offsetY - 615
	end
	local cell = self.gemCell:addLuaComponent(GEM_MAIN_CELL)
	cell:refresh()

	return offsetY
end

function ShopComp:updateGoldCell(offsetY)
	self.goldTitleBg:setActive(true)
	self.goldCell:setActive(true)
	if self.showIdx == GConst.ShopConst.MAIN_PAGE_TYPE.GOLD_STORE then
		self.beginPosY = math.abs(offsetY)
	end
	if self.rpPosY == nil then
		self.rpPosY = DataManager.ShopData:hasGoldRedPoint(true) and math.abs(offsetY) or nil
	end

	self.goldTitleBg:setAnchoredPositionY(offsetY)
	offsetY = offsetY - 70
	self.goldCell:setAnchoredPositionY(offsetY)
	offsetY = offsetY - 322

	local cell = self.goldCell:addLuaComponent(GOLD_MAIN_CELL)
	cell:refresh()

	return offsetY
end

return ShopComp