local EquipGrowthUI = class("EquipGrowthUI", BaseUI)

local EQUIP_LIST_CELL = "app/ui/equip/cell/equip_list_cell"

local PAGE = {
	ENHANCE = 1,
	REFINE = 2,
}

function EquipGrowthUI:isFullScreen()
	return false
end

function EquipGrowthUI:getPrefabPath()
	return "assets/prefabs/ui/equip/equip_growth_ui.prefab"
end

function EquipGrowthUI:onPressBackspace()
	self:closeUI()
end

function EquipGrowthUI:ctor(parmas)
	self.slotId = parmas.slotId
	self.equipPart = parmas.part or 1
	self.page = PAGE.ENHANCE
	self:checkRedPage()
end

function EquipGrowthUI:checkRedPage()
	if DataManager.EquipData:hasEquipEnhanceRedPoint(self.slotId, self.equipPart) then
		self.page = PAGE.ENHANCE
	end
	if DataManager.EquipData:hasEquipRefineRedPoint(self.slotId, self.equipPart) then
		self.page = PAGE.REFINE
	end
end

function EquipGrowthUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()

	uiMap["equip_growth_ui.mask"]:addClickListener(function()
		self:closeUI()
	end)
	uiMap["equip_growth_ui.bg.close_btn"]:addClickListener(function()
		self:closeUI()
	end)
	-- uiMap["equip_growth_ui.bg.close_btn"]:addClickListener(function()
	-- 	self:closeUI()
	-- end)

	self.nameTx = uiMap["equip_growth_ui.bg.name_tx"]
	self.equipCell = uiMap["equip_growth_ui.bg.equip_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.EQUIP_CELL)

	-- 强化
	self.upCurrLvTx = uiMap["equip_growth_ui.bg.page_info_1.bg1.curr_lv_tx"]
	self.upNextLvTx = uiMap["equip_growth_ui.bg.page_info_1.bg2.next_lv_tx"]
	self.upDescTx = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.desc_tx"]
	self.upAttrNameTx = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.attr_name_tx"]
	self.upAttrCurrTx = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.attr_curr_tx"]
	self.upAttrNextTx = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.attr_next_tx"]
	-- self.upInfoBtn = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.bg.info_btn"]
	self.upDescTx1 = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.bg.desc_tx"]
	self.upCostBg = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.cost_bg"]
	self.upCostIcon = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.cost_bg.cost_icon"]
	self.upCostTx = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.cost_bg.cost_tx"]
	self.upUpBtn = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.up_btn"]
	-- self.upUpBtnTx = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.up_btn.text"]
	self.upAutoBtn = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.auto_btn"]
	-- self.upAutoBtnTx = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.auto_btn.text"]
	self.upMaxDescTx = uiMap["equip_growth_ui.bg.page_info_1.attr_bg.max_desc_tx"]
	self.upUpBtn:addClickListener(function()
		ModuleManager.EquipManager:onEquipUpgradeReq(self.slotId, self.equipPart)
	end)
	self.upAutoBtn:addClickListener(function()
		ModuleManager.EquipManager:onEquipUpgradeReq(self.slotId, 0)
	end)
	uiMap["equip_growth_ui.bg.page_info_1.attr_bg.bg.info_btn"]:addClickListener(function()
		ModuleManager.EquipManager:showEquipResonateUI(self.slotId, GConst.EquipConst.RESONATE_PAGE.LV_UP)
	end)
	uiMap["equip_growth_ui.bg.page_info_1.attr_bg.up_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_3))
	uiMap["equip_growth_ui.bg.page_info_1.attr_bg.auto_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_23))
	uiMap["equip_growth_ui.bg.page_info_2.attr_bg.up_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_20))
	uiMap["equip_growth_ui.bg.resolve_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_24))
	self.upMaxDescTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_27))

	-- 精炼
	-- self.refineNowBg = uiMap["equip_growth_ui.bg.page_info_2.now_bg"]
	self.refineCurrLvTx = uiMap["equip_growth_ui.bg.page_info_2.now_bg.lv_tx"]
	-- self.refineNextBg = uiMap["equip_growth_ui.bg.page_info_2.next_bg"]
	self.refineNextLvTx = uiMap["equip_growth_ui.bg.page_info_2.next_bg.lv_tx"]
	self.refineDescTx = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.desc_tx"]
	-- self.refineInfoBtn = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.bg.info_btn"]
	self.refineDescTx1 = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.bg.desc_tx"]
	self.refineCostBg = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.cost_bg"]
	self.refineCostIcon = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.cost_bg.cost_icon"]
	self.refineCostTx = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.cost_bg.cost_tx"]
	self.refineUpBtn = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.up_btn"]
	self.refineRateTx = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.rate_tx"]
	self.refineDescTx2 = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.desc_tx_1"]
	self.resolveBtn = uiMap["equip_growth_ui.bg.resolve_btn"]
	self.refineUpBtn:addClickListener(function()
		ModuleManager.EquipManager:onEquipRefineReq(self.slotId, self.equipPart)
	end)
	self.resolveBtn:addClickListener(function()
		ModuleManager.EquipManager:showEquipResolveUI()
	end)
	uiMap["equip_growth_ui.bg.page_info_2.attr_bg.bg.info_btn"]:addClickListener(function()
		ModuleManager.EquipManager:showEquipResonateUI(self.slotId, GConst.EquipConst.RESONATE_PAGE.REFINE)
	end)

	self.refineAttrCells = {}
	self.refineAttrNameTxs = {}
	self.refineAttrCurrTxs = {}
	self.refineAttrNextTxs = {}
	for i = 1, 4 do
		self.refineAttrCells[i] = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.list.attr_extra_cell_" .. i]
		self.refineAttrNameTxs[i] = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.list.attr_extra_cell_" .. i .. ".attr_name_tx"]
		self.refineAttrCurrTxs[i] = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.list.attr_extra_cell_" .. i .. ".attr_curr_tx"]
		self.refineAttrNextTxs[i] = uiMap["equip_growth_ui.bg.page_info_2.attr_bg.list.attr_extra_cell_" .. i .. ".attr_next_tx"]
	end

	self.equipCells = {}
	for i = 1, 6 do
		self.equipCells[i] = uiMap["equip_growth_ui.bg.equip_list.equip_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.EQUIP_CELL)
		self.equipCells[i]:addClickListener(function()
			local eid = DataManager.EquipData:getPartEquipUid(self.slotId, i)
			if not eid or eid <= 0 then
				return
			end
			if self.equipPart == i then
				return
			end
			self.equipPart = i
			self:checkRedPage()
			self:onRefresh()
		end)
	end

	self.btnTxs = {I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_19), I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_20)}
	self.pageInfos = {}
	self.pageBtns = {}
	self.pageBtnTxs = {}
	for i = 1, 2 do
		self.pageInfos[i] = uiMap["equip_growth_ui.bg.page_info_" .. i]
		self.pageBtns[i] = uiMap["equip_growth_ui.bg.page_btn_" .. i]
		self.pageBtnTxs[i] = uiMap["equip_growth_ui.bg.page_btn_" .. i .. ".text"]
		self.pageBtns[i] :addClickListener(function()
			if self.page == i then
				return
			end
			self.page = i
			self:onRefresh()
		end)
	end

	uiMap["equip_growth_ui.bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_18))

	self:bind(DataManager.HeroData, "isDirty", function()
		self:onRefresh()
	end)
	self:bind(DataManager.EquipData, "isDirtyRefineSuccess", function()
		self:onRefresh()
	end)
	self:bind(DataManager.EquipData, "isDirtyRefineFail", function()
		self:onRefresh()
	end)
end

function EquipGrowthUI:onRefresh()
	self:refreshEquipCell()
	self:refreshPageBtn()
	self:refreshPageInfo()
	self:refreshRedPoint()
end

function EquipGrowthUI:refreshEquipCell()
	local eid = DataManager.EquipData:getPartEquipUid(self.slotId, self.equipPart)
	if eid and eid > 0 then
		self.equipEntity = DataManager.EquipData:getEquipByUid(eid)
		self.equipCell:refresh(self.equipEntity, self.equipPart)
	else
		self.equipCell:refreshEmpty(self.equipPart)
	end

	for part = 1, 6 do
		local eid = DataManager.EquipData:getPartEquipUid(self.slotId, part)
		if eid and eid > 0 then
			local equipEntity = DataManager.EquipData:getEquipByUid(eid)
			self.equipCells[part]:refresh(equipEntity, self.slotId)
		else
			self.equipCells[part]:refreshEmpty(part)
		end
	end
end

function EquipGrowthUI:refreshPageBtn()
	for i = 1, 2 do
		if self.page == i then
			self.pageInfos[i]:setActive(true)
			self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.COMMON, "common_menu_1")
			self.pageBtnTxs[i]:setText(self.btnTxs[i])
		else
			self.pageInfos[i]:setActive(false)
			self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.COMMON, "common_menu_2")
			self.pageBtnTxs[i]:setText(self.btnTxs[i])
		end
	end
end

function EquipGrowthUI:refreshPageInfo()
	if self.page == 1 then
		self:refreshPageUp()
	else
		self:refreshPageRefine()
	end
end

--@region 强化
function EquipGrowthUI:refreshPageUp()
	local lv = DataManager.EquipData:getPartLv(self.slotId, self.equipPart)
	local eid = DataManager.EquipData:getPartEquipUid(self.slotId, self.equipPart)
	local equipEntity = DataManager.EquipData:getEquipByUid(eid)
	local isLvMax = equipEntity:getIsLvMax(self.slotId)
	-- if eid and eid > 0 then
	-- 	local equipEntity = DataManager.EquipData:getEquipByUid(eid)
	-- 	self.equipCell:refresh(equipEntity, self.equipPart)
	-- else
	-- 	self.equipCell:refreshEmpty(self.equipPart)
	-- end

	self.upCurrLvTx:setText(lv)
	self.upDescTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_14))
	local lv1 = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.LV_UP, self.slotId)
	self.upDescTx1:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_ACTIVATION_1, lv1))
	self.upMaxDescTx:setActive(isLvMax)

	local currAttr = equipEntity:getBaseAttrWithLv(lv)
	self.upAttrNameTx:setText(GFunc.getAttrNameByType(currAttr.type))
	local currAttrNum = GFunc.getFinalAttrValue(currAttr.type, currAttr.num, 2)
	self.upAttrCurrTx:setText(currAttrNum)
	if not isLvMax then
		self.upNextLvTx:setText(lv + 1)
		local nextAttr = equipEntity:getBaseAttrWithLv(lv + 1)
		local nextAttrNum = GFunc.getFinalAttrValue(nextAttr.type, nextAttr.num, 2)
		self.upAttrNextTx:setText(nextAttrNum)
		local itemCost = DataManager.EquipData:getLevelCost(lv + 1)
		local costId = GFunc.getRewardId(itemCost)
		local costNum = GFunc.getRewardNum(itemCost)
		local count = DataManager.BagData.ItemData:getItemNumById(costId)
		self.upCostIcon:setSprite(ModuleManager.ItemManager:getItemIcon(costId))
		if costNum > count then
			self.upCostTx:setText("<color=#049500>" .. count .. "</color>/"..costNum)
		else
			self.upCostTx:setText(count .. "/" .. costNum)
		end
	end
end
--@endregion

--@region 精炼
function EquipGrowthUI:refreshPageRefine()
	local refine = DataManager.EquipData:getPartRefine(self.slotId, self.equipPart)
	local isMaxRefine = DataManager.EquipData:isRefineLvMax(self.slotId, self.equipPart)
	local attrs = self.equipEntity:getExtraList()
	local count = #attrs
	for i, cell in ipairs(self.refineAttrCells) do
		if count >= i then
			cell:setActive(true)
			local extraAttr = attrs[i]
			local nowAttr = DataManager.EquipData:getRefineAttrAdd(refine, extraAttr.type)
			self.refineAttrNameTxs[i]:setText(GFunc.getAttrNameByType(extraAttr.type))
			self.refineAttrCurrTxs[i]:setText(GFunc.getFinalAttrValue(extraAttr.type, extraAttr.num + nowAttr.num, 2))
			if not isMaxRefine then
				local nextAttr = DataManager.EquipData:getRefineAttrAdd(refine + 1, extraAttr.type)
				self.refineAttrNextTxs[i]:setText(GFunc.getFinalAttrValue(nextAttr.type, extraAttr.num + nextAttr.num, 2))
				-- if isGrowthUp and self.effectNameResult  then
				-- 	self:playEffect(cell.txAttrBaseEffect)
				-- end
			end
		else
			cell:setActive(false)
		end
	end

	self.refineCurrLvTx:setText(refine)
	self.refineNextLvTx:setText(refine + 1)
	self.refineDescTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_12))
	local lv1 = DataManager.EquipData:getResonateLevel(GConst.EquipConst.RESONATE_PAGE.REFINE, self.slotId)
	self.refineDescTx1:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_ACTIVATION_2, lv1))
	if isMaxRefine then
		self.refineRateTx:setActive(false)
		self.refineUpBtn:setActive(false)
		self.refineCostBg:setActive(false)
		self.refineDescTx2:setActive(true)
		self.refineDescTx2:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_26))
	else
		local lv = DataManager.EquipData:getPartLv(self.slotId, self.equipPart)
		local needLevel = DataManager.EquipData:getRefineNeedLevel(refine + 1)
		self.refineDescTx2:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_34, needLevel))
		local showLimit = lv < needLevel
		self.refineDescTx2:setActive(showLimit)

		self.refineRateTx:setActive(not showLimit)
		self.refineUpBtn:setActive(not showLimit)
		self.refineCostBg:setActive(not showLimit)
		local pro = DataManager.EquipData:getPartRefineFailPro(self.slotId, self.equipPart)
		self.refineRateTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_HERO_DESC_25, pro))

		local itemCost = DataManager.EquipData:getRefineCost(refine + 1)
		local costId = GFunc.getRewardId(itemCost)
		local costNum = GFunc.getRewardNum(itemCost)
		local count = DataManager.BagData.ItemData:getItemNumById(costId)
		self.refineCostIcon:setSprite(ModuleManager.ItemManager:getItemIcon(costId))
		if costNum > count then
			self.refineCostTx:setText("<color=#049500>" .. count .. "</color>/".. costNum)
		else
			self.refineCostTx:setText(count .. "/" .. costNum)
		end
	end
end
--@endregion

--@region 红点
function EquipGrowthUI:refreshRedPoint()
	if DataManager.EquipData:hasEquipUnWearRedPoint() then
		self.resolveBtn:addRedPoint(85, 37, 1)
	else
		self.resolveBtn:removeRedPoint()
	end
	if DataManager.EquipData:hasEquipEnhanceRedPoint(self.slotId, self.equipPart) then
		self.upUpBtn:addRedPoint(102, 42, 1)
		self.pageBtns[1]:addRedPoint(106, 20, 1)
	else
		self.upUpBtn:removeRedPoint()
		self.pageBtns[1]:removeRedPoint()
	end
	if DataManager.EquipData:hasEquipRefineRedPoint(self.slotId, self.equipPart) then
		self.refineUpBtn:addRedPoint(102, 42, 1)
		self.pageBtns[2]:addRedPoint(106, 20, 1)
	else
		self.refineUpBtn:removeRedPoint()
		self.pageBtns[2]:removeRedPoint()
	end
end
--@endregion
return EquipGrowthUI