local BoxLevelUI = class("BoxLevelUI", BaseUI)

local BOX_3_CELL_HEIGHT = 260
local BG_WITHOUT_3_BOX_HEIGHT = 660

function BoxLevelUI:ctor(params)
    params = params or {}
    self.level = params.level or DataManager.SummonData:getSummonLevel()
end

function BoxLevelUI:isFullScreen()
    return false
end

function BoxLevelUI:getPrefabPath()
    return "assets/prefabs/ui/shop/box_level_ui.prefab"
end

function BoxLevelUI:onLoadRootComplete()
    self.uiMap = self.root:genAllChildren()
	self.uiMap["box_level_ui.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

    self.bg = self.uiMap["box_level_ui.bg"]
	self.titleTx = self.uiMap["box_level_ui.bg.title_img.title"]
    self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_37))
	self.lvTx = self.uiMap["box_level_ui.bg.lv_node.lv_bg.text"]
    self.leftArrow = self.uiMap["box_level_ui.bg.lv_node.left_arrow"]
    self.rightArrow = self.uiMap["box_level_ui.bg.lv_node.right_arrow"]

	self.box1Node = self.uiMap["box_level_ui.bg.box_1"]
    self.box1Add1Node = self.uiMap["box_level_ui.bg.box_1.add_node_1"]
    self.box1Add1ItemCell = CellManager:addCellComp(self.uiMap["box_level_ui.bg.box_1.add_node_1.item_cell"], GConst.TYPEOF_LUA_CLASS.ITEM_CELL)
    self.box1Add1Text = self.uiMap["box_level_ui.bg.box_1.add_node_1.text"]
    self.box1Add2Node = self.uiMap["box_level_ui.bg.box_1.add_node_2"]
    self.box1Add2ItemCell = CellManager:addCellComp(self.uiMap["box_level_ui.bg.box_1.add_node_2.item_cell"], GConst.TYPEOF_LUA_CLASS.ITEM_CELL)
    self.box1Add2Text = self.uiMap["box_level_ui.bg.box_1.add_node_2.text"]
    self.box1Desc = self.uiMap["box_level_ui.bg.box_1.none_text"]
    self.box1Desc:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_4)) -- 无额外奖励加成

	self.box2Node = self.uiMap["box_level_ui.bg.box_2"]
    self.box2Add1Node = self.uiMap["box_level_ui.bg.box_2.add_node_1"]
    self.box2Add1ItemCell = CellManager:addCellComp(self.uiMap["box_level_ui.bg.box_2.add_node_1.item_cell"], GConst.TYPEOF_LUA_CLASS.ITEM_CELL)
    self.box2Add1Text = self.uiMap["box_level_ui.bg.box_2.add_node_1.text"]
    self.box2Add2Node = self.uiMap["box_level_ui.bg.box_2.add_node_2"]
    self.box2Add2ItemCell = CellManager:addCellComp(self.uiMap["box_level_ui.bg.box_2.add_node_2.item_cell"], GConst.TYPEOF_LUA_CLASS.ITEM_CELL)
    self.box2Add2Text = self.uiMap["box_level_ui.bg.box_2.add_node_2.text"]
    self.box2Desc = self.uiMap["box_level_ui.bg.box_2.none_text"]
    self.box2Desc:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_4)) -- 无额外奖励加成

    self.box3Node = self.uiMap["box_level_ui.bg.box_3"]
    self.box3Add1Node = self.uiMap["box_level_ui.bg.box_3.add_node_1"]
    self.box3Add1ItemCell = CellManager:addCellComp(self.uiMap["box_level_ui.bg.box_3.add_node_1.item_cell"], GConst.TYPEOF_LUA_CLASS.ITEM_CELL)
    self.box3Add1Text = self.uiMap["box_level_ui.bg.box_3.add_node_1.text"]
    self.box3Add2Node = self.uiMap["box_level_ui.bg.box_3.add_node_2"]
    self.box3Add2ItemCell = CellManager:addCellComp(self.uiMap["box_level_ui.bg.box_3.add_node_2.item_cell"], GConst.TYPEOF_LUA_CLASS.ITEM_CELL)
    self.box3Add2Text = self.uiMap["box_level_ui.bg.box_3.add_node_2.text"]
    self.box3Desc = self.uiMap["box_level_ui.bg.box_3.none_text"]
    self.box3Desc:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_4)) -- 无额外奖励加成

    self.bottomDesc = self.uiMap["box_level_ui.bg.title"]
    self.bottomDesc:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_5)) -- 开宝箱提升等级,增加宝箱奖励

    self.leftArrow:addClickListener(function()
        self.level = self.level - 1
        if self.level <= 1 then
            self.level = 1
        end
        self:refresh()
    end)
    self.rightArrow:addClickListener(function()
        self.level = self.level + 1
        if self.level >= DataManager.SummonData:getSummonMaxLv() then
            self.level = DataManager.SummonData:getSummonMaxLv()
        end
        self:refresh()
    end)

    self:refresh()
end

function BoxLevelUI:refresh()
    self.lvTx:setText("Lv." .. tostring(self.level))

	local maxLevel = DataManager.SummonData:getSummonMaxLv()
    if self.level == 1 then
        self.leftArrow:setSprite(GConst.ATLAS_PATH.COMMON, "common_arrow_3")
        self.rightArrow:setSprite(GConst.ATLAS_PATH.COMMON, "common_arrow_2")
    elseif self.level == maxLevel then
        self.leftArrow:setSprite(GConst.ATLAS_PATH.COMMON, "common_arrow_2")
        self.rightArrow:setSprite(GConst.ATLAS_PATH.COMMON, "common_arrow_3")
    else
        self.leftArrow:setSprite(GConst.ATLAS_PATH.COMMON, "common_arrow_2")
        self.rightArrow:setSprite(GConst.ATLAS_PATH.COMMON, "common_arrow_2")
    end

    if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.STORE_BOX_3_OPEN, true) then
        self.box3Node:setVisible(false)
        self.bg:setSizeDeltaY(BG_WITHOUT_3_BOX_HEIGHT)
    else
        self.box3Node:setVisible(true)
        self.bg:setSizeDeltaY(BG_WITHOUT_3_BOX_HEIGHT + BOX_3_CELL_HEIGHT)
    end

    if self.level == 1 then
        self.box1Add1Node:setVisible(false)
        self.box1Add2Node:setVisible(false)
        self.box1Desc:setVisible(true)

        self.box2Add1Node:setVisible(false)
        self.box2Add2Node:setVisible(false)
        self.box2Desc:setVisible(true)

        self.box3Add1Node:setVisible(false)
        self.box3Add2Node:setVisible(false)
        self.box3Desc:setVisible(true)
    else
        self.box1Add1Node:setVisible(false)
        self.box1Add2Node:setVisible(false)
        self.box1Desc:setVisible(false)

        self.box2Add1Node:setVisible(false)
        self.box2Add2Node:setVisible(false)
        self.box2Desc:setVisible(false)

        self.box3Add1Node:setVisible(false)
        self.box3Add2Node:setVisible(false)
        self.box3Desc:setVisible(false)

        local reward1 = DataManager.SummonData:getSummonRewardByLv(GConst.SummonConst.SUMMON_TYPE.LV_1, self.level)
        local reward2 = DataManager.SummonData:getSummonRewardByLv(GConst.SummonConst.SUMMON_TYPE.LV_2, self.level)
        local reward3 = DataManager.SummonData:getSummonRewardByLv(GConst.SummonConst.SUMMON_TYPE.LV_3, self.level)

        local count = #reward1
        if count == 1 then
            self.box1Add1Node:setVisible(true)
            self.box1Add1ItemCell:refreshByCfg(reward1[1].id, 0)
            self.box1Add1ItemCell:setNum("")
            self.box1Add1ItemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(reward1[1].id, GConst.REWARD_TYPE.ITEM, self.box1Add1ItemCell:getBaseObject())
			end)
            self.box1Add1Text:setText("+" .. tostring(reward1[1].num))
        else
            self.box1Add1Node:setVisible(true)
            self.box1Add1ItemCell:refreshByCfg(reward1[1].id, 0)
            self.box1Add1ItemCell:setNum("")
            self.box1Add1ItemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(reward1[1].id, GConst.REWARD_TYPE.ITEM, self.box1Add1ItemCell:getBaseObject())
			end)
            self.box1Add1Text:setText("+" .. tostring(reward1[1].num))

            self.box1Add2Node:setVisible(true)
            self.box1Add2ItemCell:refreshByCfg(reward1[2].id, 0)
            self.box1Add2ItemCell:setNum("")
            self.box1Add2ItemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(reward1[2].id, GConst.REWARD_TYPE.ITEM, self.box1Add2ItemCell:getBaseObject())
			end)
            self.box1Add2Text:setText("+" .. tostring(reward1[2].num))
        end

        local count = #reward2
        if count == 1 then
            self.box2Add1Node:setVisible(true)
            self.box2Add1ItemCell:refreshByCfg(reward2[1].id, 0)
            self.box2Add1ItemCell:setNum("")
            self.box2Add1ItemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(reward2[1].id, GConst.REWARD_TYPE.ITEM, self.box2Add1ItemCell:getBaseObject())
			end)
            self.box2Add1Text:setText("+" .. tostring(reward2[1].num))
        else
            self.box2Add1Node:setVisible(true)
            self.box2Add1ItemCell:refreshByCfg(reward2[1].id, 0)
            self.box2Add1ItemCell:setNum("")
            self.box2Add1ItemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(reward2[1].id, GConst.REWARD_TYPE.ITEM, self.box2Add1ItemCell:getBaseObject())
			end)
            self.box2Add1Text:setText("+" .. tostring(reward2[1].num))

            self.box2Add2Node:setVisible(true)
            self.box2Add2ItemCell:refreshByCfg(reward2[2].id, 0)
            self.box2Add2ItemCell:setNum("")
            self.box2Add2ItemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(reward2[2].id, GConst.REWARD_TYPE.ITEM, self.box2Add2ItemCell:getBaseObject())
			end)
            self.box2Add2Text:setText("+" .. tostring(reward2[2].num))
        end

        local count = #reward3
        if count == 1 then
            self.box3Add1Node:setVisible(true)
            self.box3Add1ItemCell:refreshByCfg(reward3[1].id, 0)
            self.box3Add1ItemCell:setNum("")
            self.box3Add1ItemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(reward3[1].id, GConst.REWARD_TYPE.ITEM, self.box3Add1ItemCell:getBaseObject())
			end)
            self.box3Add1Text:setText("+" .. tostring(reward3[1].num))
        else
            self.box3Add1Node:setVisible(true)
            self.box3Add1ItemCell:refreshByCfg(reward3[1].id, 0)
            self.box3Add1ItemCell:setNum("")
            self.box3Add1ItemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(reward3[1].id, GConst.REWARD_TYPE.ITEM, self.box3Add1ItemCell:getBaseObject())
			end)
            self.box3Add1Text:setText("+" .. tostring(reward3[1].num))

            self.box3Add2Node:setVisible(true)
            self.box3Add2ItemCell:refreshByCfg(reward3[2].id, 0)
            self.box3Add2ItemCell:setNum("")
            self.box3Add2ItemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(reward3[2].id, GConst.REWARD_TYPE.ITEM, self.box3Add2ItemCell:getBaseObject())
			end)
            self.box3Add2Text:setText("+" .. tostring(reward3[2].num))
        end
    end
end

return BoxLevelUI