local ShopComp = class("ShopComp", LuaComponent)

local TOP_HEIGHT = 136
local BOTTOM_HEIGHT = 218
local TITLE_HEIGHT = 54
local PAGE_DISCOUNT = 1
local PAGE_MAIN = 2
local TITLE_POS = -100
local PAGE_MAIN_BOX_SELL_CELL = "app/ui/shop/cell/box_sell_cell"
local PAGE_MAIN_HOT_SELL_CELL = "app/ui/shop/cell/hot_sell_cell"
local PAGE_MAIN_GEM_SELL_CELL = "app/ui/shop/cell/gem_sell_cell"
local PAGE_MAIN_GOLD_SELL_CELL = "app/ui/shop/cell/gold_sell_cell"

local PAGE_DISCOUNT_CHAPTER_SELL_CELL = "app/ui/shop/cell/chapter_sell_cell" -- 章节
local PAGE_DISCOUNT_BEGINNER_SELL_CELL = "app/ui/shop/cell/beginner_sell_cell" -- 新手
local PAGE_DISCOUNT_LEVEL_SELL_CELL = "app/ui/shop/cell/level_sell_cell" -- 助力
local PAGE_DISCOUNT_GROW_SELL_CELL = "app/ui/shop/cell/grow_sell_cell" -- 成长
local PAGE_DISCOUNT_COIN_SELL_CELL = "app/ui/shop/cell/coin_sell_cell" -- 金币
local PAGE_DISCOUNT_ARENA_SELL_CELL = "app/ui/shop/cell/arena_gift_sell_cell" -- 竞技场
local PAGE_DISCOUNT_WEAPON_SELL_CELL = "app/ui/shop/cell/weapon_gift_sell_cell" -- 武器
local PAGE_DISCOUNT_ARMOR_SELL_CELL = "app/ui/shop/cell/armor_gift_sell_cell" -- 防具

function ShopComp:init()
	self.uiMap = self.baseObject:genAllChildren()

	self.page = PAGE_MAIN -- 默认展示主要商品
	self.post = false

	self:initTitlePage()
	self:initMainPage()
	self:initDiscountPage()
	self:switchPage(self.page)
end

function ShopComp:initTitlePage()
	self.titleNode = self.uiMap["shop_comp.title_node"]

	self.subTitleText1 = self.uiMap["shop_comp.title_node.btn_cell_1.text"]
	self.subTitleSelectText1 = self.uiMap["shop_comp.title_node.btn_cell_1.select_text"]
	self.subTitleIcon1 = self.uiMap["shop_comp.title_node.btn_cell_1.icon"]
	self.subTitleBtn1 = self.uiMap["shop_comp.title_node.btn_cell_1"]

	self.subTitleText2 = self.uiMap["shop_comp.title_node.btn_cell_2.text"]
	self.subTitleSelectText2 = self.uiMap["shop_comp.title_node.btn_cell_2.select_text"]
	self.subTitleIcon2 = self.uiMap["shop_comp.title_node.btn_cell_2.icon"]
	self.subTitleBtn2 = self.uiMap["shop_comp.title_node.btn_cell_2"]

	self.subTitleText1:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_16)) -- 特惠商品
	self.subTitleSelectText1:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_16)) -- 特惠商品
	self.subTitleText2:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_17)) -- 主要商品
	self.subTitleSelectText2:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_17)) -- 主要商品

	self.subTitleBtn1:addClickListener(function()
		self:switchPage(PAGE_DISCOUNT)
	end)
	self.subTitleBtn2:addClickListener(function()
		self:switchPage(PAGE_MAIN)
	end)
	self:refreshTitle()

	local safeHeight = SafeAreaManager:getNotchScreenHeight()
	self.titleNode:setAnchoredPositionY(TITLE_POS - safeHeight)
	if GFunc.isShenhe() then
		self.subTitleBtn2:setAnchoredPositionX(0)
		self.subTitleBtn1:setVisible(false)
	end
end

function ShopComp:refreshTitle()
	if self.page == PAGE_DISCOUNT then
		self.subTitleText1:setAnchoredPositionY(GConst.NOT_VISIBLE_POS)
		self.subTitleSelectText1:setAnchoredPositionY(0)
		self.subTitleIcon1:setAnchoredPositionY(0)

		self.subTitleText2:setAnchoredPositionY(0)
		self.subTitleSelectText2:setAnchoredPositionY(GConst.NOT_VISIBLE_POS)
		self.subTitleIcon2:setAnchoredPositionY(GConst.NOT_VISIBLE_POS)
	else
		self.subTitleText1:setAnchoredPositionY(0)
		self.subTitleSelectText1:setAnchoredPositionY(GConst.NOT_VISIBLE_POS)
		self.subTitleIcon1:setAnchoredPositionY(GConst.NOT_VISIBLE_POS)

		self.subTitleText2:setAnchoredPositionY(GConst.NOT_VISIBLE_POS)
		self.subTitleSelectText2:setAnchoredPositionY(0)
		self.subTitleIcon2:setAnchoredPositionY(0)
	end
end

function ShopComp:switchPage(page)
	if page == PAGE_DISCOUNT and GFunc.isShenhe() then
		page = PAGE_MAIN
	end
	if self.page ~= page then
		self.page = page
		self.post = false
		self:refresh()
	end
end

function ShopComp:goSubType(subType)
	local scrollRectObj = self.uiMap["shop_comp.main.scrollrect"] 
	local contentObj = self.uiMap["shop_comp.main.scrollrect.viewport.content"]
	local boxHeight = 0
	if self.boxSellCell and self.boxSellCell:getIsOpen() then
		boxHeight = self.boxSellCell:getCellHeight() 
	end
	local hotHeight = 0
	if self.hotSellCell and self.hotSellCell:getIsOpen() then
		hotHeight = self.hotSellCell:getCellHeight() 
	end
	local gemHeight = 0
	if self.gemSellCell and self.gemSellCell:getIsOpen() then
		gemHeight = self.gemSellCell:getCellHeight() 
	end
	local goldHeight = 0
	if self.goldSellCell and self.goldSellCell:getIsOpen() then
		goldHeight = self.goldSellCell:getCellHeight() 
	end
	local maxHeight = math.min(boxHeight + hotHeight + gemHeight + goldHeight - scrollRectObj:fastGetSizeDeltaY())
	local height = 0
	if subType == GConst.ShopConst.MAIN_PAGE_TYPE.BOX then
		-- 默认在开头
	elseif subType == GConst.ShopConst.MAIN_PAGE_TYPE.HOT then
		height = height + boxHeight
	elseif subType == GConst.ShopConst.MAIN_PAGE_TYPE.GEM then
		height = height + boxHeight + hotHeight
	elseif subType == GConst.ShopConst.MAIN_PAGE_TYPE.GOLD then
		height = height + boxHeight + hotHeight + gemHeight
	end
	contentObj:setAnchoredPositionY(math.min(height, maxHeight))
end

function ShopComp:initMainPage()
	self.mainNode = self.uiMap["shop_comp.main"]
	local scrollrect = self.uiMap["shop_comp.main.scrollrect"]
	local height = self.baseObject:getTransform().rect.height
	height = height - TITLE_HEIGHT - TOP_HEIGHT - BOTTOM_HEIGHT
	local safeHeight = SafeAreaManager:getNotchScreenHeight()
	height = height - safeHeight
	scrollrect:setSizeDeltaY(height)
	self.mainScrollContent = self.uiMap["shop_comp.main.scrollrect.viewport.content"]
	self.boxSellCell = self.uiMap["shop_comp.main.scrollrect.viewport.content.box_sell_cell"]:addLuaComponent(PAGE_MAIN_BOX_SELL_CELL)
	self.hotSellCell = self.uiMap["shop_comp.main.scrollrect.viewport.content.hot_sell_cell"]:addLuaComponent(PAGE_MAIN_HOT_SELL_CELL)
	self.gemSellCell = self.uiMap["shop_comp.main.scrollrect.viewport.content.gem_sell_cell"]:addLuaComponent(PAGE_MAIN_GEM_SELL_CELL)
	self.goldSellCell = self.uiMap["shop_comp.main.scrollrect.viewport.content.gold_sell_cell"]:addLuaComponent(PAGE_MAIN_GOLD_SELL_CELL)
end

function ShopComp:initDiscountPage()
	self.discountNode = self.uiMap["shop_comp.discount"]
	local scrollrect = self.uiMap["shop_comp.discount.scrollrect"]
	local height = self.baseObject:getTransform().rect.height
	height = height - TITLE_HEIGHT - TOP_HEIGHT - BOTTOM_HEIGHT
	local safeHeight = SafeAreaManager:getNotchScreenHeight()
	height = height - safeHeight
	scrollrect:setSizeDeltaY(height)
	self.discountContent = self.uiMap["shop_comp.discount.scrollrect.viewport.content"]
	self.chapterSellCell = self.uiMap["shop_comp.discount.scrollrect.viewport.content.chapter_sell_cell"]:addLuaComponent(PAGE_DISCOUNT_CHAPTER_SELL_CELL)
	self.beginnerSellCell = self.uiMap["shop_comp.discount.scrollrect.viewport.content.beginner_sell_cell"]:addLuaComponent(PAGE_DISCOUNT_BEGINNER_SELL_CELL)
	self.levelSellCell = self.uiMap["shop_comp.discount.scrollrect.viewport.content.level_sell_cell"]:addLuaComponent(PAGE_DISCOUNT_LEVEL_SELL_CELL)
	self.growSellCell = self.uiMap["shop_comp.discount.scrollrect.viewport.content.grow_sell_cell"]:addLuaComponent(PAGE_DISCOUNT_GROW_SELL_CELL)
	self.coinSellCell = self.uiMap["shop_comp.discount.scrollrect.viewport.content.coin_sell_cell"]:addLuaComponent(PAGE_DISCOUNT_COIN_SELL_CELL)
	self.arenaSellCell = self.uiMap["shop_comp.discount.scrollrect.viewport.content.arena_gift_sell_cell"]:addLuaComponent(PAGE_DISCOUNT_ARENA_SELL_CELL)
	self.weponSellCell = self.uiMap["shop_comp.discount.scrollrect.viewport.content.weapon_gift_sell_cell"]:addLuaComponent(PAGE_DISCOUNT_WEAPON_SELL_CELL)
	self.armorSellCell = self.uiMap["shop_comp.discount.scrollrect.viewport.content.armor_gift_sell_cell"]:addLuaComponent(PAGE_DISCOUNT_ARMOR_SELL_CELL)
end

function ShopComp:refresh()
	if self.page == PAGE_DISCOUNT then
		self.mainNode:setAnchoredPositionX(GConst.NOT_VISIBLE_POS)
		self.discountNode:setAnchoredPositionX(0)
		DataManager.ShopData:markShopDiscountRedPoint()
		self.subTitleBtn1:removeRedPoint()
		if DataManager.ShopData:getRp() then
			self.subTitleBtn2:addRedPoint(84, 24)
		else
			self.subTitleBtn2:removeRedPoint()
		end
	else
		self.mainNode:setAnchoredPositionX(0)
		self.discountNode:setAnchoredPositionX(GConst.NOT_VISIBLE_POS)
		self.subTitleBtn2:removeRedPoint()
		if DataManager.ShopData:getShopDiscountRedPoint() then
			self.subTitleBtn1:addRedPoint(-84, 24)
		else
			self.subTitleBtn1:removeRedPoint()
		end
	end


	self:refreshTitle()
	self:refreshDiscountPage()
	self:refreshMainPage()

	-- 如果已经跨天了 需要重新请求每日礼包数据
	if DataManager.ShopData:getMallDailyDirty() then
		DataManager.ShopData:markMallDailyDirty(false)
		ModuleManager.ShopManager:mallDailyGiftOverDay()
	end

	self:refreshTime()

	-- 上报
	self:postEnterPage()
end

function ShopComp:postEnterPage()
	if self.post then
		return
	end
	self.post = true

	if self.page == PAGE_DISCOUNT then
		-- 找到所有的项目
		-- 章节
		local actIdList = DataManager.ShopData:getActChapterStoreCanBuyActIds()
		if actIdList and #actIdList > 0 then
			BIReport:postPayUIShow(BIReport.GIFT_TYPE.ACT_CHAPTER_STORE, actIdList[1], BIReport.PAY_UI_SHOW_TYPE.SHOP_SHOW) -- 约定上报第一个付费项
		end
		-- 新手
		if not DataManager.ShopData:getBeginnerGiftHasBuy() then
			BIReport:postPayUIShow(BIReport.GIFT_TYPE.BEGINNER_GIFT, GConst.ShopConst.BEGINNER_GIFT_ID, BIReport.PAY_UI_SHOW_TYPE.SHOP_SHOW)
		end
		-- 助力
		local actIdList = DataManager.ShopData:getLevelUpGiftActIds()
		if actIdList and #actIdList > 0 then
			for _, id in ipairs(actIdList) do
				BIReport:postPayUIShow(BIReport.GIFT_TYPE.LEVEL_UP_GIFT, id, BIReport.PAY_UI_SHOW_TYPE.SHOP_SHOW)
			end
		end 
		-- 成长
		local act = DataManager.ShopData:getGrowUpGift()
		if act then
			BIReport:postPayUIShow(BIReport.GIFT_TYPE.GROW_UP_GIFT_NEW, act.current_grow_up_id, BIReport.PAY_UI_SHOW_TYPE.SHOP_SHOW)
		end
		-- 金币
		local actId = DataManager.ShopData:getValidCoinGiftId()
		if actId and actId > 0 then
			BIReport:postPayUIShow(BIReport.GIFT_TYPE.COIN_GIFT, actId, BIReport.PAY_UI_SHOW_TYPE.SHOP_SHOW)
		end
		-- 竞技场
		local actId = DataManager.ArenaData:getGiftId()
		if actId and actId > 0 then
			BIReport:postPayUIShow(BIReport.GIFT_TYPE.ARENA_GIFT, actId, BIReport.PAY_UI_SHOW_TYPE.SHOP_SHOW)
		end
		-- 武器副本
		local actIdInfo = DataManager.ShopData:getGift(PayManager.PURCHARSE_ACT_TYPE.WEAPON_GIFT)
		if actIdInfo then
			local actGiftId = actIdInfo.id
			if actGiftId and actGiftId > 0 then
				BIReport:postPayUIShow(BIReport.GIFT_TYPE.WEAPON_GIFT, actGiftId, BIReport.PAY_UI_SHOW_TYPE.SHOP_SHOW)
			end
		end
		-- 防具副本
		local actIdInfo = DataManager.ShopData:getGift(PayManager.PURCHARSE_ACT_TYPE.ARMOR_GIFT)
		if actIdInfo then
			local actGiftId = actIdInfo.id
			if actGiftId and actGiftId > 0 then
				BIReport:postPayUIShow(BIReport.GIFT_TYPE.ARMOR_GIFT, actGiftId, BIReport.PAY_UI_SHOW_TYPE.SHOP_SHOW)
			end
		end
	else
		-- 找到所有的钻石项目
		BIReport:postPayUIShow(BIReport.GIFT_TYPE.MALL_TREASURE, 2, BIReport.PAY_UI_SHOW_TYPE.SHOP_SHOW) -- 约定上报第一个付费项
	end
end

function ShopComp:clearPostFlag()
	self.post = false
end

function ShopComp:refreshTime()
	if self.hotSellCell and self.hotSellCell:getIsOpen() then
		self.hotSellCell:refreshTime()
	end
	local coinCellCount = 0
	if self.coinSellCell and self.coinSellCell:getIsOpen() then
		coinCellCount = self.coinSellCell:getCellCount()
		self.coinSellCell:refreshTime()
	end
	if self.coinCellCount ~= coinCellCount then
		self.coinCellCount = coinCellCount
		DataManager.ShopData:setDirty()
	end
	local growCellCount = 0
	if self.growSellCell and self.growSellCell:getIsOpen() then
		growCellCount = self.growSellCell:getCellCount()
		self.growSellCell:refreshTime()
	end
	if self.growCellCount ~= growCellCount then
		self.growCellCount = growCellCount
		DataManager.ShopData:setDirty()
	end
	local arenaGiftOpen = false
	if self.arenaSellCell and self.arenaSellCell:getIsOpen() then
		arenaGiftOpen = true
		self.arenaSellCell:refreshTime()
	end
	if self.arenaGiftOpen ~= arenaGiftOpen then
		self.arenaGiftOpen = arenaGiftOpen
		DataManager.ShopData:setDirty()
	end
	local weponGiftOpen = false
	if self.weponSellCell and self.weponSellCell:getIsOpen() then
		weponGiftOpen = true
		self.weponSellCell:refreshTime()
	end
	if self.weponGiftOpen ~= weponGiftOpen then
		self.weponGiftOpen = weponGiftOpen
		DataManager.ShopData:setDirty()
	end
	local armorGiftOpen = false
	if self.armorSellCell and self.armorSellCell:getIsOpen() then
		armorGiftOpen = true
		self.armorSellCell:refreshTime()
	end
	if self.armorGiftOpen ~= armorGiftOpen then
		self.armorGiftOpen = armorGiftOpen
		DataManager.ShopData:setDirty()
	end
end

function ShopComp:refreshDiscountPage()
	-- 同SideBarDiscountCell:getIsOpen()一起修改
	local y = 0
	local cellHeight = 0
	-- 章节
	if self.chapterSellCell:getIsOpen() then
		self.chapterSellCell:setVisible(true)
		self.chapterSellCell:refresh()
		self.chapterSellCell:getBaseObject():setAnchoredPositionY(0)
		cellHeight = self.chapterSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.chapterSellCell:setVisible(false)
	end
	-- 新手
	if self.beginnerSellCell:getIsOpen() then
		self.beginnerSellCell:setVisible(true)
		self.beginnerSellCell:refresh()
		self.beginnerSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.beginnerSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.beginnerSellCell:setVisible(false)
	end
	-- 竞技场
	if self.arenaSellCell:getIsOpen() then
		self.arenaSellCell:setVisible(true)
		self.arenaSellCell:refresh()
		self.arenaSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.arenaSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.arenaSellCell:setVisible(false)
	end
	-- 武器
	if self.weponSellCell:getIsOpen() then
		self.weponSellCell:setVisible(true)
		self.weponSellCell:refresh()
		self.weponSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.weponSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.weponSellCell:setVisible(false)
	end
	-- 防具
	if self.armorSellCell:getIsOpen() then
		self.armorSellCell:setVisible(true)
		self.armorSellCell:refresh()
		self.armorSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.armorSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.armorSellCell:setVisible(false)
	end
	-- 助力
	if self.levelSellCell:getIsOpen() then
		self.levelSellCell:setVisible(true)
		self.levelSellCell:refresh()
		self.levelSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.levelSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.levelSellCell:setVisible(false)
	end
	-- 成长
	if self.growSellCell:getIsOpen() then
		self.growSellCell:setVisible(true)
		self.growSellCell:refresh()
		self.growSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.growSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.growSellCell:setVisible(false)
	end
	-- 金币
	if self.coinSellCell:getIsOpen() then
		self.coinSellCell:setVisible(true)
		self.coinSellCell:refresh()
		self.coinSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.coinSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.coinSellCell:setVisible(false)
	end
	self.discountContent:setSizeDeltaY(y)
end

function ShopComp:refreshMainPage()
	local y = 0
	local cellHeight = 0
	-- 宝箱
	if self.boxSellCell:getIsOpen() then
		self.boxSellCell:setVisible(true)
		self.boxSellCell:refresh()
		self.boxSellCell:getBaseObject():setAnchoredPositionY(0)
		cellHeight = self.boxSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.boxSellCell:setVisible(false)
	end
	-- 每日特惠
	if self.hotSellCell:getIsOpen() then
		self.hotSellCell:setVisible(true)
		self.hotSellCell:refresh()
		self.hotSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.hotSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.hotSellCell:setVisible(false)
	end
	-- 钻石
	if self.gemSellCell:getIsOpen() then
		self.gemSellCell:setVisible(true)
		self.gemSellCell:refresh()
		self.gemSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.gemSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.gemSellCell:setVisible(false)
	end
	-- 金币
	if self.goldSellCell:getIsOpen() then
		self.goldSellCell:setVisible(true)
		self.goldSellCell:refresh()
		self.goldSellCell:getBaseObject():setAnchoredPositionY(-y)
		cellHeight = self.goldSellCell:getCellHeight()
		y = y + cellHeight
	else
		self.goldSellCell:setVisible(false)
	end
	self.mainScrollContent:setSizeDeltaY(y)
end

return ShopComp