--BEGIN CONFIG
--GLOBAL CONSTANTS
local LOG_LEVEL = 4
local RESOURCE_CODE = GetResourceMetadata(GetCurrentResourceName(), "resource_code", 0)
local USE_MYSQL = tonumber(GetResourceMetadata(GetCurrentResourceName(), "USE_MYSQL", 0))

--Global Constants
--Global Variables
--LOCAL VARIABLES
local config = {}

--END CONFIG
-----------------------------------------------------------------------------------------------



-------------------
--YOUR CODE HERE---
--HAPPY CODING!----
-------------------



------------------------------------------------------------------------------------------------
--Config Syncing Function DO NOT TOUCH unless you know what you are doing.
Citizen.CreateThread(function()
  local cfgCounter = 0
  local RunOnce = true
  local RunConfig = true
  while RunConfig do
    local cfgToLoad = {}
    local initconfig = json.decode(LoadResourceFile(GetCurrentResourceName(), "initconfig.json"))
    if USE_MYSQL == 1 and RunOnce then
      local ready = exports.GCONFIG:IsReady()
      if ready then
        if cfgCounter <= 3 then
          local qResults = exports.GCONFIG:GetConfig(RESOURCE_CODE)
          if qResults ~= -1 then
            cfgToLoad = exports.GCONFIG:GetConfigWithUNI(RESOURCE_CODE)
            RunOnce = false
            cfgCounter = 0
          else
            local initconfig = json.decode(LoadResourceFile(GetCurrentResourceName(), "initConfig.json"))
            TriggerEvent("GCONFIG:CreateInitialConfig", RESOURCE_CODE, initconfig)
            --INCREMENT COUNTER
            cfgCounter = cfgCounter + 1
            Log("Config re-running in 1sec to procure updated MySQL values...", 4)
          end
        elseif cfgCounter > 3 then
          Log("Config retry reached max limit, loading config defaults.  Check GCONFIG", 1)
          RunOnce = false
          cfgToLoad = initconfig
        else
          Log("I have no clue how you have arrived here, something is very wrong...", 1)
        end
      end
    else
      Log("RunOnce check aborted as UseMySQL is not TRUE.", 4)
      RunOnce = false
      cfgToLoad = initconfig
    end
    if not RunOnce and RunConfig then
      UpdateConfigs(cfgToLoad)
      RunConfig = false
    end
    Wait(1000)
  end
end)

function UpdateConfigs(cfgJson) --for loop that reads all variables from config file.
  Log("Updating Config", 4)
  config = cfgJson
  for i, qResult in pairs(config) do
    Log("QRESULT.CONFIGNAME "..qResult.ConfigName, 4)
    if qResult.ConfigName == "LOG_LEVEL" then
      LOG_LEVEL = qResult.ConfigValue
    elseif qResult.ConfigName == "CHANGEME" then
      CHANGE_ME = qResult.ConfigValue
    end
  end
  Wait(1000)
  TriggerClientEvent(RESOURCE_CODE..":ConfigUpdate_cl", -1, config)
end

RegisterNetEvent(RESOURCE_CODE..":ConfigUpdate_sv")
AddEventHandler(RESOURCE_CODE..":ConfigUpdate_sv", function(cfgJson)
  UpdateConfigs(cfgJson)
  Wait(0)
  TriggerClientEvent(RESOURCE_CODE..":ConfigUpdate_cl", -1, cfgJson)
end)

RegisterCommand(string.lower(RESOURCE_CODE).."gconfig", function(source, args) 
  TriggerEvent("GCONFIG:UpdateMe", RESOURCE_CODE, true)
end)

--SLF Server Logging Function DO NOT TOUCH unless you know what you are doing.
function Log(message, logLevel)
  if tonumber(LOG_LEVEL) >= logLevel then
      exports.SLF:LogToServer(RESOURCE_CODE, logLevel, message)
  end
end

function DisLog(message)
  exports.SLF:LogServerToDiscord(RESOURCE_CODE, message)
end

function GetSID(UUID)
  Log("Requesting SID for UUID: "..UUID, 4)
  local SID = exports.SPH:GetSID(UUID)
  Log("Retreived SID: "..SID.." for UUID: "..UUID, 4)
  return SID
end