for _, v in pairs({"7granddad", "betmma", "honey", "oxy", "maxie", "peppino"}) do
    SMODS.Atlas {
        px = 71,
        py = 95,
        key = "jane" .. v,
        path = Jane.config.texture_pack .. "/j_jane_" .. v .. ".png",
    }
end

for i = 1, 6 do
    SMODS.Sound({key = "grand" .. i, path = "grand_dad" .. i .. ".ogg"})
end

local exotic = Cryptid and "cry_exotic" or 4

local food = {
    "j_gros_michel",
    "j_egg",
    "j_ice_cream",
    "j_cavendish",
    "j_turtle_bean",
    "j_diet_cola",
    "j_popcorn",
    "j_ramen",
    "j_selzer",
    "j_cry_pickle",
    "j_cry_chili_pepper",
    "j_cry_oldcandy",
    "j_cry_caramel",
    "j_cry_foodm",
    "j_cry_cotton_candy",
    "j_cry_wrapped",
    "j_cry_candy_cane",
    "j_cry_candy_buttons",
    "j_cry_jawbreaker",
    "j_cry_mellowcreme",
    "j_cry_brittle",
    "j_jane_peppino",
}

local granddad_palette = {
    HEX("155fd9"),
    HEX("ff8170"),
    HEX("ffffff"),
    HEX("6c0700")
}

local function food_jokers_count()
    if not G.jokers then
        return 0
    end

    local amount = 0

    for _, v in pairs(food) do
        amount = amount + #SMODS.find_card(v)
    end

    return amount
end

local function grand_dad(card)
    if ((Talisman or {}).config_file or {}).disable_anims then
        return
    end

    Jane.q(function()
        card:juice_up(0.5, 0.5)
        return true
    end)

    local rnd = math.random(6)
    local obj = card.edition or {}

    Jane.play_sound(
        "jane_grand" .. rnd,
        obj.jane_jumbo and (1 / Jane.config.wee_sizemod) or 1,
        0.5
    )

    Jane.card_status_text(
        card,
        rnd == 2 and "Flintstones?!" or rnd == 6 and "Gruhh- Dad!" or "Grand Dad!",
        nil,
        0.05 * card.T.h,
        granddad_palette[math.random(#granddad_palette)],
        0.6,
        0.6,
        nil,
        nil,
        "bm"
    )
end

local function voucher_count()
    if not G.GAME.used_vouchers then
        return 0
    end

    local count = 0

    for _, v in pairs(G.GAME.used_vouchers) do
        if v then
            count = count + 1
        end
    end

    return count
end

local maxie_quotes = {
    normal = {
        "Hey! I hope we can become great friends together!",
        "Need a paw?",
        "Together as a team!",
        "Mmm... Milk sounds good right about now.",
        "Unlike most other bunnies; I don't like carrots.",
        "Mmm... Mac and Cheese...",
        "I am just sitting here.",
    },
    scared = {
        "A-ah! S-stop it, please!! You're scaring me...!",
        "E-eek! B-be careful!!",
    },
}

SMODS.Joker {
    key = "maxie",
    atlas = "janemaxie",
    loc_txt = {
        name = "Maxie",
        text = {
            "{C:attention}+#1# {}booster pack slot#2#",
            "{C:inactive,s:0.75,E:1}#3#{C:red,s:1.5,E:1}#4#",
        }
    },
    config = {extra = {choices = Cryptid and 2 or 1}},
    pos = {x = 0, y = 0},
    sinis = {x = 2, y = 0},
    soul_pos = {x = 1, y = 0},
    cost = 6,
    rarity = 2,
    loc_vars = function(_, _, card)
        return {vars = {
            card.ability.extra.choices,
            card.ability.extra.choices == 1 and "" or "s",
            Jane.sinister and "" or pseudorandom_element(maxie_quotes.normal, pseudoseed("MaxieQuotes")),
            Jane.sinister and pseudorandom_element(maxie_quotes.scared, pseudoseed("MaxieQuotes")) or "",
        }}
    end,
    calculate = function(_, card, context)
        if context.starting_shop then
            for _ = 1, card.ability.extra.choices do
                SMODS.add_booster_to_shop()
            end
        end
    end
}

SMODS.Joker {
    key = "honey",
    atlas = "janehoney",
    loc_txt = {
        name = "Honey{C:dark_edition}#1#",
        text = {
            "{C:attention}Common#2#{}#3#",
            "#4#cannot appear",
            "#5#{C:attention}#6#",
            "{C:dark_edition}#7#{}#8#{C:inactive,s:0.75,E:1}#9#",
            "{C:inactive,s:0.75,E:1}#10#{C:red,s:1.5,E:1}#11#",
        }
    },
    config = {extra = {is_corrupted = false}},
    pos = {x = 0, y = 0},
    sinis = {x = 2, y = 0},
    soul_pos = {x = 1, y = 0},
    cost = 8,
    rarity = 3,
    add_to_deck = function(_, _, _)
        local honey = SMODS.find_card("j_jane_honey")

        for _, v in ipairs(honey) do
            if not v.ability.extra.is_corrupted and v ~= honey[#honey] then
                v.sell_cost = v.sell_cost + honey[#honey].sell_cost
                v.ability.extra.is_corrupted = true
                honey[#honey]:start_dissolve()
                honey[#honey] = nil
            end
        end
    end,
    loc_vars = function(_, _, card)
        local is_corrupted = card.ability.extra.is_corrupted

        return {vars = {
            is_corrupted and " (Corrupted)" or "",
            is_corrupted and " and Uncommon" or "",
            is_corrupted and "" or " jokers",
            is_corrupted and "jokers " or "",
            is_corrupted and "" or "Getting another ",
            is_corrupted and "" or "Honey",
            is_corrupted and "" or "corrupts ",
            is_corrupted and "" or "this Joker",
            Jane.sinister and "" or (is_corrupted and "*Her eyes are looking around," or ""),
            Jane.sinister and "" or (is_corrupted and "as if she wants to say something...*" or "Buzzzzz! I'll do my best!"),
            Jane.sinister and (is_corrupted and "..." or "S-STOP THAT!! YOU'RE FREAKING ME OOOUT!!!") or "",
        }}
    end,
    update = function(_, card, _)
        if card.added_to_deck and card.children.center and card.children.floating_sprite then
            local extra = card.ability.extra or {}
            local y = extra.is_corrupted and 1 or 0
            card.children.center:set_sprite_pos({x = 0, y = y})
            card.children.floating_sprite:set_sprite_pos({x = Jane.sinister and 2 or 1, y = y})
        end
    end,
}

if Cryptid then
    Cryptid.aliases["honey"] = "j_jane_honey"
end

local orig_rarity = SMODS.poll_rarity

function SMODS.poll_rarity(_pool_key, _rand_key)
    local is_corrupted

    for _, v in pairs(SMODS.find_card("j_jane_honey")) do
        is_corrupted = is_corrupted or v.ability.extra.is_corrupted
    end

    local rarity = orig_rarity(_pool_key, _rand_key)
    return (is_corrupted and (rarity == 1 or rarity == 2)) and 3 or ((is_corrupted ~= nil and rarity == 1) and 2 or rarity)
end

local oxy_quotes = {
    normal = {
        "YOU WILL LOSE",
        "DON'T EVEN TRY",
        "WE WILL STOP YOU",
        "He glares at the blinds with conviction.",
    },
    scared = {
        "...H-how are you doing t-that...?",
        "This is... unsettling...",
        "S-... So much power...",
    }
}

SMODS.Joker {
    key = "oxy",
    atlas = "janeoxy",
    loc_txt = {
        name = "Oxy{C:dark_edition}#1#",
        text = {
            "{C:attention}Scored steel {}cards give",
            "{X:mult,C:white}x#2#{} {C:mult}Mult {}and {X:chips,C:white}x#2#{} {C:chips}Chips",
            "{C:dark_edition,E:1}#3#{}#4#{C:red}#5#{C:attention}#6#",
            "{C:dark_edition,E:1}#7#{}#8#",
            "#9#{C:dark_edition,E:1}#10#",
            "{C:inactive,s:0.75,E:1}#11#{C:red,s:1.5,E:1}#12#",
        }
    },
    config = {extra = {corrupted_steel = 2.5, is_corrupted = false, milestone = 3, progress = 0, steel = 1.5}},
    pos = {x = 0, y = 0},
    sinis = {x = 2, y = 0},
    soul_pos = {x = 1, y = 0},
    blueprint_compat = true,
    cost = Cryptid and 8 or 20,
    rarity = Cryptid and 3 or 4,
    loc_vars = function(_, _, card)
        local ability = card.ability.extra or {}
        local milestone = ability.milestone
        local progress = ability.progress
        local is_corrupted = ability.is_corrupted
        local amount = is_corrupted and ability.corrupted_steel or ability.steel
        local effective = is_corrupted and progress or milestone - progress
        local plural = effective == 1 and "" or "s"

        return {vars = {
            is_corrupted and " (Corrupted)" or "",
            amount,
            is_corrupted and "Purifies " or "",
            is_corrupted and "in " or "",
            is_corrupted and "" or "Destroying ",
            effective .. (is_corrupted and " round" or " steel card") .. plural,
            is_corrupted and "" or "corrupts ",
            is_corrupted and "Destroying steel cards" or "this Joker",
            is_corrupted and "prolongs the " or "",
            is_corrupted and "corruption" or "",
            Jane.sinister and "" or (is_corrupted and pseudorandom_element(oxy_quotes.normal, pseudoseed("OxyQuotes")) or "We all cut close..."),
            Jane.sinister and (is_corrupted and pseudorandom_element(oxy_quotes.scared, pseudoseed("OxyQuotes")) or "WHAT ARE YOU DOING DOWN THERE?!?") or "",
        }}
    end,
    update = function(_, card, _)
        if card.added_to_deck and card.children.center and card.children.floating_sprite then
            local extra = card.ability.extra or {}
            local y = extra.is_corrupted and 1 or 0
            card.children.center:set_sprite_pos({x = 0, y = y})
            card.children.floating_sprite:set_sprite_pos({x = Jane.sinister and 2 or 1, y = y})
        end
    end,
    calculate = function(_, card, context)
        local extra = card.ability.extra or {}
        local destroyed_steel = 0

        for _, v in pairs(context.removed or {}) do
            if (v.ability or {}).name == "Steel Card" then
                destroyed_steel = destroyed_steel + 1
            end
        end

        if destroyed_steel > 0 then
            extra.progress = extra.progress + destroyed_steel

            card_eval_status_text(
                card,
                "extra",
                nil,
                nil,
                nil,
                {
                    message = (extra.is_corrupted and "+" or "-") .. destroyed_steel,
                    colour = extra.is_corrupted and G.C.PURPLE or G.C.GREY
                }
            )
        end

        if context.end_of_round and
            extra.progress > 0 and not
            context.individual and not
            context.repetition and not
            context.retrigger_joker then
            extra.progress = extra.progress - 1

            card_eval_status_text(
                card,
                "extra",
                nil,
                nil,
                nil,
                {
                    message = extra.is_corrupted and "-1" or "+1",
                    colour = extra.is_corrupted and G.C.PURPLE or G.C.GREY
                }
            )
        end

        if not extra.is_corrupting and
            ((extra.progress >= extra.milestone and not extra.is_corrupted) or
            (extra.progress <= 0 and extra.is_corrupted)) then
            extra.is_corrupting = true

            G.E_MANAGER:add_event(Event({
                delay = 0.1,
                func = function()
                    card:flip()
                    play_sound("card1")
                    return true
                end
            }))

            G.E_MANAGER:add_event(Event({
                delay = 1,
                func = function()
                    card:flip()
                    card:juice_up(1, 1)
                    play_sound("card1")
                    extra.is_corrupted = not extra.is_corrupted
                    extra.is_corrupting = nil
                    return true
                end
            }))
        end

        if context.individual and
            context.cardarea == G.play and
            context.other_card.ability.name == "Steel Card" then
            local amount = extra.is_corrupted and extra.corrupted_steel or extra.steel
            return {x_chips = amount, x_mult = amount, colour = G.C.PURPLE, card = card}, true
        end
    end
}

if Cryptid then
    Cryptid.aliases["oxy"] = "j_jane_oxy"
end

local operator = Cryptid and "^" or "X"

SMODS.Joker {
    key = "betmma",
    atlas = "janebetmma",
    loc_txt = {
        name = "Betmma",
        text = {
            "{X:dark_edition,C:chips}+" .. operator .. "#1#{C:chips} Chips{} for every",
            "{C:attention}unique Voucher redeemed",
            "{C:inactive}(Currently {X:dark_edition,C:chips}" .. operator .. "#2#{C:inactive})",
        }
    },
    config = {big_num_scaler = true, extra = {tet = Cryptid and 0.1 or 0.5}},
    pos = {x = 0, y = 0},
    soul_pos = {x = 1, y = 0},
    blueprint_compat = true,
    cost = Cryptid and 20 or 8,
    rarity = Cryptid and 4 or 3,
    loc_vars = function(_, _, center)
        return {vars = {center.ability.extra.tet, 1 + (voucher_count() * center.ability.extra.tet)}}
    end,
    calculate = function(_, card, context)
        if not context.joker_main then
            return
        end

        local v = voucher_count()

        if v > 0 then
            local num = 1 + (v * card.ability.extra.tet)

            return {
                card = card,
                colour = G.C.jane_RGB,
                message = operator .. number_format(num),
                [Cryptid and "Echips_mod" or "Xchips_mod"] = num,
            }, true
        end
    end
}

SMODS.Joker {
    key = "7granddad",
    loc_txt = {
        name = "7 GRAND DAD",
        text = {
            "This Joker has a {C:jane_RGB,E:1}strange",
            "{C:jane_RGB,E:1}reaction {}to scored {C:attention}7{}s",
        }
    },
    config = {},
    pos = {x = 0, y = 0},
    soul_pos = {x = 1, y = 0},
    atlas = "jane7granddad",
    blueprint_compat = true,
    cost = Cryptid and 50 or 8,
    rarity = Cryptid and exotic or 3,
    loc_vars = function(_, _, center)
        return {vars = {center.ability.shopslots}}
    end,
    add_to_deck = function(_, card, _)
        grand_dad(card)
    end,
    remove_from_deck = function(_, card, _)
        grand_dad(card)
    end,
    calculate = function(_, card, context)
        if context.cardarea == G.play then
            local function scj(c)
                return c.cardarea and c.cardarea == G.play and not c.before and not c.after and not c.repetition
            end

            if context.other_card and context.other_card:get_id() == 7 and scj(context) then
                grand_dad(card)
                local palette = granddad_palette[math.random(#granddad_palette)]
                local rnd = pseudorandom(pseudoseed("granddad"), 1, Cryptid and 7 or 5)

                if rnd == 1 then
                    return {
                        message = "+777",
                        chips = 777,
                        colour = palette,
                        card = card
                    }, true
                elseif rnd == 2 then
                    return {
                        message = "+777 Mult",
                        mult = 777,
                        colour = palette,
                        card = card
                    }, true
                elseif rnd == 3 then
                    return {
                        message = "+$7",
                        dollars = 7,
                        colour = palette,
                        card = card
                    }, true
                elseif rnd == 4 then
                    return {
                        message = "x7",
                        x_chips = 7,
                        colour = palette,
                        card = card
                    }, true
                elseif rnd == 5 then
                    return {
                        message = "x7 Mult",
                        x_mult = 7,
                        colour = palette,
                        card = card
                    }, true
                elseif rnd == 6 then
                    return {
                        message = "^1.77",
                        e_chips = 1.77,
                        colour = palette,
                        card = card
                    }, true
                else
                    return {
                        message = "^1.77 Mult",
                        e_mult = 1.77,
                        colour = palette,
                        card = card
                    }, true
                end
            end
        end
    end
}

SMODS.Joker {
    key = "peppino",
    atlas = "janepeppino",
    loc_txt = {
        name = "Peppino Spaghetti",
        text = {
            "{X:dark_edition,C:red}" .. operator .. "2^x{C:red} Mult{} for every",
            "{C:attention}food or Peppino Joker",
            "in your possession",
            "{C:inactive}(Currently {X:dark_edition,C:red}" .. operator .. "#1#{C:red} Mult{C:inactive})",
        }
    },
    pos = {x = 0, y = 0},
    soul_pos = {x = 1, y = 0},
    rarity = exotic,
    blueprint_compat = true,
    cost = Cryptid and 50 or 20,
    loc_vars = function(_, _, _)
        return {vars = {2 ^ food_jokers_count()}}
    end,
    calculate = function(_, _, context)
        local count = food_jokers_count()

        if context.joker_main and count > 0 then
            return {
                colour = G.C.DARK_EDITION,
                message = operator .. 2 ^ count .. " Mult",
                [Cryptid and "Emult_mod" or "Xmult_mod"] = 2 ^ count,
            }, true
        end
    end
}
