[manifest]
version = "1.0.0"
dump_lua = true
priority = -10

#========================================================#
# Choose any rank for custom deck and use provided atlas #
#========================================================#
[[patches]]
[patches.regex]
target = "functions/misc_functions.lua"
pattern = '''if _front and _front.suit and \(_front.value == 'Jack' or _front.value == 'Queen' or _front.value == 'King'\) then([\s\S]*?)end([\s\S]*?)end([\s\S]*?)end'''
position = "at"
payload = '''
if _front and _front.suit and G.SETTINGS.CUSTOM_DECK and G.SETTINGS.CUSTOM_DECK.Collabs then
    local collab = G.SETTINGS.CUSTOM_DECK.Collabs[_front.suit]
    if collab then
        local deckSkin = SMODS.DeckSkins[collab]
        if deckSkin then
            if deckSkin.outdated then
                local hasRank = false
                for i = 1, #deckSkin.ranks do
                    if deckSkin.ranks[i] == _front.value then hasRank = true break end
                end
                if hasRank then
                    local atlas = G.ASSET_ATLAS[G.SETTINGS.colour_palettes[_front.suit] == 'hc' and deckSkin.hc_atlas or deckSkin.lc_atlas]
                    if atlas then
                        if deckSkin.pos_style == 'collab' then
                            return atlas, G.COLLABS.pos[_front.value]
                        elseif deckSkin.pos_style == 'suit' then
                            return atlas, { x = _front.pos.x, y = 0}
                        elseif deckSkin.pos_style == 'deck' then
                            return atlas, _front.pos
                        elseif deckSkin.pos_style == 'ranks' or nil then
                            for i, rank in ipairs(deckSkin.ranks) do
                                if rank == _front.value then
                                    return atlas, { x = i - 1, y = 0}
                                end
                            end
                        end
                    end
                end
                return G.ASSET_ATLAS[G.SETTINGS.colour_palettes[_front.suit] == 'hc' and _front.hc_atlas or _front.lc_atlas or {}] or G.ASSET_ATLAS[_front.atlas] or G.ASSET_ATLAS["cards_"..(G.SETTINGS.colour_palettes[_front.suit] == 'hc' and 2 or 1)], _front.pos
            else
                local palette = deckSkin.palette_map and deckSkin.palette_map[G.SETTINGS.colour_palettes[_front.suit] or ''] or (deckSkin.palettes or {})[1]
                local hasRank = false
                for i = 1, #palette.ranks do
                    if palette.ranks[i] == _front.value then hasRank = true break end
                end
                if hasRank then
                    local atlas = G.ASSET_ATLAS[palette.atlas]
                    if type(palette.pos_style) == "table" then
                        if palette.pos_style[_front.value] then
                            if palette.pos_style[_front.value].atlas then
                                atlas = G.ASSET_ATLAS[palette.pos_style[_front.value].atlas]
                            end
                            if palette.pos_style[_front.value].pos then
                                return atlas, palette.pos_style[_front.value].pos
                            end
                        elseif palette.pos_style.fallback_style then
                            if palette.pos_style.fallback_style == 'collab' then
                                return atlas, G.COLLABS.pos[_front.value]
                            elseif palette.pos_style.fallback_style == 'suit' then
                                return atlas, { x = _front.pos.x, y = 0}
                            elseif palette.pos_style.fallback_style == 'deck' then
                                return atlas, _front.pos
                            end
                        end
                    elseif palette.pos_style == 'collab' then
                        return atlas, G.COLLABS.pos[_front.value]
                    elseif palette.pos_style == 'suit' then
                        return atlas, { x = _front.pos.x, y = 0}
                    elseif palette.pos_style == 'deck' then
                        return atlas, _front.pos
                    elseif palette.pos_style == 'ranks' or nil then
                        for i, rank in ipairs(palette.ranks) do
                            if rank == _front.value then
                                return atlas, { x = i - 1, y = 0}
                            end
                        end
                    end
                end
                return G.ASSET_ATLAS[palette.hc_default and _front.hc_atlas or _front.lc_atlas or {}] or G.ASSET_ATLAS[_front.atlas] or G.ASSET_ATLAS["cards_"..(palette.hc_default and 2 or 1)], _front.pos
            end
        end
    end
end
'''

[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = "{n=G.UIT.O, config={object = face_cards}}"
position = "at"
payload = "{n=G.UIT.O, config={object = G.cdds_cards}}"
match_indent = true
overwrite = false

[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = "table.insert(t, create_toggle({label = localize('b_high_contrast_cards'), ref_table = G.SETTINGS, ref_value = 'colourblind_option', callback = G.FUNCS.refresh_contrast_mode}))"
position = "at"
payload = '''
local deckskin_key = G.COLLABS.options[_suit][current_option]

local palette_loc_options = SMODS.DeckSkin.get_palette_loc_options(deckskin_key, _suit)

local selected_palette = 1
for i, v in ipairs(G.COLLABS.colour_palettes[deckskin_key]) do
    if G.SETTINGS.colour_palettes[_suit] == v then
        selected_palette = i
    end
end

table.insert(t,
    {n=G.UIT.R, config={align = "cm", id = 'palette_selector'}, nodes={
      create_option_cycle({options = palette_loc_options, w = 5.5, cycle_shoulders = false, curr_suit = _suit, curr_skin = deckskin_key, opt_callback = 'change_colour_palette', current_option = selected_palette, colour = G.C.ORANGE, focus_args = {snap_to = true, nav = 'wide'}}),
    }}
  )
'''
match_indent = true
overwrite = false

#=======================#
# Extend custom deck ui #
#=======================#
[[patches]]
[patches.regex]
target = "functions/UI_definitions.lua"
pattern = '''local face_cards = CardArea\(([\s\S]*?)\)'''
position = "at"
payload = '''
local rankCount = 0
local lookup = {}
for i, s in ipairs(SMODS.Suit:obj_list(true)) do
    local options = G.COLLABS.options[s.key]
    for i = 1, #options do
        local skin = SMODS.DeckSkins[options[i]]
        if skin.palettes and not (skin.display_ranks or skin.ranks) then
            for _, p in ipairs(skin.palettes) do
                local p_ranks = p.display_ranks or p.ranks
                for j = 1, #p_ranks do
                    if not lookup[p_ranks[j]] then
                        lookup[p_ranks[j]] = true
                        rankCount = rankCount + 1
                    end
                end
            end
        elseif not skin.palettes and (skin.display_ranks or skin.ranks) then
            local ranks = skin.display_ranks or skin.ranks
            for j = 1, #ranks do
                if not lookup[skin.ranks[j]] then
                    lookup[skin.ranks[j]] = true
                    rankCount = rankCount + 1
                end
            end
        end

    end
end

G.cdds_cards = CardArea(
    0,0,
    math.min(math.max(rankCount*G.CARD_W*0.6, 4*G.CARD_W), 10*G.CARD_W),
    1.4*G.CARD_H,
    {card_limit = rankCount, type = 'title', highlight_limit = 0})

G.cdds_cards.rankCount = rankCount

'''

[[patches]]
[patches.regex]
target = "functions/UI_definitions.lua"
pattern = '''for i = 1, 3 do([\s\S]*?)end'''
position = "at"
payload = '''
G.FUNCS.update_collab_cards(current_option, _suit, true)
'''

[[patches]]
[patches.regex]
target = "functions/UI_definitions.lua"
pattern = '''function create_UIBox_customize_deck()([\s\S]*?)end'''
position = "at"
payload = '''
function create_UIBox_customize_deck()
  local suitTabs = {}

  local index = 1
  for i, suit in ipairs(SMODS.Suit:obj_list(true)) do
    if G.COLLABS.options[suit.key] then
        suitTabs[index] = {
                    label = localize(suit.key, 'suits_plural'),
                    tab_definition_function = G.UIDEF.custom_deck_tab,
                    tab_definition_function_args = suit.key
                }
        index = index + 1
    end
  end

  if suitTabs[1] then
    suitTabs[1].chosen = true
  end

  local t = create_UIBox_generic_options({ back_func = 'options', snap_back = nil, contents = {
    {n=G.UIT.R, config={align = "cm", padding = 0}, nodes={
      create_tabs(
        {tabs = suitTabs, snap_to_nav = true, no_shoulders = true}
    )}}}
  })

  return t
end
'''

[[patches]]
[patches.pattern]
target = "functions/common_events.lua"
pattern = '''
if specific_vars.nominal_chips then
    localize{type = 'other', key = 'card_chips', nodes = desc_nodes, vars = {specific_vars.nominal_chips}}
end
if specific_vars.bonus_chips then
    localize{type = 'other', key = 'card_extra_chips', nodes = desc_nodes, vars = {specific_vars.bonus_chips}}
end
'''
position = "at"
payload = '''
if card.area == G.cdds_cards and card.generate_ds_card_ui and type(card.generate_ds_card_ui) == 'function' and card.deckskin and card.palette then
    card.generate_ds_card_ui(card, card.deckskin, card.palette, info_queue, desc_nodes, specific_vars, full_UI_table)
else
    if specific_vars.nominal_chips then
        localize{type = 'other', key = 'card_chips', nodes = desc_nodes, vars = {specific_vars.nominal_chips}}
    end
    if specific_vars.bonus_chips then
        localize{type = 'other', key = 'card_extra_chips', nodes = desc_nodes, vars = {specific_vars.bonus_chips}}
    end
end
'''
match_indent = true

#=======================#
# DeckSkin Crediting UI #
#=======================#
[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = '''
label = "Production",
chosen = true,
'''
position = "at"
payload = '''
label = "Production",
chosen = not SMODS.init_collab_credits,
'''
match_indent = true

[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = '''label = "Collabs",'''
position = "after"
payload = '''chosen = SMODS.init_collab_credits,'''
match_indent = true