[manifest]
version = "1.0.0"
dump_lua = true
priority = 69

#
# Vanilla patches
#

# Overwrite how suits are added
 
[[patches]] 
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = "table.insert(SUITS[v.base.suit], v)"
position = "at"
payload = '''
local greyed
if unplayed_only and not ((v.area and v.area == G.deck) or v.ability.wheel_flipped) then
  greyed = true
end
local card_string = v:cart_to_string {deck_view = true}
if greyed then
    card_string = card_string .. "Greyed"
end

if greyed and Cartomancer.SETTINGS.deck_view_hide_drawn_cards then
  -- Ignore this card.
elseif not SUITS[v.base.suit][card_string] then
  table.insert(SUITS_SORTED[v.base.suit], card_string)

  local _scale = 0.7
  local copy = copy_card(v, nil, _scale)

  copy.greyed = greyed
  copy.stacked_quantity = 1

  SUITS[v.base.suit][card_string] = copy
else
  local stacked_card = SUITS[v.base.suit][card_string]
  stacked_card.stacked_quantity = stacked_card.stacked_quantity + 1
end'''
match_indent = true

[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = '''
    if SUITS[suit_map[j]][1] then
      local view_deck = CardArea('''
position = "at"
payload = '''
    if SUITS_SORTED[suit_map[j]][1] then
      local view_deck = CardArea('''
match_indent = true
 
[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = "local suit_map = {'Spades', 'Hearts', 'Clubs', 'Diamonds'}"
position = "after"
payload = "local SUITS_SORTED = Cartomancer.tablecopy(SUITS)"
match_indent = true

[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = "local suit_tallies = {['Spades']  = 0, ['Hearts'] = 0, ['Clubs'] = 0, ['Diamonds'] = 0}"
position = "before"
payload = '''
-----------------------------------------------------------
-- Add empty card area to view deck to fix a visual issue with missing deck.
if not next(deck_tables) then
  local view_deck = CardArea(
    G.ROOM.T.x + 0.2*G.ROOM.T.w/2,G.ROOM.T.h,
    6.5*G.CARD_W,
    0.6*G.CARD_H,
    {card_limit = 1, type = 'title', view_deck = true, highlight_limit = 0, card_w = G.CARD_W*0.7, draw_layers = {'card'}})

  table.insert(
    deck_tables, 
    {n=G.UIT.R, config={align = "cm", padding = 0}, nodes={
      {n=G.UIT.O, config={object = view_deck}}
    }}
  )
end
-----------------------------------------------------------
'''
match_indent = true

[[patches]]
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = "{card_limit = #SUITS[suit_map[j]], type = 'title', view_deck = true, highlight_limit = 0, card_w = G.CARD_W*0.7, draw_layers = {'card'}})"
position = "at"
payload = "{card_limit = #SUITS_SORTED[suit_map[j]], type = 'title', view_deck = true, highlight_limit = 0, card_w = G.CARD_W*0.7, draw_layers = {'card'}})"
match_indent = true

# Add unique count

[[patches]] 
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = '''modded and {n=G.UIT.R, config={align = "cm"}, nodes={'''
position = "before"
payload = "not unplayed_only and Cartomancer.add_unique_count() or nil,"
match_indent = true

# Overwrite cards copy and display code

#Note for self: multi line pattern patches only work with original spacing.
[[patches]] 
[patches.pattern]
target = "functions/UI_definitions.lua"
pattern = '''
      for i = 1, #SUITS[suit_map[j]] do
        if SUITS[suit_map[j]][i] then
          local greyed, _scale = nil, 0.7
          if unplayed_only and not ((SUITS[suit_map[j]][i].area and SUITS[suit_map[j]][i].area == G.deck) or SUITS[suit_map[j]][i].ability.wheel_flipped) then
            greyed = true
          end
          local copy = copy_card(SUITS[suit_map[j]][i],nil, _scale)
          copy.greyed = greyed
          copy.T.x = view_deck.T.x + view_deck.T.w/2
          copy.T.y = view_deck.T.y

          copy:hard_set_T()
          view_deck:emplace(copy)
        end
      end'''
position = "at"
payload = '''
      for i = 1, #SUITS_SORTED[suit_map[j]] do
        local card_string = SUITS_SORTED[suit_map[j]][i]
        local card = SUITS[suit_map[j]][card_string]
      
        card.T.x = view_deck.T.x + view_deck.T.w/2
        card.T.y = view_deck.T.y
        card:create_quantity_display()
      
        card:hard_set_T()
        view_deck:emplace(card)
      
      end
'''
match_indent = true