SMODS.Atlas {
    px = 71,
    py = 95,
    key = "janedecks",
    path = Jane.config.texture_pack .. "/b_jane_decks.png"
}

local eternal_text = Cryptid and "Absolute" or "Eternal"

SMODS.Back {
    key = "nitro",
    atlas = "janedecks",
    pos = {x = 1, y = 1},
    loc_txt = {
        name = "Acceleration Deck",
        text = {
            Cryptid and "" or "{C:attention}Ante increases twice{} as strong",
            "After defeating the {C:attention}Boss Blind{},",
            Cryptid and "set {C:attention}ante {}to the {C:attention}next" or "",
            Cryptid and "{C:attention}triangle number {}and create" or "",
            Cryptid and "an {C:spectral,E:1}Empowered Tag" or "create a {C:dark_edition}Negative {C:spectral,E:1}Soul",
        }
    },
    apply = function(_)
        G.GAME.win_ante = Cryptid and G.GAME.win_ante * (G.GAME.win_ante + 1) / 2 or G.GAME.win_ante * 2 - 1
        G.GAME.nitro = true
    end,
    trigger_effect = function(_, args)
        if args.context == "eval" and G.GAME.last_blind and G.GAME.last_blind.boss then
            Jane.q(function()
                Jane.empowered()
                return true
            end)
        end
    end
}

SMODS.Back {
    key = 'obsidian',
    atlas = 'janedecks',
    pos = {x = 2, y = 1},
    loc_txt = {
        name = 'Obsidian Deck',
        text = {
            '{C:attention}Hidden{} cards {C:inactive}(ex. {C:spectral}The Soul{C:inactive})',
            'can {C:attention}appear normally{}',
        }
    },
    apply = function(_)
        G.GAME.obsidian = true
    end
}

SMODS.Back {
    key = "orrery",
    atlas = "janedecks",
    pos = {x = 0, y = 0},
    loc_txt = {
        name = "Orrery Deck",
        text = {
            "{C:attention}Hand stats {}are",
            "always " .. (Cryptid and "{C:cry_ascendant}" or "{C:attention}") .. "equalized",
        }
    },
    apply = function(_)
        Jane.q(function()
            local orrery = {}
            G.GAME.orrery = orrery

            for k, v in pairs(G.GAME.hands) do
                v.mult = 1
                v.chips = 150
                orrery[k] = {chips = v.chips, level = v.level, mult = v.mult}
            end

            save_run()
            return true
        end
    )
    end
}

SMODS.Back {
    key = "tortoise",
    atlas = "janedecks",
    pos = {x = 3, y = 1},
    loc_txt = {
        name = "Tortoise Deck",
        text = {
            "{C:attention}Ante increases",
            "{C:attention}half{} as strong",
        }
    },
    apply = function(_)
        G.GAME.tortoise = true
    end
}

SMODS.Back {
    key = "weeck",
    atlas = "janedecks",
    pos = {x = 4, y = 1},
    loc_txt = {
        name = "Weeck",
        text = {
            "Start with an {C:purple,E:1}" .. eternal_text,
            "{C:attention}Wee Joker {}and a deck",
            "containing {C:attention}2 {C:purple,E:1}" .. eternal_text,
            "{C:attention}2's {}of {C:attention}each suit",
        }
    },
    apply = function(_)
        G.GAME.weeck = true

        Jane.q(function()
            local new_card = create_card("Joker", G.jokers, nil, nil, nil, nil, "j_wee", "weeck")
            new_card.ability.cry_absolute = true
            new_card.ability.eternal = true
            G.jokers:emplace(new_card)
            local add = {}

            for _, v in pairs(G.playing_cards) do
                if v.base.id == 2 then
                    v.ability.cry_absolute = true
                    v.ability.eternal = true
                    add[#add + 1] = v
                else
                    v:start_dissolve(nil, true)
                end
            end

            for _, v in pairs(add) do
                local dupe = copy_card(v)
                dupe:start_materialize()
                dupe:add_to_deck()
                G.hand:emplace(dupe)
                G.playing_card = (G.playing_card and G.playing_card + 1) or 1
                table.insert(G.playing_cards, dupe)
            end

            save_run()
            return true
        end)
    end
}

local orig_ante = ease_ante

---@diagnostic disable-next-line: lowercase-global
function ease_ante(mod)
    local function next_triangle_number(x)
        local n = (math.sqrt(8 * x + 1) - 1) / 2
        return (n + 1) * (n + 2) / 2
    end

    if G.GAME.nitro then
        mod = Cryptid and
            (G.GAME.round_resets.ante < 0 and -G.GAME.round_resets.ante
            or math.ceil(next_triangle_number(G.GAME.round_resets.ante) - G.GAME.round_resets.ante)) or
            (mod > 0 and mod * 2 or mod)
    end

    if G.GAME.tortoise and mod > 0 then
        local remainder = 0

        if mod % 2 == 1 then
            G.GAME.tortoise_halved = not G.GAME.tortoise_halved
            remainder = G.GAME.tortoise_halved and 0 or 1
        end

        mod = math.floor(mod / 2) + remainder
    end

    orig_ante(mod)
end
