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

# function G.UIDEF.challenge_list_page()
[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = "local challenge_unlocked = G.PROFILES[G.SETTINGS.profile].challenges_unlocked and (G.PROFILES[G.SETTINGS.profile].challenges_unlocked >= k)"
position = 'at'
payload = "local challenge_unlocked = SMODS.challenge_is_unlocked(v, k)"
match_indent = true

# function G.UIDEF.challenges() - fix challenge unlock count
[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = """
local _ch_tab = {comp = _ch_comp, unlocked = G.PROFILES[G.SETTINGS.profile].challenges_unlocked}
"""
position = 'at'
payload = """
local unlock_count = 0
for k, v in ipairs(G.CHALLENGES) do
    if SMODS.challenge_is_unlocked(v, k) then
        unlock_count = unlock_count + 1
    end
end
local _ch_tab = {comp = _ch_comp, unlocked = unlock_count}
"""
match_indent = true

# Add button colour
[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = "UIBox_button({id = k, col = true, label = {challenge_unlocked and localize(v.id, 'challenge_names') or localize('k_locked'),}, button = challenge_unlocked and 'change_challenge_description' or 'nil', colour = challenge_unlocked and G.C.RED or G.C.GREY, minw = 4, scale = 0.4, minh = 0.6, focus_args = {snap_to = not snapped}}),"
position = 'at'
match_indent = true
payload = '''
UIBox_button({id = k, col = true, label = {challenge_unlocked and localize(v.id, 'challenge_names') or localize('k_locked'),}, button = challenge_unlocked and 'change_challenge_description' or 'nil', colour = challenge_unlocked and (v.button_colour or G.C.RED) or G.C.GREY, minw = 4, scale = 0.4, minh = 0.6, focus_args = {snap_to = not snapped}}),
'''

[[patches]]
[patches.pattern]
target = "game.lua"
pattern = '''if _ch.restrictions.banned_cards then'''
position = "after"
payload = '''   if type(_ch.restrictions.banned_cards) == 'function' then
        _ch.restrictions.banned_cards = _ch.restrictions.banned_cards()
	end'''
match_indent = true
times = 1

[[patches]]
[patches.pattern]
target = "game.lua"
pattern = '''if _ch.restrictions.banned_tags then'''
position = "after"
payload = '''
    if type(_ch.restrictions.banned_tags) == 'function' then
        _ch.restrictions.banned_tags = _ch.restrictions.banned_tags()
    end'''
match_indent = true
times = 1

[[patches]]
[patches.pattern]
target = "game.lua"
pattern = '''if _ch.restrictions.banned_other then'''
position = "after"
payload = '''   
    if type(_ch.restrictions.banned_other) == 'function' then
        _ch.restrictions.banned_other = _ch.restrictions.banned_other()
    end'''
match_indent = true
times = 1

# apply
[[patches]]
[patches.pattern]
target = "game.lua"
pattern = '''
local _ch = args.challenge
'''
position = "after"
payload = '''
if _ch.apply and type(_ch.apply) == "function" then
    _ch:apply()
end
'''
match_indent = true