// texthandler command v0.1
// Submitted by Wonder
block load
{
   es_xsetinfo _tempcore 0
   es_xexists _tempcore command texthandler
   es es_xif (server_var(_tempcore) == 0) do
   {
      es_xregcmd texthandler corelib/textlib/texthandler "Handles different variations of text and controls colors used."

      es_xsetinfo _th_arg1 0
      es_xsetinfo _th_arg2 0
      es_xsetinfo _th_exists 0
      es_xsetinfo _th_arg3 0
      es_xsetinfo _th_arg4 0
      es_xsetinfo _th_game 0
   }

   keygroupremove _th_text
   es_xkeygroupcreate _th_text
}
block unload
{
   keygroupremove _th_text
}
block texthandler
{
   es_xgetargv _th_arg1 1
   es es_xif (server_var(_th_arg1) == create) do
   {
      es_xgetargv _th_arg2 2
      es es_xif (server_var(_th_arg2) != 0) do
      {
         es_xsetinfo _th_exists 0
         es es_xexists _th_exists key _th_text server_var(_th_arg2)
         es es_xif (server_var(_th_exists) == 1) do
         {
            es es_xkeydelete _th_text server_var(_th_arg2)
         }
         es es_xkeycreate _th_text server_var(_th_arg2)
      }
      es_xelse do
      {
         es_xdbgmsg 0 [texthandler] You must provide a name!
      }
   }
   es_xelse do
   {
      es es_xif (server_var(_th_arg1) == delete) do
      {
         es_xgetargv _th_arg2 2
         es es_xif (server_var(_th_arg2) != 0) do
         {
            es_xsetinfo _th_exists 0
            es es_xexists _th_exists key _th_text server_var(_th_arg2)
            es es_xif (server_var(_th_exists) == 1) do
            {
               es es_xkeydelete _th_text server_var(_th_arg2)
            }
            es_xelse do
            {
               es_xdbgmsg 0 [texthandler] Tried to delete inexistant text!
            }
         }
         es_xelse do
         {
            es_xdbgmsg 0 [texthandler] You must provide a name!
         }
      }
      es_xelse do
      {
         es es_xif (server_var(_th_arg1) == set) do
         {
            es_xgetargv _th_arg2 2
            es es_xif (server_var(_th_arg2) != 0) do
            {
               es_xsetinfo _th_exists 0
               es es_xexists _th_exists key _th_text server_var(_th_arg2)
               es es_xif (server_var(_th_exists) == 1) do
               {
                  es_xgetargv _th_arg3 3
                  es es_xif (server_var(_th_arg3) != 0) do
                  {
                     es_xgetargv _th_arg4 4
                     es es_xif (server_var(_th_arg4) != 0) do
                     {
                        es_xgetgame _th_game
                        es es_xif (server_var(_th_game) != "Counter-Strike: Source") do
                        {
                           es_xstring _th_arg4 replace #green
                           es_xstring _th_arg4 replace #lightgreen
                           es_xstring _th_arg4 replace #default
                        }
                        es es_xkeysetvalue _th_text server_var(_th_arg2) server_var(_th_arg3) server_var(_th_arg4)
                     }
                     es_xelse do
                     {
                        es_xdbgmsg 0 [texthandler] You must provide a value for the text!
                     }
                  }
                  es_xelse do
                  {
                     es_xdbgmsg 0 [texthandler] You must provide a variation name!
                  }
               }
               es_xelse do
               {
                  es_xdbgmsg 0 [texthandler] Invalid text name!
               }
            }
            es_xelse do
            {
               es_xdbgmsg 0 [texthandler] You must provide a name!
            }
         }
         es_xelse do
         {
            es es_xif (server_var(_th_arg1) == get) do
            {
               es_xgetargv _th_arg2 2
               es es_xif (server_var(_th_arg2) != 0) do
               {
                  es_xsetinfo _th_exists 0
                  es es_xexists _th_exists variable server_var(_th_arg2)
                  es es_xif (server_var(_th_exists) == 1) do
                  {
                     es_xgetargv _th_arg3 3
                     es_xsetinfo _th_exists 0
                     es es_xexists _th_exists key _th_text server_var(_th_arg3)
                     es es_xif (server_var(_th_exists) == 1) do
                     {
                        es_xgetargv _th_arg4 4
                        es es_xkeygetvalue _th_arg4 _th_text server_var(_th_arg3) server_var(_th_arg4)
                        es es_xif (server_var(_th_arg4) != 0) do
                        {
                           es es_xsetinfo server_var(_th_arg2) server_var(_th_arg4)
                        }
                        es_xelse do
                        {
                           es es_xsetinfo server_var(_th_arg2) 0
                           es_xdbgmsg 0 [texthandler] Text variation does not exist!
                        }
                     }
                     es_xelse do
                     {
                        es_xdbgmsg 0 [texthandler] Invalid text name!
                     }
                  }
                  es_xelse do
                  {
                     es_xdbgmsg 0 [texthandler] The return cvar does not exist!
                  }
               }
               es_xelse do
               {
                  es_xdbgmsg 0 [texthandler] You must provide a return cvar!
               }
            }
         }
      }
   }
}

block texthandler_test
{
   profilecmd _th_profile_create "texthandler create _th_testtext"
   profilecmd _th_profile_set "texthandler set _th_testtext test1 #greenlol#defaultlol"
   profilecmd _th_profile_set "texthandler set _th_testtext test2 #lightgreenlol#greenlol"
   profilecmd _th_profile_set "texthandler set _th_testtext test3 lollol"

   es_xsetinfo _th_testreturn 0
   profilecmd _th_profile_get "texthandler get _th_testreturn _th_testtext test1"
   // On CSS: _th_testreturn = #greenlol#defaultlol
   // Not on CSS: _th_testreturn = lollol
   es echo _th_testreturn = server_var(_th_testreturn)

   profilecmd _th_profile_get "texthandler get _th_testreturn _th_testtext test2"
   // On CSS: _th_testreturn = #lightgreenlol#greenlol
   // Not on CSS: _th_testreturn = lollol
   es echo _th_testreturn = server_var(_th_testreturn)

   profilecmd _th_profile_get "texthandler get _th_testreturn _th_testtext test3"
   // On CSS: _th_testreturn = lollol
   // Not on CSS: _th_testreturn = lollol
   es echo _th_testreturn = server_var(_th_testreturn)

   profilecmd _th_profile_delete "texthandler delete _th_testtext"
}
