---------
---
--- GoldCounter (Core.lua) by Hexiatröhm
---
---------
local format = string.format
local gsub   = string.gsub
local floor  = math.floor
local abs    = math.abs
local sub	 = string.sub
local find   = string.find
local lower  = string.lower
local len 	 = string.len
local upper  = string.upper

local i, _ = nil, nil

--- Slash Commands
function SlashCmdList.GC(msg, editBox)

	if msg then msg = lower( msg ) end
	
	if msg == "options" then
		GoldCounterOptionsPanel:Show()		
		return
	end

	if msg == "lastlogin" then
		local LastLoginDateFormat = GC_TranslateDate( GC_LastLoginDate ) --- ToString with delimiters
		local LastLogoutDateFormat = GC_TranslateDate( GC_LastLogoutDate ) --- ToString with delimiters
		DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00GoldCounter|r ".."|cff00FFFF"..GC_Locale_Text.CHAT_LASTLOGGED.."|r |cff90EE90"..LastLoginDateFormat..
		" "..GC_LastLoginTime.."|r" )
		DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00GoldCounter|r ".."|cff00FFFF"..GC_Locale_Text.CHAT_LASTLOGOUT.."|r |cff90EE90"..LastLogoutDateFormat..
		" "..GC_LastLogouTime.."|r" )
		return
	end

	if msg == "clearchardb" then
		GC_ClearCharDB()
		return
	end

	if msg == "uiv" then

	local BuildInfo = select(4, GetBuildInfo())
	local ProjectInfo = WOW_PROJECT_ID

		DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00UI Version:|r "..BuildInfo )
		DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00Project ID:|r "..ProjectInfo )
		return
	end
	
	if msg == "beacon" then
	
		if GC_Beacon then
			GC_Beacon = false
			print("|cffffff00GoldCounter|r ".."Beacon OFF")
		else GC_Beacon = true
			print("|cffffff00GoldCounter|r ".."Beacon ON")
		end
	return
	end

	DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00GoldCounter|r version "..GC_Version.." by Hexiatröhm" )
	DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00     /gc options|r   "..GC_Locale_Text.CHAT_SHOWOPTIONS )
	DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00     /gc clearchardb|r   "..GC_Locale_Text.OPTIONSPANELTXT_CLEARDB )
	DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00     /gc lastlogin|r   "..GC_Locale_Text.CHAT_LASTLOGGED )
	
end

--------------------------------------------------------------------------------------------------------------

function GoldCounter_Reset( AutoReset )

	GC_MyDailyData.DailyCount = GC_MyDailyData.DailyCount + MySavedMoney --- Counting difference + actions

	--- Counter Sign to use
	local MoneySign = " " --- Probably 0
	if     ( GC_MyDailyData.DailyCount < 0 ) then MoneySign = "-"
	elseif ( GC_MyDailyData.DailyCount > 0 ) then MoneySign = "+"
	end
	---

	local Data1, Flag = "", boolean
	local NewDate = date("%y%m%d")
	local NewDateNum = tonumber( NewDate )
	local Date_STR_FORMAT = NewDate:sub(1,2) .. "/" .. NewDate:sub(3,4) .. "/" .. NewDate:sub(5,6)

-------------------------------
local function SetDataToTable()

	if AutoReset then
		MySavedMoneyHistory[1].date = Date_STR_FORMAT.." |cff909090(Auto)|r"
		DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00GoldCounter|r "..GC_Locale_Text.CHAT_COUNT_RESET_AUTO )
	else
		MySavedMoneyHistory[1].date = Date_STR_FORMAT
		DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00GoldCounter|r "..GC_Locale_Text.CHAT_COUNT_RESET )
	end

	MySavedMoneyHistory[1].amount = abs( GC_MyDailyData.DailyCount )
	MySavedMoneyHistory[1].sign = MoneySign
end
-------------------------------

	Data1  = MySavedMoneyHistory[1].date
	Flag   = find( Data1, Date_STR_FORMAT ) --- Search for current date in first record

			--- Dates are matching - Keeping Slot 1
			if not Flag then

				-----------------------------------------------------
				-- if max record > 1 and one record then Shift table
				-----------------------------------------------------
				if My_GC_Settings_Local.MaxHistoryRec > 1 and Data1 ~= "*" then
				
					for i = My_GC_Settings_Local.MaxHistoryRec - 1, 1, -1 do
						MySavedMoneyHistory[i+1].date   = MySavedMoneyHistory[i].date
						MySavedMoneyHistory[i+1].amount = MySavedMoneyHistory[i].amount
						MySavedMoneyHistory[i+1].sign   = MySavedMoneyHistory[i].sign
					end
				end
				-----------------------------------------------------
			end

				SetDataToTable()
				GoldCounter_SetHistoryList() -- Update tooltip

				My_GC_Settings_Local.LastResetDate = NewDateNum

	MySavedMoney		= 0
	GC_MyMoney.Gold		= "0"
	GC_MyMoney.Silver	= "00"
	GC_MyMoney.Copper	= "00"
	GC_MyMoney.Sign		= " "
	GC_MyMoney.Color	= Money_NoColor
	
	GoldCounter_Update()

	GC_CurrentFunds 	= GetMoney() -- Store current funds
	GC_LoginMoneyInit	= GC_CurrentFunds
	GC_PreviousMoney 	= GC_CurrentFunds

	-------------------------------------------------
	--- if new day reset basic values
	-------------------------------------------------
	if NewDateNum > GC_CurrentDate then
		GC_MyDailyData.DailyCount = 0
		GC_MyDailyData.QuestXP = 0
		GC_MyDailyData.Instance_MoneyLoot = 0
		GC_MyDailyData.Money_Loot = 0
		GC_MyDailyData.Merchant_Money = 0
		GC_MyDailyData.Auctioneer = 0
		GC_MyDailyData.MailBoxIncome = 0
		GC_MyDailyData.QuestReward = 0
		GC_MyDailyData.Merchant_Transmo = 0
		GC_MyDailyData.TaxiCost = 0
		GC_CurrentDate = NewDateNum
		GC_CurrentTime = date("%H:%M:%S")
	end
	-------------------------------------------------

	FlashCoinIcons( true )
	PlaySound( SOUNDKIT.IG_BACKPACK_COIN_SELECT )
end

function GC_TranslateMoney()

		local AbsTotal	= tostring( abs( MySavedMoney ) )
		local DataLen	= len( AbsTotal )

			if DataLen < 3 then -- It's only Coppers
			
					GC_MyMoney.Gold 	= "0"
					GC_MyMoney.Silver	= "00"

				if DataLen == 1 then -- One digit Coppers
				
					GC_MyMoney.Copper	= tostring( AbsTotal )
					GC_MyMoney.Copper  = "0"..GC_MyMoney.Copper
					
				else -- Two digits Coppers
				
					GC_MyMoney.Copper	= tostring( AbsTotal )
				
				end
			
			elseif DataLen < 5 then -- Silvers and Coppers
			
					GC_MyMoney.Gold 	= "0"
					GC_MyMoney.Copper 	= sub( AbsTotal, DataLen -1 )

				if DataLen == 3 then -- One digit Silvers and two digits Coppers
				
					GC_MyMoney.Silver = sub( AbsTotal, 1, 1 )
					
						GC_MyMoney.Silver = "0"..GC_MyMoney.Silver
				
				else -- Two digits Silvers
				
					GC_MyMoney.Silver 	= sub( AbsTotal, 1, 2 )
				
				end
				
			else -- There is gold in the river

					GC_MyMoney.Copper 	= sub( AbsTotal, DataLen -1 )
					GC_MyMoney.Silver 	= sub( AbsTotal, DataLen -3, DataLen -2 )

				if DataLen == 5 then -- One digit Gold
				
					GC_MyMoney.Gold 	= sub( AbsTotal, 1, 1 )

				else -- More gold
				
					GC_MyMoney.Gold 	= sub( AbsTotal, 1, DataLen -4 )

				end
				
			end
			
			GoldCounter_Update()
end
function GoldCounter_Update()

		local DelimitedGold = GC_reformatInt( GC_MyMoney.Gold )

				if MySavedMoney < 0 then

					GC_MyMoney.Sign = "-"
					GC_MyMoney.Color = GC_MyMoney.NegColor

				elseif MySavedMoney > 0 then

					GC_MyMoney.Sign = "+"
					GC_MyMoney.Color = GC_MyMoney.PosColor

				else

					GC_MyMoney.Sign = " "
					GC_MyMoney.Color = GC_MyMoney.NoColor

				end

		GC_ShowCoppers:SetText( GC_MyMoney.Color..GC_MyMoney.Copper.."|r" )
		GC_ShowSilvers:SetText( GC_MyMoney.Color..GC_MyMoney.Silver.."|r" )
		GC_ShowGolds:SetText( GC_MyMoney.Color..GC_MyMoney.Sign..DelimitedGold.."|r" )

		if MySavedMoney == 0 then
			GC_ResetButton:Disable()
		else
			GC_ResetButton:Enable()
		end
end

function GC_PlaySFX( Index )

	if ( Index == 1 ) then

		PlaySoundFile( "Interface\\AddOns\\GoldCounter\\SFX\\UI_VoidStorage_Redo.ogg", "SFX" )

	elseif ( Index == 2 ) then

		PlaySoundFile( "Interface\\AddOns\\GoldCounter\\SFX\\UI_VoidStorage_Undo.ogg", "SFX" )

	elseif ( Index == 3 ) then

		PlaySoundFile( "Interface\\AddOns\\GoldCounter\\SFX\\UI_EtherealWindow_Open.ogg", "SFX" )
	end
end

function GC_MinViewShowAllSetTooltip()

			GoldCounter_Custom_Tooltip:SetOwner( _G["GoldCounter_MainFrame"], "ANCHOR_BOTTOM", 50, 0 )
			GoldCounter_Custom_Tooltip:ClearLines()

			local fullViewStr = GC_MoneyCoinsTextures( nil, MySavedMoney, false )

			GoldCounter_Custom_TooltipTextLeft1:SetFontObject( GameTooltipTextSmall )
			GoldCounter_Custom_Tooltip:AddLine( fullViewStr, 1, 1, 1, false )

end

--- Minimap Button Functions
function GC_MiniMapButton_OnClick( frame, button )

	if button == "LeftButton" then

		if not GC_ToggleSwitch then

			GC_ToggleSwitch = true
			GoldCounter_MainFrame:Hide()
			PlaySound( SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_OFF )

			StopLoop1() --- Stop All Anim
			StopLoop2()

		else

			GC_ToggleSwitch = false
			GoldCounter_MainFrame:Show()
			PlaySound( SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON )

			GC_ResetFrame()

			--- Check if Resting or Fighting and start Animation
			GC_CheckResting()
			GC_CheckFighting()
		end

	elseif button == "RightButton" then

		if ( GoldCounterOptionsPanel:IsShown() ) then

			GoldCounterOptionsPanel:Hide()
		else

			GoldCounterOptionsPanel:Show()

		end

	elseif button == "MiddleButton" and MySavedMoney ~= 0 then

		GoldCounter_Reset()

	end
end

function GC_MiniMapButton_OnEnter( frame )

		local fText

		--- Coded for Vanilla compatibility
		GC_MinimapButtonTooltip:SetOwner( frame, "ANCHOR_TOPRIGHT")
		GC_MinimapButtonTooltip:SetPoint("TOPLEFT", frame, "BOTTOMRIGHT")
		GC_MinimapButtonTooltip:ClearLines()

		GC_MinimapButtonTooltipTextLeft1:SetFontObject( Fancy12Font )
		GC_MinimapButtonTooltip:AddLine("GoldCounter v"..GC_Version, 1, 1, 1, false )

		fText = "|cffffff00"..GC_Locale_Text.ICONTOOLTIP_LEFTCLICK..":|r "..GC_Locale_Text.ICONTOOLTIP_TEXT1
		GC_MinimapButtonTooltipTextLeft2:SetFontObject( GameFontWhiteTiny )
		GC_MinimapButtonTooltip:AddLine(fText, 1, 1, 1, false )
		--- Icon Left Click - Retail
		if GC_WOW_isRetail == true then
			GC_MinimapButtonTooltip:AddTexture( GC_Mouse_LeftClickIcon,
			{ width = 8, height = 12, margin = { left = 0, right = 3, top = 0, bottom = 0 } } )
		end

		fText = "|cffffff00"..GC_Locale_Text.ICONTOOLTIP_RIGHTCLICK..":|r "..GC_Locale_Text.ICONTOOLTIP_TEXT2
		GC_MinimapButtonTooltipTextLeft3:SetFontObject( GameFontWhiteTiny )
		GC_MinimapButtonTooltip:AddLine(fText, 1, 1, 1, false )
		--- Icon Right Click - Retail
		if GC_WOW_isRetail == true then
			GC_MinimapButtonTooltip:AddTexture( GC_Mouse_RightClickIcon,
			{ width = 8, height = 12, margin = { left = 0, right = 3, top = 0, bottom = 0 } } )
		end

		fText = "|cffffff00"..GC_Locale_Text.ICONTOOLTIP_MIDDLECLICK..":|r "..GC_Locale_Text.ICONTOOLTIP_TEXT3
		GC_MinimapButtonTooltipTextLeft4:SetFontObject( GameFontWhiteTiny )
		GC_MinimapButtonTooltip:AddLine( fText, 1, 1, 1, false )
		--- Icon Middle Click - Retail
		if GC_WOW_isRetail == true then
		GC_MinimapButtonTooltip:AddTexture( GC_Mouse_MiddleClickIcon,
		{ width = 8, height = 12, margin = { left = 0, right = 3, top = 0, bottom = 0 } } )
		end

		--- Incomes texts
		local DonjonMoney = GC_MoneyCoinsTextures( nil, GC_MyDailyData.Instance_MoneyLoot, false)
		local LootMoney = GC_MoneyCoinsTextures( nil, GC_MyDailyData.Money_Loot, false)
		local MerchantMoney = GC_MoneyCoinsTextures( nil, GC_MyDailyData.Merchant_Money, false)
		local Auctioneer = GC_MoneyCoinsTextures( nil, GC_MyDailyData.Auctioneer, false )
		local MailInbox = GC_MoneyCoinsTextures( nil, GC_MyDailyData.MailBoxIncome, false )
		local QuestReward = GC_MoneyCoinsTextures( nil, GC_MyDailyData.QuestReward, false )
		local TaxiCost = GC_MoneyCoinsTextures( nil, GC_MyDailyData.TaxiCost, false )
		local MerchantTransmo = GC_MoneyCoinsTextures( nil, GC_MyDailyData.Merchant_Transmo, false )

		GC_MinimapButtonTooltip:AddLine( " ", 1, 1, 1, false )

		GC_MinimapButtonTooltipTextLeft6:SetFontObject( Fancy12Font )
		GC_MinimapButtonTooltip:AddLine( GC_Locale_Text.PLAYERCARD_INCOMES, 0, 0.4, 1, false )

		GC_MinimapButtonTooltipTextLeft7:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltipTextRight7:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltip:AddDoubleLine( GC_Locale_Text.PLAYER_INCOMES_DUNGEON, DonjonMoney, nil, nil, nil, false )

		GC_MinimapButtonTooltipTextLeft8:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltipTextRight8:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltip:AddDoubleLine( GC_Locale_Text.PLAYER_INCOMES_MONEYLOOT, LootMoney, nil, nil, nil, false )

		GC_MinimapButtonTooltipTextLeft9:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltipTextRight9:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltip:AddDoubleLine( GC_Locale_Text.PLAYER_INCOMES_TRADER, MerchantMoney, nil, nil, nil, false )

		GC_MinimapButtonTooltipTextLeft10:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltipTextRight10:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltip:AddDoubleLine( GC_Locale_Text.PLAYER_INCOMES_AUCTIONEER, Auctioneer, nil, nil, nil, false )

		GC_MinimapButtonTooltipTextLeft11:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltipTextRight11:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltip:AddDoubleLine( GC_Locale_Text.PLAYER_INCOMES_MAILBOX, MailInbox, nil, nil, nil, false )

		GC_MinimapButtonTooltipTextLeft12:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltipTextRight12:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltip:AddDoubleLine( GC_Locale_Text.PLAYER_INCOMES_QUESTREWARD, QuestReward, nil, nil, nil, false )

		GC_MinimapButtonTooltipTextLeft13:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltipTextRight13:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltip:AddDoubleLine( GC_Locale_Text.PLAYER_INCOMES_TAXICOST, TaxiCost, nil, nil, nil, false )

		local TooltipIndex = 13

		if GC_WOW_isRetail or GC_WOW_isPANDARIA then
		TooltipIndex = TooltipIndex + 1
		_G["GC_MinimapButtonTooltipTextLeft"..TooltipIndex]:SetFontObject( FriendsFont_Small )
		_G["GC_MinimapButtonTooltipTextRight"..TooltipIndex]:SetFontObject( FriendsFont_Small )
			GC_MinimapButtonTooltip:AddDoubleLine( GC_Locale_Text.PLAYER_INCOMES_TRANSMO, MerchantTransmo, nil, nil, nil, false )
		end

		TooltipIndex = TooltipIndex + 1
		GC_MinimapButtonTooltip:AddLine( " ", nil, nil, nil, false )

		TooltipIndex = TooltipIndex + 1
		local TOTALtoConvert =
			GC_MyDailyData.Instance_MoneyLoot + GC_MyDailyData.Money_Loot + GC_MyDailyData.Merchant_Money
			+ GC_MyDailyData.Auctioneer + GC_MyDailyData.MailBoxIncome + GC_MyDailyData.QuestReward + GC_MyDailyData.TaxiCost
			+ GC_MyDailyData.Merchant_Transmo
			
		local TOTAL = GC_MoneyCoinsTextures( nil, TOTALtoConvert, nil)

		_G["GC_MinimapButtonTooltipTextLeft"..TooltipIndex]:SetFontObject( FriendsFont_Small )
		_G["GC_MinimapButtonTooltipTextRight"..TooltipIndex]:SetFontObject( FriendsFont_Small )
		GC_MinimapButtonTooltip:AddDoubleLine( GC_Locale_Text.TOOLTIP_INFOS_TOTAL, TOTAL, 1, 1, 1, false )
		
		GC_MinimapButtonTooltip:Show()

end

function GC_ResetDefault()

	local LastResetDate		= My_GC_Settings_Local.LastResetDate

	--- Addon Settings
	My_GC_Settings_Local = { }
	My_GC_Settings_Local.LastResetDate 	= LastResetDate

	My_GC_Settings_Local.LastLoginDate = GC_LastLoginDate
	My_GC_Settings_Local.LastLoginTime = GC_LastLoginTime
	My_GC_Settings_Local.LastLogoutDate = GC_LastLogoutDate
	My_GC_Settings_Local.LastLogoutTime = GC_LastLogouTime

	--- Minimap button
	GC_Icon_DB.profile.minimap = { hide = false }
	GC_Icon_DB.profile.minimap.minimapPos = nil
	GC_Icon:Refresh("GOLDCOUNTER", GC_Icon_DB.profile.minimap)

	--- Reset Position
	GoldCounter_MainFrame:ClearAllPoints()
	GoldCounter_MainFrame:SetPoint("CENTER", UIParent, 0, 0, 0 )
	GoldCounter_MainFrame:SetUserPlaced(true)

	GC_SetAddonOptions()

	--- DropDown Texts
	GC_FontDropDownList_OnClick()
	GC_ThouSeparator_DropDownList_OnClick( nil, My_GC_Settings_Local.Thousand_Delimiter )
	GC_DateFormat_DropDownList_OnClick( nil, My_GC_Settings_Local.UserDateFormat )

	-------------------		
	--- Reset All Data
	-------------------		
	if GC_Reset_EraseAllData_Checked  then
	
		if MySavedMoney ~= 0 then GoldCounter_Reset( true ) end

		GC_ClearHistory()

		GC_MyDailyData = { } --- Reset calling GC_SetAddonOptions()
		
		GC_SetAddonOptions()

	end
	-------------------		

	-------------------		
	--- Reset Global Profiles
	-------------------		
	if GC_Reset_EraseAllProfiles_Checked then

		My_GC_Profile = { }	

	end
	-------------------		

	GC_PlaySFX( 2 )
end

--------------------------------------------------------
--- Convert Money number to String with textures
--------------------------------------------------------

function GC_MoneyCoinsTextures( NumSign, amount, Raw )

	if not amount then return nil end
	amount = tonumber( amount ) or 0 --- Make sur amount is a number

	local StringColor = ""
	
	--- Default number's sign and color
	if not NumSign then
	
			NumSign = " "
			StringColor = GC_MyMoney.NoColor
		
	elseif NumSign == "+" then

			StringColor = GC_MyMoney.PosColor
	
	elseif NumSign == "-" then

			StringColor = GC_MyMoney.NegColor
	end
		

	if amount ~= 0 then 
	
			if amount > 0 and NumSign == " " then

					NumSign  = "+"
					StringColor = GC_MyMoney.PosColor

			elseif amount < 0 and NumSign == " " then

					NumSign  = "-"
					StringColor = GC_MyMoney.NegColor
			end

	else

		if Raw then return "0|TInterface\\MoneyFrame\\UI-CopperIcon:14:14:2:0|t" end
			
	end	


	-------------------------
	--- Format Money String
	-------------------------

	amount = abs(amount) --- In case it's negative "-"

		local TextureString = ""
		local CoinsString = ""

		local CurrentGold = floor( amount / 1e4 )
		local CurrentSilver = floor( amount / 100 ) % 100
		local CurrentCopper = amount % 100

	if amount ~= 0 then 

		--- Add Thousand Delimiter
		local _Gold = GC_reformatInt( CurrentGold )

		if CurrentGold > 0 then
			TextureString = TextureString .. _Gold .. "|TInterface\\MoneyFrame\\UI-GoldIcon:14:14:2:0|t"
		end

		if CurrentGold > 0 or CurrentSilver > 0 then
			
			if CurrentGold > 0 then

				TextureString = TextureString .. format(" %02d", CurrentSilver)

			else
			
				TextureString = TextureString .. CurrentSilver
			
			end
			
		TextureString = TextureString .. "|TInterface\\MoneyFrame\\UI-SilverIcon:14:14:2:0|t"
		
		end
		
		if CurrentCopper > 0 or CurrentSilver > 0 or CurrentGold > 0 then
		
			if CurrentSilver > 0 or CurrentGold > 0 then

				TextureString = TextureString .. format(" %02d", CurrentCopper )
				
			else
			
				TextureString = TextureString .. CurrentCopper
				
			end

		TextureString = TextureString .. "|TInterface\\MoneyFrame\\UI-CopperIcon:14:14:2:0|t"
		
		end

	else

		TextureString = "0|TInterface\\MoneyFrame\\UI-CopperIcon:14:14:2:0|t"
		
	end

	if not Raw then

		CoinsString = StringColor..NumSign..TextureString.."|r"
	else

		CoinsString = TextureString
	end

	return CoinsString

end


function GC_reformatInt( Value )

---  return tostring( Value ):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")

	local NumValue = tonumber( Value ) or 0
	
	if abs( NumValue ) < 1000 or not My_GC_Settings_Local.Thousand_Delimiter then --- Security for weird cases seen
		return tostring(Value)
	end

	local NewValue = tostring( Value ):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")

	NewValue = gsub( NewValue, ",", My_GC_Settings_Local.Thousand_Delimiter )

  return NewValue

end


function GC_GetPlayerItemLevel()

	local _, PlayerItemLevel = GetAverageItemLevel()
	PlayerItemLevel = format( "%.2f", PlayerItemLevel )

	return PlayerItemLevel

end


function GC_TranslateDate( Date )

	local ShortDate, TimeOrAuto = strsplit( " ", Date, 2)
	if not ShortDate then return nil end

local function FormatDate(Date)

	local FormatedString = ""

	local YY, MM, DD = sub( Date, 1, 2 ), sub( Date, 3, 4 ), sub( Date, 5, 6 )
	
		if ( My_GC_Settings_Local.UserDateFormat == "MM/DD/YY" ) then
	
			FormatedString = MM.."/"..DD.."/"..YY

		elseif ( My_GC_Settings_Local.UserDateFormat == "DD/MM/YY" ) then

			FormatedString = DD.."/"..MM.."/"..YY

		else

			FormatedString = YY.."/"..MM.."/"..DD

		end

		return FormatedString

end

	-- If the date was simple number in string
	if len(ShortDate) == 6 then

		return FormatDate(ShortDate)

	else

	-- If the date has an add (Compatibility with Date+Hour or Date+Auto)
	-- Reformat the date
	ShortDate = gsub(ShortDate, "/", "")
	ShortDate = FormatDate(ShortDate)

		if ( not TimeOrAuto ) then TimeOrAuto = "" end

		ShortDate = ShortDate .. " " .. TimeOrAuto

	end 

	return ShortDate
end

function GC_GetCharInfos()

	-------------------------------------------
	--- Get Character name and data -- Trying to deal with Classic servers lags
	GC_PlayerName			= GetUnitName( "player" ) or "?"
	if not GC_PlayerName or GC_PlayerName == "?" then return nil end
	
	GC_PlayerRealm			= GetRealmName() or "?"
	if not GC_PlayerRealm or GC_PlayerRealm == "?" then return nil end

	GC_PlayerNormRealm		= GetNormalizedRealmName() or "?"
	if not GC_PlayerNormRealm or GC_PlayerNormRealm == "?" then return nil end

	GC_PlayerLevel			= UnitLevel("player") or 0
	if not GC_PlayerLevel or GC_PlayerLevel == 0  or GC_PlayerLevel == "?" then return nil end

	GC_PlayerClassLocale, GC_PlayerClass, GC_PlayerClassIndex = UnitClass( "player" )
	GC_PlayerFaction, GC_PlayerFactionLocale = UnitFactionGroup( "player" )
	GC_PlayerItemLevel		= GC_GetPlayerItemLevel() or 0 --- Get Initial ItemLevel

	GC_PlayerXP				= UnitXP("player")
	GC_MaxXP				= UnitXPMax("player")
	GC_PlayerNormName		= GC_PlayerName.."-"..GC_PlayerNormRealm
	GC_CharGender = UnitSex("player")
	-------------------------------------------

	-------------------------------------------
	--- If Unknown Faction or Pandaria Neutral
	if not GC_PlayerFaction or GC_PlayerFaction == "Neutral" then
		GC_PlayerFaction = "?"
		GC_PlayerFactionLocale = "?"
	end
	-------------------------------------------

	return true
end
