local OpenTricks_Table = OpenTricks_Table
local OpenTricks_Table_Pending = OpenTricks_Table_Pending
local OpenTricks_Hooks = OpenTricks_Hooks

local function OpenTricks_Add_Complex(...)
  -- here be dragons. this'll give you better control over stuff, but prepare for breakage.
  local objects = {...}
  for k, v in pairs(objects)
    local final_properties = {}
    local final_hooks = {}
    for k2, v2 in pairs(v)
      if string.sub(k2, -4) == "Hook" -- get final four letters
        final_hooks[k2] = v2
      else
        final_properties[k2] = v2
      end
    end
    if #OpenTricks_Table_Pending == #OpenTricks_Hooks -- check to see if we've gained non-hooked trick styles from netplay.
      table.insert(OpenTricks_Table_Pending, final_properties) 
    end
    table.insert(OpenTricks_Hooks, final_hooks)
    OpenTricks_TableNeedsUpdate = true
  
    print("[OpenTricks]: " .. v.name .. " registered.")
  end
end

rawset(_G, "OPENTRICKS_ADD_COMPLEX", OpenTricks_Add_Complex)

local function OpenTricks_Add(name, hint, offIcon, onIcon, defaultBanned, chosenHook, thinkerHook, successHook, landingHook)
  -- name: string used as a key for stuff specific to your Trick Style, and for UI
  -- hint: string used for selection UI.
  -- icon: patch name for the associated graphic.
  
  -- All hooks use the player entity as the sole argument.
  -- chosenHook:  function called when the player's choice is locked in. Initialize stuff here.
  -- thinkerHook: function called every tick for each player using this Trick Style. Activates before other hooks.
  -- successHook: function called for each time a player succeeds (mid-air only) while using this Trick Style.
  --              Use player.OpenTricks.combo to get the number of tricks so far (including this one.)
  -- landingHook: function that gets called for each time a player lands while using this Trick Style.
  --              Use player.OpenTricks.combo to get the number of tricks prior to landing.
  
  local style_t = {}
  style_t.name = name
  style_t.hint = hint
  style_t.offIcon = offIcon
  style_t.onIcon = onIcon
  style_t.chosenIcon = onIcon
  style_t.chosenIconSmall = onIcon
  style_t.banned = defaultBanned
  style_t.chosenHook = chosenHook
  style_t.thinkerHook = thinkerHook
  style_t.successHook = successHook
  style_t.landingHook = landingHook
  
  OpenTricks_Add_Complex(style_t)
end

rawset(_G, "OPENTRICKS_ADD", OpenTricks_Add)

local function OpenTricks_Add_FourIcons(name, hint, offIcon, onIcon, chosenIcon, chosenIconSmall, defaultBanned, chosenHook, thinkerHook, successHook, landingHook)
  local style_t = {}
  style_t.name = name
  style_t.hint = hint
  style_t.offIcon = offIcon
  style_t.onIcon = onIcon
  style_t.chosenIcon = chosenIcon
  style_t.chosenIconSmall = chosenIconSmall
  style_t.banned = defaultBanned
  style_t.chosenHook = chosenHook
  style_t.thinkerHook = thinkerHook
  style_t.successHook = successHook
  style_t.landingHook = landingHook
  
  OpenTricks_Add_Complex(style_t)
end

rawset(_G, "OPENTRICKS_ADD_FOURICONS", OpenTricks_Add_FourIcons)