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

SMODS.Sound({key = "draw", path = "draw.ogg"})

local function conjure(card, number)
    for _ = 1, math.min(
        math.ceil(card.ability.extra.spectrals) * number,
        G.consumeables.config.card_limit - #G.consumeables.cards
    ) do
        G.E_MANAGER:add_event(Event({
            trigger = "after",
            delay = 0.4,
            func = function()
                if G.consumeables.config.card_limit <= #G.consumeables.cards then
                    return true
                end

                play_sound("jane_draw")
                local spectral = create_card("Spectral", G.consumeables, nil, nil, nil, nil, nil, "pri")
                spectral:add_to_deck()
                G.consumeables:emplace(spectral)
                card:juice_up(0.3, 0.5)
                return true
            end
        }))
    end
end

local function createfulldeck(enhancement, edition, amount, emplacement)
    local cards = {}

    for _, v in pairs(G.P_CARDS) do
        for i = 1, (amount or 1) do
            G.E_MANAGER:add_event(Event({
                delay = 0.1,
                func = function()
                    cards[i] = true
                    G.playing_card = (G.playing_card and G.playing_card + 1) or 1

                    local card = Card(
                        G.play.T.x + G.play.T.w / 2,
                        G.play.T.y, G.CARD_W, G.CARD_H,
                        v,
                        enhancement or G.P_CENTERS.c_base,
                        {playing_card = G.playing_card}
                    )

                    if edition then
                        card:set_edition(type(edition) == "table" and edition or {[edition] = true}, true, true)
                    end

                    play_sound("card1")
                    table.insert(G.playing_cards, card)
                    card:add_to_deck()

                    if emplacement then
                        emplacement:emplace(card)
                    else
                        G.deck:emplace(card)
                    end

                    return true
                end
            }))
        end
    end

    Jane.q(function()
        if next(cards) then
            playing_card_joker_effects(cards)
        end

        return true
    end)

    Jane.q(function()
        cards = nil
        return true
    end)
end

local function randomize(targets, noanim)
    if #targets <= 0 then
        return
    end

    if noanim then
        for i=1, #targets do
            local card = targets[i]
            card:set_base(pseudorandom_element(G.P_CARDS))

            card:set_ability(
                pseudorandom(pseudoseed("chancetime")) > 1 / (#G.P_CENTER_POOLS["Enhanced"] + 1) and
                pseudorandom_element(G.P_CENTER_POOLS["Enhanced"], pseudoseed("spectral_chance")) or
                G.P_CENTERS["c_base"]
            )

            local edition_rate = 2
            card:set_edition(poll_edition("standard_edition" .. G.GAME.round_resets.ante, edition_rate, true), true, true)
            local seal_rate = 10
            local seal_poll = pseudorandom(pseudoseed("stdseal" .. G.GAME.round_resets.ante))

            if seal_poll > 1 - 0.02 * seal_rate then
                local seal_type = pseudorandom(pseudoseed("stdsealtype" .. G.GAME.round_resets.ante))
                local seal_list = {}

                for k, _ in pairs(G.P_SEALS) do
                    table.insert(seal_list, k)
                end

                seal_type = math.floor(seal_type * #seal_list)
                card:set_seal(seal_list[seal_type], true, true)
            else
                card:set_seal(nil, true, true)
            end

            card:juice_up(0.3, 0.3)
        end
    else
        for i = 1, #targets do
            local percent = 1.15 - (i - 0.999) / (#G.hand.cards - 0.998) * 0.3

            G.E_MANAGER:add_event(Event({
                trigger = "after",
                delay = 0.15,
                func = function()
                    targets[i]:flip()
                    play_sound("card1", percent)
                    targets[i]:juice_up(0.3, 0.3)
                    return true
                end
            }))
        end

        delay(0.2)

        for i = 1, #targets do
            local percent = 0.85 + (i - 0.999) / (#G.hand.cards - 0.998) * 0.3

            G.E_MANAGER:add_event(Event({
                trigger = "after",
                delay = 0.1,
                func = function()
                    local card = targets[i]
                    card:set_base(pseudorandom_element(G.P_CARDS))
                    card:set_ability(pseudorandom_element(G.P_CENTER_POOLS["Enhanced"]))

                    local edition_rate = 2
                    card:set_edition(poll_edition("standard_edition" .. G.GAME.round_resets.ante, edition_rate, true))
                    local seal_rate = 10
                    local seal_poll = pseudorandom(pseudoseed("stdseal" .. G.GAME.round_resets.ante))

                    if seal_poll > 1 - 0.02 * seal_rate then
                        local seal_type = pseudorandom(pseudoseed("stdsealtype" .. G.GAME.round_resets.ante))
                        local seal_list = {}

                        for k, _ in pairs(G.P_SEALS) do
                            table.insert(seal_list, k)
                        end

                        seal_type = math.floor(seal_type * #seal_list)
                        card:set_seal(seal_list[seal_type])
                    else
                        card:set_seal()
                    end

                    card:flip()
                    play_sound("card3", percent, 0.6)
                    card:juice_up(0.3, 0.3)
                    return true
                end
            }))
        end
    end
end

SMODS.Consumable {
    key = "obfuscation",
    atlas = "janeacc",
    set = "Spectral",
    loc_txt = {
        name = "Obfuscation",
        text = {
            "{C:green,E:1}Randomises{} all cards in hand",
            "{C:inactive}(Rank, seal, edition,",
            "{C:inactive}enhancement, and suit)",
        }
    },
    pos = {x = 0, y = 4},
    cost = 4,
    can_use = function(_, _)
        return Jane.can_use() and #((G.hand or {}).cards or {}) > 0
    end,
    use = function(_, card, _, _)
        G.E_MANAGER:add_event(Event({
            trigger = "after",
            delay = 0.4,
            func = function()
                play_sound("tarot1")
                card:juice_up(0.3, 0.5)
                return true
            end
        }))

        randomize(G.hand.cards)
        delay(0.5)
    end
}

SMODS.Consumable {
    key = "conjure",
    set = "Spectral",
    atlas = "janeacc",
    loc_txt = {
        name = "Conjure",
        text = {
            "Creates {C:attention}#1#",
            "{C:spectral}Spectral{} cards",
            "{C:inactive}(Must have room)",
        }
    },
    config = {extra = {spectrals = 2}},
    pos = {x = 2, y = 4},
    cost = 4,
    loc_vars = function(self, info_queue, center)
        return {vars = {math.ceil(center.ability.extra.spectrals)}}
    end,
    can_use = Jane.can_use,
    use = function(_, card, _, _)
        conjure(card, 1)
        delay(0.6)
    end,
    bulk_use = function(_, card, _, _, number)
        conjure(card, number)
        delay(0.6)
    end
}

SMODS.Consumable {
    key = "shadows",
    set = "Spectral",
    atlas = "janeacc",
    loc_txt = {
        name = "Shadows",
        text = {
            "Create {C:attention}#1#{} {C:green}random {C:dark_edition}Negative",
            "{C:attention}Perishable {C:attention}Jokers{}, set",
            "{C:money}sell value{} of {C:attention}all Jokers{} to {C:money}$0",
        }
    },
    config = {extra = {shadows = 2}},
    pos = {x = 3, y = 4},
    cost = 4,
    loc_vars = function(_, _, center)
        return {vars = {((center.ability or {}).extra or {}).shadows or 2}}
    end,
    can_use = Jane.can_use,
    use = function(_, card, _, _)
        for _ = 1, card.ability.extra.shadows do
            local joker = create_card("Joker", G.jokers, nil, nil, nil, nil, nil, "phantom")
            joker:set_edition({negative = true})
            joker.ability.eternal = false
            joker.ability.perishable = true
            joker.ability.perish_tally = 5
            joker:add_to_deck()
            G.jokers:emplace(joker)
        end

        delay(0.6)

        for i = 1, #G.jokers.cards do
            G.jokers.cards[i].base_cost = 0
            G.jokers.cards[i].extra_cost = 0
            G.jokers.cards[i].cost = 0
            G.jokers.cards[i].sell_cost = 0
            G.jokers.cards[i].sell_cost_label = G.jokers.cards[i].facing == "back" and "?" or G.jokers.cards[i].sell_cost
        end
    end,
    bulk_use = function(_, card, _, _, number)
        for _ = 1, card.ability.extra.shadows * number do
            local joker = create_card("Joker", G.jokers, nil, nil, nil, nil, nil, "phantom")
            joker.no_forced_edition = true
            joker:set_edition({negative = true})
            joker.no_forced_edition = nil
            joker.ability.eternal = false
            joker.ability.perishable = true
            joker.ability.perish_tally = 5
            joker:add_to_deck()
            G.jokers:emplace(joker)
        end

        delay(0.6)

        for i = 1, #G.jokers.cards do
            G.jokers.cards[i].base_cost = 0
            G.jokers.cards[i].extra_cost = 0
            G.jokers.cards[i].cost = 0
            G.jokers.cards[i].sell_cost = 0
            G.jokers.cards[i].sell_cost_label = G.jokers.cards[i].facing == "back" and "?" or G.jokers.cards[i].sell_cost
        end
    end
}


SMODS.Consumable {
    key = "reverse_hanged_man",
    atlas = "janertarots",
    set = "Spectral",
    loc_txt = {
        name = "Zen",
        text = {
            "{C:attention}Reset{} your deck to",
            "a {C:attention}standard 52-card deck"
        }
    },
    config = {extra = {destruction = 0.5}},
    pos = {x = 7, y = 1},
    cost = 15,
    loc_vars = function(_, _, center)
        return {vars = {
            math.min(100, center.ability.extra.destruction * 100),
            math.ceil(#(G.playing_cards or {}) * center.ability.extra.destruction)
        }}
    end,
    can_use = Jane.can_use,
    use = function(_, _, _, _)
        Jane.q(function()
            for _, v in pairs(G.playing_cards) do
                v:start_dissolve()
            end

            return true
        end)

        local function realdelay(time, queue)
            G.E_MANAGER:add_event(Event({
                trigger = "after",
                timer = "REAL",
                delay = time or 1,
                func = function()
                   return true
                end
            }), queue)
        end

        realdelay(1)
        createfulldeck()
    end
}
