local DungeonDailyMainUI = class("DungeonDailyMainUI", BaseUI)

local DUNGEON_GOLD_COMP = "app/ui/dungeon/comp/dungeon_gold_comp"
local DUNGEON_MATERIAL_COMP = "app/ui/dungeon/comp/dungeon_material_comp"

function DungeonDailyMainUI:ctor(params)
	self.page = params and params.page
	if self.page == nil then
		if DataManager.DungeonDailyData.GoldData:isOpen() then
			self.page = GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD
		elseif DataManager.DungeonDailyData.MaterialData:isOpen() then
			self.page = GConst.DungeonConst.DUNGEON_DAILY_TYPE.MATERIAL
		end
	elseif self.page == GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD then
		if not DataManager.DungeonDailyData.GoldData:isOpen() then
			self.page = GConst.DungeonConst.DUNGEON_DAILY_TYPE.MATERIAL
		end
	elseif self.page == GConst.DungeonConst.DUNGEON_DAILY_TYPE.MATERIAL then
		if not DataManager.DungeonDailyData.MaterialData:isOpen() then
			self.page = GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD
		end
	end
	self.btns = {}
end

function DungeonDailyMainUI:getPrefabPath()
	return "assets/prefabs/ui/dungeon/dungeon_daily_main_ui.prefab"
end

function DungeonDailyMainUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.closeBtn = uiMap["dungeon_daily_main_ui.bottom.close_btn"]
	self.closeBtn:addClickListener(function()
		self:closeUI()
	end)

	self.dungeonGoldComp = uiMap["dungeon_daily_main_ui.gold_comp"]:addLuaComponent(DUNGEON_GOLD_COMP)
	self.dungeonMaterialComp = uiMap["dungeon_daily_main_ui.material_comp"]:addLuaComponent(DUNGEON_MATERIAL_COMP)

	self.goldBtn = uiMap["dungeon_daily_main_ui.bottom.gold_btn"]
	self.goldBtn:addClickListener(function()
		self:switchPage(GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD)
	end)
	uiMap["dungeon_daily_main_ui.bottom.gold_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_TITLE_8))
	self.materialBtn = uiMap["dungeon_daily_main_ui.bottom.material_btn"]
	self.materialBtn:addClickListener(function()
		self:switchPage(GConst.DungeonConst.DUNGEON_DAILY_TYPE.MATERIAL)
	end)
	uiMap["dungeon_daily_main_ui.bottom.material_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_TITLE_9))

	self:_bind()
end

function DungeonDailyMainUI:_bind()
	self:bind(DataManager.DungeonDailyData.GoldData, "isDirty", function()
		self:onRefresh()
	end)
	self:bind(DataManager.DungeonDailyData.MaterialData, "isDirty", function()
		self:onRefresh()
	end)
	self:bind(DataManager.PrivilegeCardData, "isDirty", function()
		self:onRefresh()
	end)
end

function DungeonDailyMainUI:onRefresh()
	local btnsCount = #self.btns
	for i = 1, btnsCount do
		table.remove(self.btns)
	end
	if DataManager.DungeonDailyData.MaterialData:isOpen() then
		self.materialBtn:setActive(true)
		table.insert(self.btns, self.materialBtn)
		if DataManager.DungeonDailyData.MaterialData:getIsShowRedPoint() then
			self.materialBtn:addRedPoint(30, 30, 1)
		else
			self.materialBtn:removeRedPoint()
		end
	else
		self.materialBtn:setActive(false)
	end
	if DataManager.DungeonDailyData.GoldData:isOpen() then
		self.goldBtn:setActive(true)
		table.insert(self.btns, self.goldBtn)
		if DataManager.DungeonDailyData.GoldData:getIsShowRedPoint() then
			self.goldBtn:addRedPoint(30, 30, 1)
		else
			self.goldBtn:removeRedPoint()
		end
	else
		self.goldBtn:setActive(false)
	end
	for k, v in ipairs(self.btns) do
		v:setAnchoredPositionX(-76 - (k-1)*123)
	end
	self:switchPage(self.page)
end

function DungeonDailyMainUI:switchPage(page)
	self.page = page
	if self.page == GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD then
		self.dungeonGoldComp:getBaseObject():setActive(true)
		self.dungeonMaterialComp:getBaseObject():setActive(false)
		self.goldBtn:setSprite(GConst.ATLAS_PATH.UI_ACT_COMMON, "act_common_btn_47_1")
		self.materialBtn:setSprite(GConst.ATLAS_PATH.UI_ACT_COMMON, "act_common_btn_48_2")
		self:refreshGold()
	elseif self.page == GConst.DungeonConst.DUNGEON_DAILY_TYPE.MATERIAL then
		self.dungeonGoldComp:getBaseObject():setActive(false)
		self.dungeonMaterialComp:getBaseObject():setActive(true)
		self.goldBtn:setSprite(GConst.ATLAS_PATH.UI_ACT_COMMON, "act_common_btn_47_2")
		self.materialBtn:setSprite(GConst.ATLAS_PATH.UI_ACT_COMMON, "act_common_btn_48_1")
		self:refreshMaterial()
	end
end

function DungeonDailyMainUI:refreshGold()
	if self._alreadyEnterGold == nil then
		self._alreadyEnterGold = true
		self.dungeonGoldComp:onRefresh(true)
	else
		self.dungeonGoldComp:onRefresh()
	end
end

function DungeonDailyMainUI:refreshMaterial()
	if self._alreadyEnterMaterial == nil then
		self._alreadyEnterMaterial = true
		self.dungeonMaterialComp:onRefresh(true)
	else
		self.dungeonMaterialComp:onRefresh(false)
	end
end

return DungeonDailyMainUI
