local ItemGetUI = class("ItemGetUI", BaseUI)

local COMMON_MAIN_POSY = 0
local COMMON_CLOSE_POSY = 280
local GIFT_MAIN_POSY = 220
local GIFT_CLOSE_POSY = 140

function ItemGetUI:isFullScreen()
    return false
end

function ItemGetUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon/item_get_ui.prefab"
end

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

function ItemGetUI:onClose()
    if self.giftCountdownSid then
		self.txCountdown:unscheduleGlobal(self.giftCountdownSid)
	end
end

function ItemGetUI:ctor(parmas)
    self.equipEntity = DataManager.EquipData:getEquip(parmas.heroId, parmas.part)
    self.targetId = parmas.id
    self.targetNum = parmas.value
end

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

    self.btnClose = uiMap["item_get_ui.content.btn_close"]
    -- 主界面
    self.panelMain = uiMap["item_get_ui.panel_main"]
    self.txTitle = uiMap["item_get_ui.content.title.tx_title"]
    self.rewardCell = uiMap["item_get_ui.content.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    self.txDesc = uiMap["item_get_ui.content.tx_desc"]
    self.txGet = uiMap["item_get_ui.content.tx_get"]
    self.scrollRectComp = uiMap["item_get_ui.content.scroll_view"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    -- 礼包界面
    self.panelGift = uiMap["item_get_ui.panel_gift"]
    self.txGiftTitle = uiMap["item_get_ui.panel_gift.tx_title"]
    self.txCountdown = uiMap["item_get_ui.panel_gift.time_node.tx_countdown"]
    self.btnBuy = uiMap["item_get_ui.panel_gift.buy_btn"]
    self.txBuy = uiMap["item_get_ui.panel_gift.buy_btn.tx_buy"]
    self.rewardNodeLayout = uiMap["item_get_ui.panel_gift.item_node"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
    self.rewardCellList = {}
    for i = 1, 4 do
        table.insert(self.rewardCellList, CellManager:addCellComp(uiMap["item_get_ui.panel_gift.item_node.pop_reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.POP_REWARD_CELL))
    end

    self.scrollRectComp:addInitCallback(function()
		return "app/ui/dungeon/cell/dungeon_target_cell"
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.targetId, self.targetNum, self.wayType, self.wayList[index])
	end)

    self.btnBuy:addClickListener(function()
        if self.giftId then
            PayManager:purchasePackage(self.giftId, PayManager.PURCHARSE_TYPE.ACT_GIFT)
        end
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.GO_DUNGEON_UI, function()
        self:closeUI()
    end)
    self:bind(DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON), "isDirty", function()
        self:onRefresh()
	end)
    self:bind(DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_ARMOR), "isDirty", function()
        self:onRefresh()
	end)
    self:bind(DataManager.EquipData, "isDirty", function()
        self:onRefresh()
	end)
end

function ItemGetUI:onRefresh()
    self.txTitle:setText(I18N:getText("item", self.targetId, "name"))
    self.txDesc:setText(I18N:getText("item", self.targetId, "desc"))
    self.txGet:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_23))
    self.rewardCell:refreshItemById(self.targetId)

    local itemCfg = ConfigManager:getConfig("item")[self.targetId]
    if itemCfg == nil or itemCfg.get_way_type == nil or itemCfg.get_way == nil or #itemCfg.get_way == 0 then
        self:closeUI()
        return
    end

    self.wayType = itemCfg.get_way_type

    local weaponData
    if self.wayType == GConst.DungeonConst.TYPE.WEAPON then
        weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON)
    elseif self.wayType == GConst.DungeonConst.TYPE.ARMOR then
        weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_ARMOR)
    end
    local farm = {}
    local unlock = {}
    local lock = {}
    for index, id in ipairs(itemCfg.get_way) do
        if weaponData:canFarmChapter(id) and weaponData:getRemianFarmCount(id) > 0 then
            table.insert(farm, id)
        elseif weaponData:canFightChapter(id) then
            table.insert(unlock, id)
        else
            table.insert(lock, id)
        end
    end
    unlock = table.addArray(farm, unlock)
    self.wayList = table.addArray(unlock, lock)

    self.scrollRectComp:clearCells()
	self.scrollRectComp:refillCells(#self.wayList)

    self:checkShowPanelGift()
end

-- 展示礼包界面
function ItemGetUI:checkShowPanelGift()
    self.panelGift:setActive(false)
    self.panelMain:setAnchoredPositionY(COMMON_MAIN_POSY)
    self.btnClose:setAnchoredPositionY(COMMON_CLOSE_POSY)

    self.giftId = DataManager.EquipData:getCanShowGiftId(self.equipEntity:getHeroId(), self.equipEntity:getPart())
    if self.giftId == nil then
        return
    end

    self.panelGift:setActive(true)
    self.panelMain:setAnchoredPositionY(GIFT_MAIN_POSY)
    self.btnClose:setAnchoredPositionY(GIFT_CLOSE_POSY)

    local gift = DataManager.ShopData:getActGiftConfig()[self.giftId]
    self.txBuy:setText(GFunc.getFormatPrice(gift.recharge_id))
    self.txGiftTitle:setText(DataManager.EquipData:getGiftTitle(self.giftId))
    for i, cell in ipairs(self.rewardCellList) do
        if gift.reward[i] then
            cell:setVisible(true, 0.8)
            cell:refresh(gift.reward[i].id, gift.reward[i].num, true)
        else
            cell:setVisible(false, 0.8)
        end
    end
    self.rewardNodeLayout:RefreshLayout()

    -- 倒计时
	if self.giftCountdownSid then
		self.txCountdown:unscheduleGlobal(self.giftCountdownSid)
	end
    self.txCountdown:setText(GFunc.getTimeStrWithHMS2(DataManager.EquipData:getGiftRemainTime(self.giftId)))
	self.giftCountdownSid = self.txCountdown:scheduleGlobal(function()
        self.txCountdown:setText(GFunc.getTimeStrWithHMS2(DataManager.EquipData:getGiftRemainTime(self.giftId)))
	end, 1)
end

return ItemGetUI