local BountyComp = class("BountyComp", LuaComponent)

local CELL = "app/ui/privilege/cell/bounty_cell"

function BountyComp:onClose()
end

function BountyComp:init()
    local uiMap = self:getUIMap()
    self.txTitle = uiMap["bounty_comp.top.tx_title"]
    self.btnHelp = uiMap["bounty_comp.top.tx_title.btn_help"]
    self.txTime = uiMap["bounty_comp.top.time.tx_time"]
    self.txTips = uiMap["bounty_comp.top.tx_tips"]
    self.txTips1 = uiMap["bounty_comp.top.img_prog.tx_tips1"]
    self.levelNode = uiMap["bounty_comp.level_node"]
    self.txLv = uiMap["bounty_comp.top.level_node.content.level.tx_lv"]
    self.imgProg = uiMap["bounty_comp.top.level_node.content.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.txProg = uiMap["bounty_comp.top.level_node.content.prog.tx_prog"]
    self.txLvTips = uiMap["bounty_comp.level_node.content.tx_tips"]
    self.txToday = uiMap["bounty_comp.level_node.content.tx_today"]
    self.scrollrect = uiMap["bounty_comp.scrollrect"]
    self.txLevel = uiMap["bounty_comp.scrollrect.title.tx_level"]
    self.txCommon = uiMap["bounty_comp.scrollrect.title.tx_common"]
    self.txPro1 = uiMap["bounty_comp.scrollrect.title.tx_pro_1"]
    self.txPro2 = uiMap["bounty_comp.scrollrect.title.tx_pro_2"]
    self.btnGet = uiMap["bounty_comp.btns.btn_get"]
    self.txGet = uiMap["bounty_comp.btns.btn_get.tx_desc"]
    self.btnBuy1 = uiMap["bounty_comp.top.btn_buy_1"]
    self.txBuy1 = uiMap["bounty_comp.top.btn_buy_1.tx_desc"]
    self.btnBuy2 = uiMap["bounty_comp.top.btn_buy_2"]
    self.txBuy2 = uiMap["bounty_comp.top.btn_buy_2.tx_desc"]

    
    self.pro1_Items = {}
    self.pro1_TxCount = {}
    self.pro1_ImgIcon = {}
    for i = 1, 4 do        
        local item = uiMap["bounty_comp.top.btn_buy_1.tips_1.groud.item_" .. i]
        local txCount = uiMap["bounty_comp.top.btn_buy_1.tips_1.groud.item_" .. i .. ".tx_count"]
        local imgProg = uiMap["bounty_comp.top.btn_buy_1.tips_1.groud.item_" .. i .. ".img_prog"]
        table.insert(self.pro1_Items, item)
        table.insert(self.pro1_TxCount, txCount)
        table.insert(self.pro1_ImgIcon, imgProg)
    end

    self.pro2_Items = {}
    self.pro2_TxCount = {}
    self.pro2_ImgIcon = {}
    for i = 1, 4 do
        local item = uiMap["bounty_comp.top.btn_buy_2.tips_2.groud.item_" .. i]
        local txCount = uiMap["bounty_comp.top.btn_buy_2.tips_2.groud.item_" .. i .. ".tx_count"]
        local imgProg = uiMap["bounty_comp.top.btn_buy_2.tips_2.groud.item_" .. i .. ".img_prog"]
        table.insert(self.pro2_Items, item)
        table.insert(self.pro2_TxCount, txCount)
        table.insert(self.pro2_ImgIcon, imgProg)
    end

    self.btnBuy1:addClickListener(function()
        ModuleManager.BountyManager:onUnlockPro(GConst.BountyConst.REWARD_GEAR.PRO_1)
    end)
    self.btnBuy2:addClickListener(function()
        ModuleManager.BountyManager:onUnlockPro(GConst.BountyConst.REWARD_GEAR.PRO_2)
    end)
    self.btnGet:addClickListener(function()
        ModuleManager.BountyManager:reqGetAllRewards()
    end)
    self.btnHelp:addClickListener(function()
        -- ModuleManager.TipsManager:showDescTips(I18N:getGlobalText(I18N.GlobalConst.BOUNTY_DESC_9), self.btnHelp)
    end)
    self:bind(DataManager.BountyData, "isDirty", function()
        self:refresh(true)
    end)

    -- self.txTitle:setSizeDeltaX(400)
	-- local width = self.txTitle:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
	-- width = math.min(400, width)
	-- self.txTitle:setSizeDeltaX(width)
end

function BountyComp:updateTime()
	self.txTime:setText(I18N:getGlobalText(I18N.GlobalConst.NEXT_REFRESH_DESC, Time:formatNumTimeStr(DataManager.BountyData:getRemainTime())))
end

function BountyComp:refresh(notFirst)
    local lv = DataManager.BountyData:getLevel()
    local exp = DataManager.BountyData:getExp()
    local maxLv = DataManager.BountyData:getMaxLv()
    if lv >= maxLv then
		exp = 0
		lv = maxLv
	end
    self.txLv:setText(lv)
    local curExp = exp
    local fullExp = DataManager.BountyData:getLvUpExp()
    self.txProg:setText(curExp .. "/" .. fullExp)
    self.imgProg.value = curExp / fullExp

    local gift1 = ConfigManager:getConfig("act_gift")[DataManager.BountyData:getProRewardGiftId(GConst.BountyConst.REWARD_GEAR.PRO_1)]
    local gift2 = ConfigManager:getConfig("act_gift")[DataManager.BountyData:getProRewardGiftId(GConst.BountyConst.REWARD_GEAR.PRO_2)]
    self.txBuy1:setText(GFunc.getFormatPrice(gift1.recharge_id))
    self.txBuy2:setText(GFunc.getFormatPrice(gift2.recharge_id))
    self.txTips1:setText(gift2.value .. "%")

    self.btnGet:setActive(DataManager.BountyData:canGetRewards())
    self.btnBuy1:setActive(not DataManager.BountyData:isUnlockReward(GConst.BountyConst.REWARD_GEAR.PRO_1))
    self.btnBuy2:setActive(not DataManager.BountyData:isUnlockReward(GConst.BountyConst.REWARD_GEAR.PRO_2))

    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)
        end)
    end
    local count = DataManager.BountyData:getMaxLv()
    if self.scrollRect:getTotalCount() == nil or self.scrollRect:getTotalCount() <= 0 then
		self.scrollRect:refillCells(count)
    elseif self.scrollRect:getTotalCount() ~= count then
		self.scrollRect:clearCells()
		self.scrollRect:refillCells(count)
	else
		self.scrollRect:updateAllCell()
	end

    if not notFirst then
        self.scrollRect:moveToIndex(DataManager.BountyData:getMoveIdx())
    end

    local rewards = DataManager.BountyData:getAllRewards(GConst.BountyConst.REWARD_GEAR.PRO_1)
    for i,v in ipairs(self.pro1_Items) do
        v:setActive(false)
    end
    for i,v in ipairs(self.pro2_Items) do
        v:setActive(false)
    end
    local index = 0
    for id, reward in pairs(rewards) do
        index = index + 1
        local item = self.pro1_Items[index]
        local txCount = self.pro1_TxCount[index]
        local imgProg = self.pro1_ImgIcon[index]
        if reward then
            item:setActive(true)
            local itemNum = reward.num or 0
            imgProg:setSprite(GConst.ATLAS_PATH.ICON_ITEM, ModuleManager.ItemManager:getItemIcon(id))
            txCount:setText(itemNum)
        end
    end
    rewards = DataManager.BountyData:getAllRewards(GConst.BountyConst.REWARD_GEAR.PRO_2)
    index = 0
    for id, reward in pairs(rewards) do
        index = index + 1
        local item = self.pro2_Items[index]
        local txCount = self.pro2_TxCount[index]
        local imgProg = self.pro2_ImgIcon[index]
        if reward then
            item:setActive(true)
            local itemNum = reward.num or 0
            imgProg:setSprite(GConst.ATLAS_PATH.ICON_ITEM, ModuleManager.ItemManager:getItemIcon(id))
            txCount:setText(itemNum)
        end
    end

end

return BountyComp