Jane = {
    config = {
        wee_sizemod = 1.25,
        texture_pack = "default",
        bans = {"j_cry_crustulum", "c_cry_hammerspace"},
    },
}

Jane.misprintize = (Cryptid or {}).misprintize
Jane.misprintize_tbl = (Cryptid or {}).misprintize_tbl
Jane.misprinitze_val = (Cryptid or {}).misprintize_val
Jane.rarity_ids = Cryptid and {1, 2, 3, "cry_epic", 4, "cry_exotic", "jane_junk"} or {1, 2, 3, 4, "jane_junk"}

Jane.rarity_names = Cryptid and {"Common", "Uncommon", "Rare", "Epic", "Legendary", "Exotic", "Junk"} or
    {"Common", "Uncommon", "Rare", "Legendary", "Junk"}

if not Jane.misprintize then
    assert(SMODS.load_file("src/misprintize.lua"))()
end

function Jane.can_use()
    return not (((G.play and #G.play.cards > 0) or
            (G.CONTROLLER.locked) or
            (G.GAME.STOP_USE and G.GAME.STOP_USE > 0)) and
        G.STATE ~= G.STATES.HAND_PLAYED and
        G.STATE ~= G.STATES.DRAW_TO_HAND and
        G.STATE ~= G.STATES.PLAY_TAROT)
end

function Jane.card_status_text(
    card,
    text,
    xoffset,
    yoffset,
    colour,
    size,
    delay,
    juice,
    jiggle,
    align,
    sound,
    volume,
    pitch,
    trigger,
    f
)
    if (delay or 0) <= 0 then
        if type(f) == "function" then
            f(card)
        end

        attention_text({
            text = text,
            scale = size or 1,
            hold = 0.7,
            backdrop_colour = colour or (G.C.FILTER),
            align = align or "bm",
            major = card,
            offset = {x = xoffset or 0, y = yoffset or (-0.05 * G.CARD_H)},
        })

        if sound then
            play_sound(sound, pitch or (0.9 + (0.2 * math.random())), volume or 1)
        end

        if juice then
            if type(juice) == "table" then
                card:juice_up(juice[1], juice[2])
            elseif type(juice) == "number" and juice ~= 0 then
                card:juice_up(juice, juice / 6)
            end
        end

        if jiggle then
            G.ROOM.jiggle = G.ROOM.jiggle + jiggle
        end
    else
        Jane.q(
            function()
                if f and type(f) == "function" then
                    f(card)
                end

                attention_text({
                    text = text,
                    scale = size or 1,
                    hold = 0.7 + (delay or 0),
                    backdrop_colour = colour or (G.C.FILTER),
                    align = align or "bm",
                    major = card,
                    offset = {x = xoffset or 0, y = yoffset or (-0.05 * G.CARD_H)},
                })

                if sound then
                    play_sound(sound, pitch or (0.9 + (0.2 * math.random())), volume or 1)
                end

                if juice then
                    if type(juice) == "table" then
                        card:juice_up(juice[1], juice[2])
                    elseif type(juice) == "number" and juice ~= 0 then
                        card:juice_up(juice, juice / 6)
                    end
                end

                if jiggle then
                    G.ROOM.jiggle = G.ROOM.jiggle + jiggle
                end
            end,
            delay,
            nil,
            trigger
        )
    end
end

function Jane.empowered()
    if Cryptid then
        add_tag(Tag("tag_cry_empowered"))
        return
    end

    local card = create_card("Spectral", G.consumeables, nil, nil, nil, nil, "c_soul", "acceleration_soul")
    card:set_edition({negative = true})
    card:add_to_deck()
    G.consumeables:emplace(card)
end

function Jane.get_chipmult_sum(chips, mult)
    chips = chips or 0
    mult = mult or 0

    local break_infinity = ((Talisman or {}).config_file or {}).break_infinity
    local big = (break_infinity == "" or break_infinity == nil) and function(x) return x end or to_big
    local op = Jane.get_operator()

    if op >= 3 then
        return big(chips) ^ big(mult)
    elseif op == 2 then
        return big(chips) * big(mult)
    end

    return big(chips) + big(mult)
end

function Jane.get_operator()
    if not G.GAME then
        return 0
    end

    G.GAME.operator = G.GAME.operator or 2
    return math.max(math.min(G.GAME.operator, 3), 1)
end

function Jane.hidden(card)
    return G.GAME and not G.GAME.obsidian and
        (type(card) == "table" and (card.name == "Black Hole" or card.name == "The Soul") or card.hidden)
end

function Jane.is_end_of_ante(context, card)
    return not context.individual and not
        context.repetition and not
        (card or {}).debuff and
        context.end_of_round and not
        context.blueprint and
        G.GAME.blind.boss and not
        (G.GAME.blind.config and G.GAME.blind.config.bonus)
end

function Jane.play_sound(sound, per, vol)
    Jane.q(function()
        play_sound(sound, per, vol)
    end)
end

function Jane.resize(card, mod, force_save)
    if force_save or not card.origsize then
        card.origsize = {w = card.T.w, h = card.T.h}
    end

    card:hard_set_T(card.T.x, card.T.y, card.T.w * mod, card.T.h * mod)
    remove_all(card.children)
    card.children = {}
    card.children.shadow = Moveable(0, 0, 0, 0)
    card:set_sprites(card.config.center, card.base.id and card.config.card)

    if card.area and
        ((G.shop_jokers and card.area == G.shop_jokers) or
            (G.shop_booster and card.area == G.shop_booster) or
            (G.shop_vouchers and card.area == G.shop_vouchers)) then
        create_shop_card_ui(card)
    end
end

function Jane.q(func, delay, timer, trigger, blockable, blocking)
    G.E_MANAGER:add_event(Event({
        delay = delay,
        timer = timer,
        trigger = (delay and not trigger) and "after" or trigger,
        blocking = blocking,
        blockable = blockable,
        func = function(...)
            local ret = func(...)
            return ret == nil and true or ret
        end,
    }))
end

function Card:gc()
    return (self.config or {}).center or {}
end

function Card:norank()
    return self.ability.name == "Stone Card" or self.config.center.no_rank
end

function Card:nosuit()
    return self.ability.name == "Stone Card" or self.config.center.no_suit
end

local orig_menu = Game.main_menu

function Game:main_menu(change_context)
    Jane.sinister = nil
    orig_menu(self, change_context)
end

local orig_update = Game.update

function Game:update(dt)
    local function delete_hardbans()
        if not Jane.config.disable_bans then
            for _, v in pairs(Jane.config.bans) do
                local e = SMODS.Center:get_obj(v)

                if e then
                    e:delete()
                end
            end
        end
    end

    local function hsv(h, s, v)
        if s <= 0 then
            return v, v, v
        end

        h = h * 6
        local c = v * s
        local x = (1 - math.abs((h % 2) - 1)) * c
        local m, r, g, b = (v - c), 0, 0, 0

        if h < 1 then
            r, g, b = c, x, 0
        elseif h < 2 then
            r, g, b = x, c, 0
        elseif h < 3 then
            r, g, b = 0, c, x
        elseif h < 4 then
            r, g, b = 0, x, c
        elseif h < 5 then
            r, g, b = x, 0, c
        else
            r, g, b = c, 0, x
        end

        return r + m, g + m, b + m
    end

    orig_update(self, dt)
    local ante = G.GAME.round_resets.ante
    local blind = get_blind_amount((ante >= 1 and ante <= 8) and math.floor(ante) or ante)
    G.P_BLINDS["bl_jane_wee"].mult = 22 / blind
    G.P_BLINDS["bl_jane_descending"].mult = math.floor(8 * math.sqrt(blind)) / blind
    Jane.update_honey()

    if not Jane.bans_done then
        Jane.bans_done = true
        delete_hardbans()
    end

    if (G.GAME or {}).banned_keys then
        for _, v in pairs(Jane.config.bans) do
            G.GAME.banned_keys[v] = true
        end
    end

    if G.ARGS.LOC_COLOURS then
        local r, g, b = hsv(self.C.jane_RGB_HUE / 360, .5, 1)
        self.C.jane_RGB[1] = r
        self.C.jane_RGB[3] = g
        self.C.jane_RGB[2] = b
        self.C.jane_RGB_HUE = (self.C.jane_RGB_HUE + 0.5) % 360
        G.ARGS.LOC_COLOURS.jane_RGB = self.C.jane_RGB
    end
end

SMODS.Atlas {
    px = 34,
    py = 34,
    key = "modicon",
    path = "almanac_avatar.png",
}

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

for _, v in ipairs({
    "back",
    "blind",
    "booster",
    "edition",
    "joker",
    "slugcat",
    "spectral",
    "tarot",
    "token",
    "voucher",
}) do
    assert(SMODS.load_file("src/" .. v .. ".lua"))()
end
