local EquipManager = class("EquipManager", BaseModule)

-- 展示材料获取弹窗(英雄id，部位，材料id，材料数量)
function EquipManager:showItemGetPop(heroId, part, id, num)
    self:reqEquipUpgradeGift(heroId, part)

    UIManager:showUI("app/ui/dungeon/item_get_ui", {heroId = heroId, part = part, id = id, value = num})
end

-- 检查装备礼包状态定时器
function EquipManager:updateEquipGiftTimer(isClear)
    self:unscheduleAll()

    if not isClear then
        local time = DataManager.EquipData:getGiftNearestRemainTime()
        if time and time > 0 then
            Logger.logHighlight("设置装备礼包倒计时:"..time)
            self.giftSid = self:performWithDelayGlobal(function()
                DataManager.EquipData:onGiftStateChange()
            end, time)
        end
    end
end

-- 请求触发装备升级礼包
function EquipManager:reqEquipUpgradeGift(heroId, part)
    if DataManager.EquipData:getCanShowGiftId(heroId, part) ~= nil then
        return
    end

    self:sendMessage(ProtoMsgType.FromMsgEnum.TriggerWeaponArmorGiftReq, {hero_id = heroId, equip_position = part}, {}, self.rspEquipUpgradeGift, nil)
end

function EquipManager:rspEquipUpgradeGift(result)
    DataManager.EquipData:initGifts(result.info)
end

-- 升级装备
function EquipManager:reqUpgrade(id, part)
    local entity = DataManager.EquipData:getEquip(id, part)
    if not entity then
        return
    end

    local heroEntity = DataManager.HeroData:getHeroById(id)
    if heroEntity == nil or not heroEntity:isUnlock() then
        return
    end

    for index, cost in ipairs(entity:getUpgradeMaterials()) do
        if cost.num > DataManager.BagData.ItemData:getItemNumById(cost.id) then
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_8))
            self:showItemGetPop(id, part, cost.id, cost.num)
            return
        end
    end

    if not entity:isEnoughGold() then
        if not ModuleManager.ShopManager:tryTriggerCoinGift() then
            GFunc.showItemNotEnough(GConst.ItemConst.ITEM_ID_GOLD)
        end
        return
    end

    if entity:isMaxLevel() then
        if entity:getPart() == GConst.EquipConst.PART_TYPE.WEAPON then
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_7, DataManager.PlayerData:getLv() + 1))
        else
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_9, entity:getLevel() + 1))
        end
        return
    end

    local itemGetType = BIReport.ITEM_GET_TYPE.EQUIP_UPGRADE
    if part ~= GConst.EquipConst.PART_TYPE.WEAPON then
        itemGetType = BIReport.ITEM_GET_TYPE.ARMOR_UPGRADE
    end
    self:sendMessage(ProtoMsgType.FromMsgEnum.EquipUpgradeReq, {hero_id = id, position = part}, {}, self.rspUpgrade, itemGetType)
end

function EquipManager:rspUpgrade(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.EquipData:onUpgradeEquip(result.reqData.hero_id, result.reqData.position)

        if result.reqData.position == GConst.EquipConst.PART_TYPE.WEAPON then
            ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_UPGRADE_WEAPON)
        else
            ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_UPGRADE_ARMOR)
        end
    end
end

return EquipManager