local TowerBountyComp = class("TowerBountyComp", LuaComponent)

local CELL = "app/ui/fund/cell/fund_tower_cell"

local TAG_COMMON_TOTAL_SIZE = 640
local TAG_SIZE = 178

function TowerBountyComp:onClose()
    self.curStage = nil
end

function TowerBountyComp:init()
    local uiMap = self:getUIMap()
    -- self.banner = uiMap["tower_bounty_comp.bg.banner"]
    -- self.bg = uiMap["tower_bounty_comp.top.bg"]
    -- self.txDesc = uiMap["tower_bounty_comp.top.bg.desc_1"]
    -- self.txDesc2 = uiMap["tower_bounty_comp.top.bg.desc_2"]
    -- self.btnHelp = uiMap["tower_bounty_comp.top.tx_title.btn_help"]
    -- self.scrollrect = uiMap["tower_bounty_comp.scrollrect"]
    -- self.txLevel = uiMap["tower_bounty_comp.scrollrect.title.tx_level"]
    -- self.txCommon = uiMap["tower_bounty_comp.scrollrect.title.tx_common"]
    -- self.txPro = uiMap["tower_bounty_comp.scrollrect.title.tx_pro"]
    -- self.btnGet = uiMap["tower_bounty_comp.btns.btn_get"]
    -- self.txGet = uiMap["tower_bounty_comp.btns.btn_get.tx_desc"]
    -- self.btnBuy = uiMap["tower_bounty_comp.btns.btn_buy"]
    -- self.txCost = uiMap["tower_bounty_comp.btns.btn_buy.tx_cost"]
    -- self.tagsNode = uiMap["tower_bounty_comp.tags"]
    -- self.tagsViewport = uiMap["tower_bounty_comp.tags.viewport"]
    -- self.tagsContent = uiMap["tower_bounty_comp.tags.viewport.content"]
    -- self.tags = {}
    -- self.tagImgs = {}
    -- self.tagDescs = {}
    -- for i = 1, 10 do         
    --     table.insert(self.tags, uiMap["tower_bounty_comp.tags.viewport.content.tag_" .. i])
    --     table.insert(self.tagImgs, uiMap["tower_bounty_comp.tags.viewport.content.tag_" .. i .. ".img"])
    --     table.insert(self.tagDescs, uiMap["tower_bounty_comp.tags.viewport.content.tag_" .. i .. ".img.tx_desc"])
    -- end

    -- self.btnBuy:addClickListener(function()
    --     local giftId = DataManager.TowerFundData:getProGiftId(self.curStage)
    --     ModuleManager.FundTowerManager:onBuyTowerBounty(giftId)
    -- end)
    -- self.btnGet:addClickListener(function()
    --     ModuleManager.FundTowerManager:getReward(self.curStage)
    -- end)
    -- self.btnHelp:addClickListener(function()
    --     ModuleManager.TipsManager:showDescTips(I18N:getGlobalText(I18N.GlobalConst.FUND_TOWER_DESC_2), self.btnHelp)
    -- end)
    -- self:bind(DataManager.TowerFundData, "isDirty", function()
    --     if not DataManager.TowerFundData:getIsOpen() then
    --         return
    --     end
    --     if not DataManager.TowerFundData:isStageUnlock(self.curStage) then
    --         self.curStage = DataManager.TowerFundData:getCurStage()
    --     end
    --     if self.curStage == nil then
    --         return
    --     end
    --     if not DataManager.TowerFundData:canGetRewards(self.curStage) then
    --         local claimedStage = DataManager.TowerFundData:getMinUnclaimedStage()
    --         if claimedStage ~= nil then
    --             self.curStage = claimedStage
    --         end
    --     end
	-- 	self:refresh(true)
	-- end)
end

function TowerBountyComp:refresh(isAuto)
    -- if self.curStage == nil then
    --     self.curStage = DataManager.TowerFundData:getMinUnclaimedStage() or DataManager.TowerFundData:getMaxUnclaimedStage()
    --     isAuto = true
    -- end

    -- self:updateData(self.curStage)
    -- self:refreshStageTags()
    -- if isAuto then
    --     local stageList = DataManager.TowerFundData:getUnlockStageList()
    --     local index
    --     for k, v in pairs(stageList) do
    --         if self.curStage == v then
    --             index = k
    --         end
    --     end
    --     if index then
    --         self.tagsContent:setAnchoredPositionX(-self.tags[index]:getAnchoredPositionX())
    --     end
    -- end

    -- if not DataManager.TowerFundData:getProBought(self.curStage) then
	-- 	local giftId = DataManager.TowerFundData:getProGiftId(self.curStage)
	-- 	local cfg = ConfigManager:getConfig("act_gift")[giftId]
    --     self.txCost:setText(GFunc.getFormatPrice(cfg.recharge_id))
	-- 	self.btnBuy:setActive(true)
	-- else
	-- 	self.btnBuy:setActive(false)
	-- end
    -- self.btnGet:setActive(DataManager.TowerFundData:canGetRewards(self.curStage))
    -- self.maxIndex = #self.cfgList
    -- self.minIndex = DataManager.TowerFundData:getMinUnclaimedRewardIndex(self.curStage)
    -- if self.scrollRect == nil then
    --     self.scrollRect = self.scrollrect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    --     self.scrollRect:addInitCallback(function()
    --         return CELL
    --     end)
    --     self.scrollRect:addRefreshCallback(function(index, cell)
    --         cell:refresh(index, self.cfgList[index], index == self.maxIndex)
    --     end)
    -- end
    -- if self.scrollRect:getTotalCount() == nil or self.scrollRect:getTotalCount() <= 0 then
	-- 	self.scrollRect:refillCells(self.maxIndex)
    -- elseif self.scrollRect:getTotalCount() ~= self.maxIndex then
	-- 	self.scrollRect:clearCells()
	-- 	self.scrollRect:refillCells(self.maxIndex)
	-- else
	-- 	self.scrollRect:updateAllCell()
	-- end
    -- self.scrollRect:moveToIndex(self.minIndex)

    -- if self.curStage then
    --     BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.TOWER_BOUNTY .. self.curStage)
    -- end
end

function TowerBountyComp:updateData(stage)
    stage = stage or DataManager.TowerFundData:getCurStage()
	if DataManager.TowerFundData:getIsOpen(stage) then
		self.cfgList = DataManager.TowerFundData:getListByStage(stage)
    else
        self.cfgList = {}
	end
end

function TowerBountyComp:onClickStageTag(stage)
    self.curStage = stage
    self:refresh()
end

function TowerBountyComp:refreshStageTags()
    -- local stageList = DataManager.TowerFundData:getUnlockStageList()
    -- if #stageList <= 1 then
    --     self.tagsNode:setActive(false)
    --     return
    -- end
    -- self.tagsNode:setActive(true)
    -- local totalWidth = #stageList * TAG_SIZE + (#stageList - 1) * 5
    -- self.tagsContent:setSizeDeltaX(totalWidth)
    -- self.tagsViewport:setSizeDeltaX(totalWidth < TAG_COMMON_TOTAL_SIZE and totalWidth or TAG_COMMON_TOTAL_SIZE)

    -- for i, tag in ipairs(self.tags) do
    --     local stage = stageList[i]
    --     if stage then
    --         tag:setActive(true)
    --         local isSelect = self.curStage == stage
    --         self.tagImgs[i]:setSprite(GConst.ATLAS_PATH.COMMON, isSelect and "common_menu_4" or "common_menu_5")
    --         self.tagImgs[i]:setAnchoredPositionY(isSelect and 5 or 0)
    --         self.tagDescs[i]:setText(I18N:getGlobalText(I18N.GlobalConst["FUND_TOWER_TAG_DESC_" .. stage]))

    --         if DataManager.TowerFundData:canGetRewards(stage) then
    --             tag:addRedPoint(80, 33, 0.9)
    --         else
    --             tag:removeRedPoint()
    --         end

    --         tag:addClickListener(function()
    --             if isSelect then
    --                 return
    --             end
    --             self:onClickStageTag(stage)
    --         end)
    --     else
    --         tag:setActive(false)
    --     end
    -- end
end

return TowerBountyComp