// popup, event scripts menu handler script
// original work by GODJonez (thanks to everybody! Can't name all of you separately)
// updated 2006/10/14 - fixes, contains _popup_debug for debugging messages

block load
{
  es_xkeygroupcreate _popup_users
  es_xkeygroupcreate _popup_menus
  es_xkeycreate _popup_menus _popup_menu_list

  es_xdoblock popup/varinit

  es es_xexists _popup_exists command popup
  if (server_var(_popup_exists) == 0) do
  {
    es_xregcmd popup popup/popup "Creates a handled popup."
  }
  
  es es_xexists _popup_exists variable _popup_debug
  if (server_var(_popup_exists) == 0) do
  {
    es_xsetinfo _popup_debug 0
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Popup script addon loaded."
  }
}

block varinit
{
  es_xsetinfo _popup_temp 0
  es_xsetinfo _popup_debugtemp 0
  es_xsetinfo _popup_cargc 0
  es_xsetinfo _popup_cargs 0
  es_xsetinfo _popup_command 0
  es_xsetinfo _popup_name 0
  es_xsetinfo _popup_block 0
  es_xsetinfo _popup_popups 0
  es_xsetinfo _popup_exists 0
  es_xsetinfo _popup_idnum 0
  es_xsetinfo _popup_deleter 0
  es_xsetinfo _popup_looper 0
  es_xsetinfo _popup_lines 0
  es_xsetinfo _popup_linen 0
  es_xsetinfo _popup_text 0
  es_xsetinfo _popup_timer 0
  es_xsetinfo _popup_userid 0
  es_xsetinfo _popup_userpopups 0
  es_xsetinfo _popup_usertimerid 0
  es_xsetinfo _popup_usertimerq 0
  es_xsetinfo _popup_var 0
  es_xsetinfo _popup_extokens 0
  es_xsetinfo _popup_choice 0
  es_xsetinfo _popup_bot 0
  es_xsetinfo _popup_int_closemode 0
  es_xsetinfo _popup_closemode 0
}

block unload
{
  es_xkeygroupdelete _popup_users
  es_xkeygroupdelete _popup_menus
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Popup script addon unloaded."
  }
}

//clean up all the popups
event es_map_start
{
  es_xkeygroupdelete _popup_users
  es_xkeygroupcreate _popup_users
  if (server_var(_popup_debug) > 1) do
  {
    echo "[Popup] Map start event: _popup_users keygroup recreated."
  }
}

block popup
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Popup console command called."
  }
  es_xgetargc _popup_cargc
  es_xmath _popup_cargc - 1
  es_xgetargs _popup_cargs
  es_xgetargv _popup_command 1
  es_xgetargv _popup_name 2
  es_xsetinfo _popup_ok 1
  if (server_var(_popup_cargc) < 2) do
  {
    echo Too few arguments for popup.
    echo See www.eventscripts.com/pages/Popup for documentation.
    es_xsetinfo _popup_ok 0
    if (server_var(_popup_debug) > 1) do
    {
      echo "[Popup] ERROR: too few arguments."
    }
  }
  if (server_var(_popup_ok) == 1) do
  {
    es es_xformat _popup_block "popup/popup_cmd_%1" server_var(_popup_command)
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup] Popup name: server_var(_popup_name)
      es echo [Popup] Executing block server_var(_popup_block) ...
    }
    es es_xdoblock server_var(_popup_block)
  }
}

block popup_cmd_create
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: create."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  es es_xsetinfo _popup_nameb server_var(_popup_name)
  if (server_var(_popup_exists) == 1) do
  {
    if (server_var(_popup_debug) > 1) do
    {
      echo "[Popup] Popup exists, deleting the old one."
    }
    es_xdoblock popup/popup_cmd_delete
  }
  es_xcopy _popup_name _popup_nameb
  es es_xkeycreate _popup_menus server_var(_popup_name)
  es es_xkeysetvalue _popup_menus server_var(_popup_name) lines 0
  es_xmath _popup_popups + 1
  es es_xkeysetvalue _popup_menus _popup_menu_list server_var(_popup_popups) server_var(_popup_name)
  es es_xkeysetvalue _popup_menus server_var(_popup_name) idnum server_var(_popup_popups)
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Popup creation complete."
  }
}

block popup_cmd_delete
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: delete."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Tried to delete non-existing popup server_var(_popup_name).
  }
  else do
  {
    es es_xkeygetvalue _popup_idnum _popup_menus server_var(_popup_name) idnum
    es es_xkeydelete _popup_menus server_var(_popup_name)
    es_xmath _popup_popups - 1
    es_xcopy _popup_deleter _popup_idnum
    es_xdoblock popup/popup_listdelete
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Popup deletion complete."
  }
}

block popup_listdelete
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]  Deletion loop iteration."
  }
  if (server_var(_popup_deleter) <= server_var(_popup_popups)) do
  {
    es_xcopy _popup_looper _popup_deleter
    es_xmath _popup_deleter + 1

    es es_xkeygetvalue _popup_name _popup_menus _popup_menu_list server_var(_popup_deleter)
    es es_xkeysetvalue _popup_menus _popup_menu_list server_var(_popup_looper) server_var(_popup_name)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) idnum server_var(_popup_looper)
    es_xdoblock popup/popup_listdelete
  }
  else do
  {
    es es_xkeysetvalue _popup_menus _popup_menu_list server_var(_popup_deleter) "0"
  }
}

block popup_cmd_addline
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: addline."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot add a line to non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_text 3 //server_var(_popup_cargc) _popup_cargs
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Text: server_var(_popup_text)
    }
    es es_xkeygetvalue _popup_lines _popup_menus server_var(_popup_name) lines
    es_xmath _popup_lines + 1
    es es_xkeysetvalue _popup_menus server_var(_popup_name) lines server_var(_popup_lines)
    es es_xformat _popup_temp "line%1" server_var(_popup_lines)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_temp) server_var(_popup_text)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) "cache" "0"
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Adding line completed."
  }
}

block popup_cmd_delline
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: delline."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot delete a line from non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_linen 3
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Line: server_var(_popup_linen)
    }
    es es_xkeygetvalue _popup_lines _popup_menus server_var(_popup_name) lines
    es es_xformat _popup_temp "line%1" server_var(_popup_lines)
    if (server_var(_popup_linen) > server_var(_popup_lines)) do
    {
      esnq echo [popup] Tried to delete non-existing line from server_var(_popup_name).
    }
    else do
    {
      es_xmath _popup_lines - 1
      es es_xkeysetvalue _popup_menus server_var(_popup_name) lines server_var(_popup_lines)
      es_xcopy _popup_deleter _popup_linen
      es_xdoblock popup/popup_listdelline
      es es_xkeysetvalue _popup_menus server_var(_popup_name) "cache" "0"
    }
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Deleting line completed."
  }
}

block popup_listdelline
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]  Line deletion loop iteration."
  }
  if (server_var(_popup_deleter) <= server_var(_popup_lines)) do
  {
    es_xcopy _popup_looper _popup_deleter
    es_xmath _popup_deleter + 1
    es es_xformat _popup_exformat "line%1" server_var(_popup_deleter)
    es es_xkeygetvalue _popup_temp _popup_menus server_var(_popup_name) server_var(_popup_exformat)
    es es_xformat _popup_exformat "line%1" server_var(_popup_looper)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_exformat) server_var(_popup_temp)
    es_xdoblock popup/popup_listdelline
  }
  else do
  {
    es es_xformat _popup_exformat "line%1" server_var(_popup_deleter)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_exformat) 0
  }
}

block popup_cmd_insline
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: insline."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot insert a line to non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_linen 3
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Line: server_var(_popup_linen)
    }
    es es_xkeygetvalue _popup_lines _popup_menus server_var(_popup_name) lines
    if (server_var(_popup_linen) > server_var(_popup_lines)) do
    {
      esnq echo [popup] Tried to insert line outside on server_var(_popup_name).
    }
    else do
    {
      es_xgetargv _popup_text 4
      es_xcopy _popup_deleter _popup_lines
      es_xdoblock popup/popup_listinsline
      es es_xformat _popup_temp "line%1" server_var(_popup_linen)
      es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_temp) server_var(_popup_text)
      es_xmath _popup_lines + 1
      es es_xkeysetvalue _popup_menus server_var(_popup_name) lines server_var(_popup_lines)
      es es_xkeysetvalue _popup_menus server_var(_popup_name) "cache" "0"
    }
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Inserting line completed."
  }
}

block popup_listinsline
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]  Line inserting loop iteration."
  }
  if (server_var(_popup_deleter) >= server_var(_popup_linen)) do
  {
    es_xcopy _popup_looper _popup_deleter
    es_xmath _popup_looper + 1

    es es_xformat _popup_exformat "line%1" server_var(_popup_deleter)
    es es_xkeygetvalue _popup_temp _popup_menus server_var(_popup_name) server_var(_popup_exformat)
    es es_xformat _popup_exformat "line%1" server_var(_popup_looper)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_exformat) server_var(_popup_temp)
    es_xmath _popup_deleter - 1
    es_xdoblock popup/popup_listinsline
  }
}

block popup_cmd_modline
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: modline."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot modify a line in non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_linen 3
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Line: server_var(_popup_linen)
    }
    es es_xkeygetvalue _popup_lines _popup_menus server_var(_popup_name) lines
    if (server_var(_popup_linen) > server_var(_popup_lines)) do
    {
      esnq echo [popup] Tried to modify non-existing line on server_var(_popup_name).
    }
    else do
    {
      es_xgetargv _popup_text 4
      es es_xformat _popup_temp "line%1" server_var(_popup_linen)
      if (server_var(_popup_debug) > 1) do
      {
        es echo [Popup]  Text: server_var(_popup_text)
        es es_xkeygetvalue _popup_debugtemp _popup_menus server_var(_popup_name) server_var(_popup_temp)
        es echo [Popup]  Old text: server_var(_popup_debugtemp)
      }
      es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_temp) server_var(_popup_text)
      es es_xkeysetvalue _popup_menus server_var(_popup_name) "cache" "0"
    }
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Modifying line completed."
  }
}

block popup_cmd_menuselect
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: menuselect."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot set menuselect to non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_block 3
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Block: server_var(_popup_block)
    }
    es es_xkeysetvalue _popup_menus server_var(_popup_name) menuselect server_var(_popup_block)
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Menuselect assignment complete."
  }
}

block popup_cmd_menuselectfb
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: menuselectfb."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot set menuselect fallback to non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_block 3
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Block: server_var(_popup_block)
    }
    es es_xkeysetvalue _popup_menus server_var(_popup_name) menuselectfb server_var(_popup_block)
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Menuselect fallback assignment complete."
  }
}

block popup_cmd_prepuser
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: prepuser."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot set prepuser to non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_block 3
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Block: server_var(_popup_block)
    }
    es es_xkeysetvalue _popup_menus server_var(_popup_name) prepuser server_var(_popup_block)
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Preparation block assignment complete."
  }
}

block popup_cmd_select
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: select."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot set select to non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_linen 3
    es_xgetargv _popup_block 4
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Option: server_var(_popup_linen)
      es echo [Popup]  Block: server_var(_popup_block)
    }
    es es_xformat _popup_temp "select%1" server_var(_popup_linen)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_temp) server_var(_popup_block)
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Menu option selecting block assignment completed."
  }
}

block popup_cmd_menuvalue
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: menuvalue."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot set menu select value to non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_var 3
    es_xgetargv _popup_linen 4
    es_xgetargv _popup_block 5
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Option: server_var(_popup_linen)
      es echo [Popup]  Set: server_var(_popup_var) = server_var(_popup_block)
    }
    es es_xformat _popup_temp "value%1" server_var(_popup_linen)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_temp) server_var(_popup_block)
    es es_xformat _popup_temp "variable%1" server_var(_popup_linen)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_temp) server_var(_popup_var)
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Value binding to menu option completed."
  }
}

block popup_cmd_submenu
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: submenu."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot set submenu to non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_linen 3
    es_xgetargv _popup_block 4
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup]  Option: server_var(_popup_linen)
      es echo [Popup]  Popup: server_var(_popup_block)
    }
    es es_xformat _popup_temp "submenu%1" server_var(_popup_linen)
    es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_temp) server_var(_popup_block)
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Submenu assignment completed."
  }
}

block popup_cmd_timeout
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: timeout."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    esnq echo [popup] Cannot set timeout to non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_timer 3
    if (server_var(_popup_timer) == "send") do
    {
      if (server_var(_popup_debug) > 1) do
      {
        echo "[Popup] Timeout for: send."
      }
      es_xsetinfo _popup_temp timeoutsend
    }
    else do
    {
      if (server_var(_popup_debug) > 1) do
      {
        echo "[Popup] Timeout for: view."
      }
      es_xsetinfo _popup_temp timeoutview
    }
    es_xgetargv _popup_timer 4
    if (server_var(_popup_debug) > 1) do
    {
      es echo [Popup] Timeout value: server_var(_popup_timer) seconds.
    }
    es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_temp) server_var(_popup_timer)
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Timeout setting completed."
  }
}

block popup_cmd_send
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: send."
  }
  es es_xexists _popup_exists key _popup_menus server_var(_popup_name)
  if (server_var(_popup_exists) == 0) do
  {
    es echo [popup] Cannot send non-existing popup server_var(_popup_name)
  }
  else do
  {
    es_xgetargv _popup_temp 3
    es_xcopy _popup_debugtemp eventscripts_debug
    es_xcopy _popup_userid _popup_temp
    if (server_var(_popup_debug) <= 0) then eventscripts_debug -1
    es_xmath _popup_temp + 1
    es_xcopy eventscripts_debug _popup_debugtemp
    es_xsetinfo _popup_profile 0
    if (server_var(_popup_temp) == server_var(_popup_userid)) do
    {
      if (server_var(_popup_debug) > 1) do
      {
        echo "[Popup]  Target not recognized as userid."
      }
      es foreach player _popup_userid server_var(_popup_temp) "es_xdoblock popup/popup_send"
    }
    else do
    {
      if (server_var(_popup_debug) > 1) do
      {
        echo "[Popup]  Target seems userid, skipping foreach."
      }
      es_xdoblock popup/popup_send
    }
  }
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Popup sending completed: added to queue."
  }
}

block popup_cmd_unsend
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: unsend."
  }
  es_xgetargv _popup_temp 3
  es foreach player _popup_userid server_var(_popup_temp) "es_xdoblock popup/popup_unsend"
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Unsending completed."
  }
}

block popup_cmd_unsendid
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: unsendid."
  }
  es_xgetargv _popup_temp 3
  es foreach player _popup_userid server_var(_popup_temp) "es_xdoblock popup/popup_unsendid"
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Unsending completed."
  }
}

block popup_cmd_unsendname
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: unsendname."
  }
  es_xgetargv _popup_temp 3
  es foreach player _popup_userid server_var(_popup_temp) "es_xdoblock popup/popup_unsendname"
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Unsending copmleted."
  }
}

block popup_cmd_close
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: close."
  }
  es_xsetinfo _popup_int_closemode 1
  es_xdoblock popup/popup_cmd_unsendname
  es_xsetinfo _popup_int_closemode 0
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Close executed."
  }
}

block popup_unsend
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]  Unsend foreach player loop iteration."
  }
  es_xcopy _popup_deleter _popup_name
  es_xdoblock popup/popup_unsendnum
}

block popup_unsendid
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]  Unsendid foreach player loop iteration."
  }
  es es_xexists _popup_exists key _popup_users server_var(_popup_userid)
  if (server_var(_popup_exists) != 0) do
  {
    es es_xkeygetvalue _popup_userpopups _popup_users server_var(_popup_userid) popups
    es es_xformat _popup_temp "timer%1" server_var(_popup_name)
    es es_xkeygetvalue _popup_deleter _popup_users server_var(_popup_userid) server_var(_popup_temp)
    es es_xformat _popup_temp "popup%1" server_var(_popup_deleter)
    es es_xkeygetvalue _popup_name _popup_users server_var(_popup_userid) server_var(_popup_temp)
    if (server_var(_popup_debug) > 3) do
    {
      es echo [Popup]   Popup timer id: server_var(_popup_deleter)
      es echo [Popup]   Popup queuenum: server_var(_popup_name)
    }
    if (server_var(_popup_deleter) > 0) then es_xdoblock popup/popup_unsendnum
  }
}

block popup_unsendname
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]  Unsendname foreach player loop iteration."
  }
  es es_xexists _popup_exists key _popup_users server_var(_popup_userid)
  if (server_var(_popup_exists) != 0) do
  {
    es es_xkeygetvalue _popup_userpopups _popup_users server_var(_popup_userid) popups
    es_xsetinfo _popup_linen 1
    es_xdoblock popup/popup_unsendnameloop
  }
}

block popup_unsendnameloop
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]   Unsendname inner loop iteration."
  }
  if (server_var(_popup_linen) <= server_var(_popup_userpopups)) do
  {
    es es_xformat _popup_temp "popup%1" server_var(_popup_linen)
    es es_xkeygetvalue _popup_temp _popup_users server_var(_popup_userid) server_var(_popup_temp)
    if (server_var(_popup_debug) > 3) do
    {
      es echo [Popup]    Queue: server_var(_popup_linen)
      es echo [Popup]    Popup: server_var(_popup_name)
    }
    if (server_var(_popup_temp) == server_var(_popup_name)) do
    {
      if (server_var(_popup_debug) > 3) do
      {
        echo "[Popup]    Name match: unsending this one"
      }
      es_xcopy _popup_deleter _popup_linen
      es_xdoblock popup/popup_unsendnum
      if (server_var(_popup_linen) == 1) do
      {
        es_xmath _popup_linen + 1
      }
    }
    else do
    {
      es_xmath _popup_linen + 1
    }
    es_xdoblock popup/popup_unsendnameloop
  }
}

block popup_unsendnum
{
  if (server_var(_popup_debug) > 1) do
  {
    echo "[Popup]  Unsendnum block."
    es echo [Popup]   Queue: server_var(_popup_deleter)
  }
  es es_xexists _popup_exists key _popup_users server_var(_popup_userid)
  if (server_var(_popup_exists) != 0) do
  {
    es es_xkeygetvalue _popup_userpopups _popup_users server_var(_popup_userid) popups
    if (server_var(_popup_userpopups) < server_var(_popup_deleter)) do
    {
      es echo [popup] Cannot unsend non-existing popup server_var(_popup_deleter)
    }
    else do
    {
      if (server_var(_popup_deleter) == 1) do
      {
        if (server_var(_popup_debug) > 2) do
        {
          echo "[Popup]   This popup is the most recent one: cexec slot10."
        }
      	es es_xkeysetvalue _popup_users server_var(_popup_userid) closemode server_var(_popup_int_closemode)
        es es_xcexec server_var(_popup_userid) "slot10"
      }
      else do
      {
        if (server_var(_popup_debug) > 2) do
        {
          echo "[Popup]   Deleting the popup from queue."
        }
        es_xdoblock popup/popup_unsendloop
        es_xmath _popup_userpopups - 1
        es es_xkeysetvalue _popup_users server_var(_popup_userid) popups server_var(_popup_userpopups)
      }
    }
  }
}

block popup_unsendloop
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]   Unsendnum loop iteration."
    es echo [Popup]    Queue: server_var(_popup_deleter) / server_var(_popup_userpopups)
  }
  if (server_var(_popup_deleter) <= server_var(_popup_userpopups)) do
  {
    es_xcopy _popup_looper _popup_deleter
    es_xmath _popup_deleter + 1
    es es_xformat _popup_block "popup%1" server_var(_popup_deleter)
    es es_xkeygetvalue _popup_temp _popup_users server_var(_popup_userid) server_var(_popup_block)
    es es_xformat _popup_block "popup%1" server_var(_popup_looper)
    es es_xkeysetvalue _popup_users server_var(_popup_userid) server_var(_popup_block) server_var(_popup_temp)
    es es_xformat _popup_block "poptimer%1" server_var(_popup_deleter)
    es es_xkeygetvalue _popup_usertimerid _popup_users server_var(_popup_userid) server_var(_popup_block)
    es es_xformat _popup_block "poptimer%1" server_var(_popup_looper)
    es es_xkeysetvalue _popup_users server_var(_popup_userid) server_var(_popup_block) server_var(_popup_usertimerid)
    es es_xformat _popup_block "timer%1" server_var(_popup_usertimerid)
    es es_xkeysetvalue _popup_users server_var(_popup_userid) server_var(_popup_block) server_var(_popup_looper)
    es_xdoblock popup/popup_unsendloop
  }
}

block popup_send
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]  Send foreach player loop iteration."
  }
  es es_xisbot _popup_bot server_var(_popup_userid)
  if (server_var(_popup_bot) == 0) do
  {
    es_xdoblock popup/popup_checkuserid
    es es_xkeygetvalue _popup_userpopups _popup_users server_var(_popup_userid) popups
    es es_xkeygetvalue _popup_timer _popup_menus server_var(_popup_name) timeoutsend
    es_xmath _popup_userpopups + 1
    if (server_var(_popup_debug) > 3) do
    {
      es echo [Popup]   User now has server_var(_popup_userpopups) popups.
    }
    es es_xkeysetvalue _popup_users server_var(_popup_userid) popups server_var(_popup_userpopups)
    es es_xformat _popup_temp "popup%1" server_var(_popup_userpopups)
    es es_xkeysetvalue _popup_users server_var(_popup_userid) server_var(_popup_temp) server_var(_popup_name)
    if (server_var(_popup_timer) > 0) do
    {
      es_xcopy _popup_usertimerq _popup_userpopups
      es_xdoblock popup/popup_addkiller
    }
    if (server_var(_popup_userpopups) > 1) do
    {
      es es_xtell server_var(_popup_userid) You have a new popup (total: server_var(_popup_userpopups)). Please dismiss the current popup to see it.
    }
    es_xdoblock popup/popup_checkuserqueue
  }
  else do
  {
    if (server_var(_popup_debug) > 2) do
    {
      echo "[Popup]   Player is a bot, not sending."
    }
  }
}

//userid must be validated anyway before this block is called, so it is not checked here
// This is the block that actually sends the popups to users
block popup_checkuserqueue
{
  if (server_var(_popup_debug) > 2) do
  {
    es echo [Popup]   Checking user queue for server_var(_popup_userid) which should be valid.
  }
  es es_xkeygetvalue _popup_userpopups _popup_users server_var(_popup_userid) popups
  if (server_var(_popup_userpopups) > 0) do
  {
    es es_xkeygetvalue _popup_temp _popup_users server_var(_popup_userid) active
    if (server_var(_popup_temp) == 0) do
    {
      es_xsetinfo _popup_cachemode 0
      es es_xkeygetvalue _popup_name _popup_users server_var(_popup_userid) popup1
      es es_xkeygetvalue _popup_block _popup_menus server_var(_popup_name) prepuser
      if (server_var(_popup_block) != 0) do
      {
        if (server_var(_popup_debug) > 3) do
        {
          echo "[Popup]    Prepuser block detected."
        }
        es es_xkeygetvalue _popup_cachemode _popup_menus server_var(_popup_name) cachemode
        if (server_var(_popup_cachemode) == "user") do
        {
          es es_xformat _popup_format "cache%1" server_var(_popup_name)
          es es_xkeygetvalue _popup_text _popup_users server_var(_popup_userid) server_var(_popup_format)
          if (server_var(_popup_text) != 0) do
          {
            es_xsetinfo _popup_cachemode 0
          }
        }
        else do
        {
          es_xsetinfo _popup_cachemode 1
          es_xsetinfo _popup_format "cached"
        }
        if (server_var(_popup_cachemode) != 0) do
        {
          es es_xdoblock server_var(_popup_block)
        }
      }
      else do
      {
        es_xsetinfo _popup_format "cached"
        es es_xkeygetvalue _popup_text _popup_menus server_var(_popup_name) "cached"
        if (server_var(_popup_text) == 0) do
        {
          if (server_var(_popup_debug) > 3) do
          {
            echo "[Popup]    This popup has not been cached yet."
          }
          es_xsetinfo _popup_cachemode 1
        }
      }
      es_xdoblock popup/popup_viewpopup
      es es_xkeysetvalue _popup_users server_var(_popup_userid) active 1
      es_xsetinfo _popup_usertimerq 1
      es es_xkeygetvalue _popup_timer _popup_menus server_var(_popup_name) timeoutsend
      if (server_var(_popup_timer) > 0) do
      {
        es_xdoblock popup/popup_delkiller
      }
      es es_xkeygetvalue _popup_timer _popup_menus server_var(_popup_name) timeoutview
      if (server_var(_popup_timer) > 0) do
      {
        es_xdoblock popup/popup_addkiller
      }
    }
    else do
    {
      if (server_var(_popup_debug) > 2) do
      {
        echo "[Popup]    User has active popups, not sending next one."
      }
    }
  }
  else do
  {
    if (server_var(_popup_debug) > 2) do
    {
      echo "[Popup]    User has no popups."
    }
  }
}

block popup_addkiller
{
  if (server_var(_popup_debug) > 1) do
  {
    echo "[Popup]  Adding killer (timeout)."
  }
  es es_xkeygetvalue _popup_usertimerid _popup_users server_var(_popup_userid) timerid
  es_xmath _popup_usertimerid + 1
  es es_xkeysetvalue _popup_users server_var(_popup_userid) timerid server_var(_popup_usertimerid)
  es es_xformat _popup_temp "timer%1" server_var(_popup_usertimerid)
  es es_xkeysetvalue _popup_users server_var(_popup_userid) server_var(_popup_temp) server_var(_popup_usertimerq)
  es es_xformat _popup_temp "poptimer%1" server_var(_popup_usertimerq)
  es es_xkeysetvalue _popup_users server_var(_popup_userid) server_var(_popup_temp) server_var(_popup_usertimerid)
  es es_xdelayed server_var(_popup_timer) popup unsendid server_var(_popup_usertimerid) server_var(_popup_userid)
}

block popup_delkiller
{
  if (server_var(_popup_debug) > 1) do
  {
    echo "[Popup]  Deleting killer (timeout)."
  }
  es es_xformat _popup_temp "poptimer%1" server_var(_popup_usertimerq)
  es es_xkeygetvalue _popup_usertimerid _popup_users server_var(_popup_userid) server_var(_popup_temp)
  es es_xkeysetvalue _popup_users server_var(_popup_userid) server_var(_popup_temp) "0"
  es es_xformat _popup_temp "timer%1" server_var(_popup_usertimerid)
  es es_xkeysetvalue _popup_users server_var(_popup_userid) server_var(_popup_temp) "0"
}

// This is the block that uses es_xmenu to display the popup!
block popup_viewpopup
{
  if (server_var(_popup_debug) > 0) do
  {
    es echo [Popup]  Displaying popup server_var(_popup_name) to user server_var(_popup_userid)
  }
  if (server_var(_popup_cachemode) != 0) do
  {
    es es_xkeygetvalue _popup_lines _popup_menus server_var(_popup_name) lines
    es_xsetinfo _popup_looper 1
    es_xdoblock popup/popup_construct
    if (server_var(_popup_cachemode) == "user") do
    {
      es es_xkeysetvalue _popup_users server_var(_popup_userid) server_var(_popup_format) server_var(_popup_text)
    }
    else do
    {
      es es_xkeysetvalue _popup_menus server_var(_popup_name) server_var(_popup_format) server_var(_popup_text)
    }
  }
  else do
  {
    if (server_var(_popup_debug) > 2) do
    {
      echo "[Popup]   Using cached popup."
    }
  }
  es es_xmenu 0 server_var(_popup_userid) server_var(_popup_text)
}

block popup_construct
{
  if (server_var(_popup_debug) > 2) do
  {
    echo "[Popup]  Popup construction loop iteration."
  }
  if (server_var(_popup_looper) <= server_var(_popup_lines)) do
  {
    es es_xformat _popup_linen "line%1" server_var(_popup_looper)
    es es_xkeygetvalue _popup_temp _popup_menus server_var(_popup_name) server_var(_popup_linen)
    if (server_var(_popup_looper) == 1) do
    {
      es_xcopy _popup_text _popup_temp
    }
    else do
    {
      es es_xformat _popup_text "%1\n%2" server_var(_popup_text) server_var(_popup_temp)
    }
    es_xmath _popup_looper + 1
    es_xdoblock popup/popup_construct
  }
}

block popup_checkuserid
{
  if (server_var(_popup_debug) > 1) do
  {
    es echo [Popup]  Checking userid server_var(_popup_userid)
  }
  es es_xexists _popup_exists key _popup_users server_var(_popup_userid)
  if (server_var(_popup_exists) == 0) do
  {
    if (server_var(_popup_debug) > 1) do
    {
      echo "[Popup]   User not found, creating key."
    }
    es es_xkeycreate _popup_users server_var(_popup_userid)
    es es_xkeysetvalue _popup_users server_var(_popup_userid) popups 0
    es es_xkeysetvalue _popup_users server_var(_popup_userid) active 0
    es es_xkeysetvalue _popup_users server_var(_popup_userid) timerid 0
  }
}

event es_client_command
{
  if (event_var(command) == "menuselect") do
  {
    if (server_var(_popup_debug) > 0) do
    {
      echo "[Popup] Client command menuselect received."
    }
    if (event_var(commandstring) > 0) do
    {
      es es_xsetinfo _popup_userid event_var(userid)
      es es_xsetinfo _popup_choice event_var(commandstring)
      es_xdoblock popup/popup_client_command
    }
  }
}

block popup_client_command
{
  if (server_var(_popup_debug) > 1) do
  {
    echo "[Popup]  Menu selection actions."
  }
  es_xdoblock popup/popup_checkuserid
  es es_xkeygetvalue _popup_userpopups _popup_users server_var(_popup_userid) popups
  if (server_var(_popup_userpopups) > 0) do
  {
    es es_xkeygetvalue _popup_name _popup_users server_var(_popup_userid) popup1
    es es_xkeygetvalue _popup_timer _popup_menus server_var(_popup_name) timeoutview
    if (server_var(_popup_timer) > 0) do
    {
      es_xsetinfo _popup_usertimerq 1
      es_xdoblock popup/popup_delkiller
    }
    es es_xkeysetvalue _popup_users server_var(_popup_userid) active 0
    es es_xkeygetvalue _popup_closemode _popup_users server_var(_popup_userid) closemode
    if (server_var(_popup_closemode) == 0) do
    {
      es es_xformat _popup_exformat "variable%1" server_var(_popup_choice)
      es es_xkeygetvalue _popup_block _popup_menus server_var(_popup_name) server_var(_popup_exformat)
      if (server_var(_popup_block) != 0) do
      {
        es es_xformat _popup_exformat "value%1" server_var(_popup_choice)
        es es_xkeygetvalue _popup_temp _popup_menus server_var(_popup_name) server_var(_popup_exformat)
        es es_xsetinfo server_var(_popup_block) server_var(_popup_temp)
        if (server_var(_popup_debug) > 3) do
        {
          es echo [Popup]   Set: server_var(_popup_block) = server_var(_popup_temp)
        }
      }
      es es_xformat _popup_exformat "submenu%1" server_var(_popup_choice)
      es es_xkeygetvalue _popup_block _popup_menus server_var(_popup_name) server_var(_popup_exformat)
      if (server_var(_popup_block) != 0) do
      {
        es es_xkeysetvalue _popup_users server_var(_popup_userid) popup1 server_var(_popup_block)
        if (server_var(_popup_debug) > 3) do
        {
          es echo [Popup]   Submenu: server_var(_popup_block)
        }
      }
      else do
      {
        if (server_var(_popup_debug) > 3) do
        {
          es echo [Popup]   No submenu, deleting current from queue.
        }
        es_xsetinfo _popup_deleter 1
        es_xdoblock popup/popup_unsendloop
        es_xmath _popup_userpopups - 1
        es es_xkeysetvalue _popup_users server_var(_popup_userid) popups server_var(_popup_userpopups)
      }
      es es_xformat _popup_exformat "select%1" server_var(_popup_choice)
      es es_xkeygetvalue _popup_block _popup_menus server_var(_popup_name) server_var(_popup_exformat)
      if (server_var(_popup_block) != 0) do
      {
        if (server_var(_popup_debug) > 3) do
        {
          es echo [Popup]   Select block: server_var(_popup_block)
        }
        es es_xdoblock server_var(_popup_block)
      }
      else do
      {
        es es_xkeygetvalue _popup_block _popup_menus server_var(_popup_name) menuselectfb
        if (server_var(_popup_block) != 0) do
        {
          if (server_var(_popup_debug) > 3) do
          {
            es echo [Popup]   Menuselect fallback: server_var(_popup_block)
          }
          es es_xdoblock server_var(_popup_block)
        }
      }
      es es_xkeygetvalue _popup_block _popup_menus server_var(_popup_name) menuselect
      if (server_var(_popup_block) != 0) do
      {
        if (server_var(_popup_debug) > 3) do
        {
          es echo [Popup]   Menuselect block: server_var(_popup_block)
        }
        es es_xdoblock server_var(_popup_block)
      }
      es es_xkeysetvalue _popup_users server_var(_popup_userid) closemode 0
    }
    else do
    {
      if (server_var(_popup_debug) > 2) do
      {
        echo "[Popup]   Close mode enabled, ignoring all actions assigned to the popup."
      }
      es_xsetinfo _popup_deleter 1
      es_xdoblock popup/popup_unsendloop
      es_xmath _popup_userpopups - 1
      es es_xkeysetvalue _popup_users server_var(_popup_userid) popups server_var(_popup_userpopups)
    }
    es_xdoblock popup/popup_checkuserqueue
  }
}

block popup_cmd_isqueued
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: isqueued."
  }
  es_xgetargv _popup_var 3
  es_xgetargv _popup_userid 4
  es_xdoblock popup/popup_checkuserid
  es es_xsetinfo server_var(_popup_var) 0
  es es_xkeygetvalue _popup_userpopups _popup_users server_var(_popup_userid) popups
  es_xsetinfo _popup_linen 1
  es_xdoblock popup/popup_isqueuedloop
}

block popup_isqueuedloop
{
  if (server_var(_popup_debug) > 2) do
  {
    es echo [Popup]   Isqueued loop iteration: server_var(_popup_linen)
  }
  if (server_var(_popup_linen) <= server_var(_popup_userpopups)) do
  {
    es es_xformat _popup_temp "popup%1" server_var(_popup_linen)
    es es_xkeygetvalue _popup_temp _popup_users server_var(_popup_userid) server_var(_popup_temp)
    if (server_var(_popup_temp) == server_var(_popup_name)) do
    {
      es es_xcopy server_var(_popup_var) _popup_linen
    }
    else do
    {
      es_xmath _popup_linen + 1
      es_xdoblock popup/popup_isqueuedloop
    }
  }
}

block popup_cmd_active
{
  if (server_var(_popup_debug) > 0) do
  {
    echo "[Popup] Command: active."
  }
  es_xgetargv _popup_var 3
  es_xgetargv _popup_userid 4
  es_xdoblock popup/popup_checkuserid
  es es_xkeygetvalue _popup_userpopups _popup_users server_var(_popup_userid) popups
  if (server_var(_popup_userpopups) > 0) do
  {
    es es_xkeygetvalue server_var(_popup_name) _popup_users server_var(_popup_userid) popup1
    es es_xcopy server_var(_popup_var) _popup_userpopups
  }
  else do
  {
    es es_xsetinfo server_var(_popup_name) 0
    es es_xsetinfo server_var(_popup_var) 0
  }
}
