local BaseTips = require "app/ui/tips/base_tips"
local RewardsTips = class("RewardsTips", BaseTips)

function RewardsTips:ctor(params)
	self.params = params
    self.tarCornerScreenPos = params.tarCornerScreenPos
    self.location = params.location
    self.callback = params.callback
end

function RewardsTips:getPrefabPath()
	return "assets/prefabs/ui/tips/rewards_tips.prefab"
end

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

function RewardsTips:init()
	local uiMap = self.root:genAllChildren()
    self.bg = uiMap["rewards_tips.bg"]
    self.bgBlack = uiMap["rewards_tips.bg_black"]

    uiMap["rewards_tips.bg.reward_node"]:setActive(false)
    uiMap["rewards_tips.bg.reward2_node"]:setActive(false)
    if self.params.typeNode == ModuleManager.TipsManager.REWARDS_TIPS_TYPE.COMMON then
        self.tipsNode = uiMap["rewards_tips.bg.reward_node"]
        self.descTx = uiMap["rewards_tips.reward_node.desc"]
        self.rewardNode = uiMap["rewards_tips.reward_node.reward_layout"]
        if not self.rewardCells then
            self.rewardCells = {}
            for i = 1, 3 do
                self.rewardCells[i] = CellManager:addCellComp(uiMap["rewards_tips.bg.reward_node.reward_layout.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
            end
        end
    elseif self.params.typeNode == ModuleManager.TipsManager.REWARDS_TIPS_TYPE.TASK then
        self.tipsNode = uiMap["rewards_tips.bg.reward2_node"]
        self.descTx = uiMap["rewards_tips.bg.reward2_node.desc"]
        self.rewardNode = uiMap["rewards_tips.bg.reward2_node.reward_layout"]
        if not self.rewardCells then
            self.rewardCells = {}
            for i = 1, 3 do
                self.rewardCells[i] = CellManager:addCellComp(uiMap["rewards_tips.bg.reward2_node.reward_layout.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
            end
        end
    end
end

function RewardsTips:onLoadRootComplete()
	self:init()
    local tipsBgTransform = self.bg:getTransform()
    self.originSizeDelta = tipsBgTransform.sizeDelta
	self.originPivot = tipsBgTransform.pivot
	self.originAnchoredPosition = tipsBgTransform.anchoredPosition
	self.originLocalPosition = tipsBgTransform.localPosition
end

function RewardsTips:onRefresh()
	self.root:addClickListener(function ()
		self:closeUI()
	end)

    self.tipsNode:setActive(true)
    self.bgBlack:setActive(self.params.showBlackBg)
	self.descTx:setText(self.params.customTitleStr or I18N:getGlobalText(I18N.GlobalConst.REWARD_PREVIEW_DESC))
    if self.params.rewards then
        for i, cell in ipairs(self.rewardCells) do
            if self.params.rewards[i] then
                cell:refreshByConfig(self.params.rewards[i])
                -- cell:addClickListener(function()
                --     ModuleManager.TipsManager:showRewardTips(self.params.rewards[i].id, self.params.rewards[i].type, cell:getBaseObject())
                -- end)
            end
            cell:getBaseObject():setActive(self.params.rewards[i] ~= nil)
        end
    end
    self.rewardNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()

    if self.tarCornerScreenPos then
        self:locate(self.location, self.originSizeDelta, self.bg, self.tarCornerScreenPos)
    end
end

function RewardsTips:onClose()
	if self.originSizeDelta then
		local tipsBgTransform = self.bg:getTransform()
		tipsBgTransform.sizeDelta = self.originSizeDelta
		tipsBgTransform.pivot = self.originPivot
		tipsBgTransform.anchoredPosition = self.originAnchoredPosition
		tipsBgTransform.localPosition = self.originLocalPosition
	end
    if self.callback then
        self.callback()
    end
end

return RewardsTips