--=============================================
---
--- GoldCounter (InfoTable.lua) by Hexiatröhm
---
--=============================================

local tremove = table.remove
local tsort   = table.sort

local GC_CharRows = {}
GC_CharListTable = {}

local ROW_COUNT  = 20
local ROW_WIDTH  = 898
local ROW_HEIGHT = 22
local ROW_GAP    = 1

local COL_NAME_X	= 0
local COL_NAME_W	= 180

local COL_LEVEL_X	= 180
local COL_LEVEL_W	= 45

local COL_ILVL_X	= 225
local COL_ILVL_W	= 80

local COL_FACTION_X	= 305
local COL_FACTION_W	= 70

local COL_REALM_X	= 375
local COL_REALM_W	= 180

local COL_COUNTER_X	= 553
local COL_COUNTER_W	= 182

local COL_FUNDS_X	= 733
local COL_FUNDS_W	= 182

local FIRST_ROW_Y	= -6

local function GC_CreateOneRow( index )

	local row = CreateFrame("Frame", "GC_CharRow"..index, GC_ListPanelSetup)
    row:SetSize( ROW_WIDTH, ROW_HEIGHT)
	
	row:EnableMouse(true)
	row.IsActive = false
	row.IsCurrentChar = false
	
	row.CurrentCharBG = row:CreateTexture(nil, "BACKGROUND")
	row.CurrentCharBG:SetAllPoints()
	row.CurrentCharBG:SetColorTexture(0.20, 0.55, 1.00, 0.18)
	row.CurrentCharBG:Hide()

	row.Highlight = row:CreateTexture(nil, "BACKGROUND")
	row.Highlight:SetAllPoints()
	row.Highlight:SetColorTexture(1, 0.647, 0, 0.12)
	row.Highlight:Hide()
	
	row:SetScript("OnEnter", function(self)
	if self.IsActive then
		self.Highlight:Show()
		GC_BuildCharRowTooltip(self)
	end
	end)

	row:SetScript("OnLeave", function(self)
		self.Highlight:Hide()
		GoldCounter_Custom_Tooltip:Hide()
	end)
	
	if index == 1 then
		row:SetPoint("TOPLEFT", GC_ListPanelSetup, "TOPLEFT", 0, FIRST_ROW_Y)
    else
        row:SetPoint("TOPLEFT", GC_CharRows[index - 1], "BOTTOMLEFT", 0, -ROW_GAP)
    end

	-- NAME
	row.Name = row:CreateFontString(nil, "OVERLAY", "SystemFont_Large")
	row.Name:SetPoint("LEFT", row, "LEFT", COL_NAME_X + 14, 0)
	row.Name:SetWidth(COL_NAME_W - 14)
	row.Name:SetJustifyH("LEFT")
	row.Name:SetJustifyV("MIDDLE")
	row.Name:SetText("")

	-- LEVEL
	row.Level = row:CreateFontString(nil, "OVERLAY", "SystemFont_Large")
	row.Level:SetPoint("LEFT", row, "LEFT", COL_LEVEL_X, 0)
	row.Level:SetWidth(COL_LEVEL_W)
	row.Level:SetJustifyH("CENTER")
	row.Level:SetJustifyV("MIDDLE")
	row.Level:SetText("")
	row.Level:SetTextColor(0.7, 0.7, 0.7, 1)

	-- iLEVEL
	row.ItemLevel = row:CreateFontString(nil, "OVERLAY", "SystemFont_Large")
	row.ItemLevel:SetPoint("LEFT", row, "LEFT", COL_ILVL_X, 0)
	row.ItemLevel:SetWidth(COL_ILVL_W)
	row.ItemLevel:SetJustifyH("CENTER")
	row.ItemLevel:SetJustifyV("MIDDLE")
	row.ItemLevel:SetText("")
	row.ItemLevel:SetTextColor(0, 0.7, 1, 1)

	-- Faction Texture
	row.Faction = row:CreateTexture(nil, "OVERLAY")
	row.Faction:SetSize(20, 20)
	row.Faction:SetPoint("CENTER", row, "LEFT", COL_FACTION_X + (COL_FACTION_W / 2), 0)
	row.Faction:SetTexture("")

	-- REALM
	row.Realm = row:CreateFontString(nil, "OVERLAY", "SystemFont_Large")
	row.Realm:SetPoint("LEFT", row, "LEFT", COL_REALM_X + 6, 0)
	row.Realm:SetWidth(COL_REALM_W - 8)
	row.Realm:SetJustifyH("CENTER")
	row.Realm:SetJustifyV("MIDDLE")
	row.Realm:SetText("")
	row.Realm:SetTextColor(1, 0.647, 0, 1)

	-- COUNTER
	row.Counter = row:CreateFontString(nil, "OVERLAY", "NumberFontNormalLargeRight")
	row.Counter:SetPoint("LEFT", row, "LEFT", COL_COUNTER_X - 5, 0)
	row.Counter:SetWidth(COL_COUNTER_W - 8)
	row.Counter:SetJustifyH("RIGHT")
	row.Counter:SetJustifyV("MIDDLE")
	row.Counter:SetText("")

	-- Funds
	row.Funds = row:CreateFontString(nil, "OVERLAY", "NumberFontNormalLargeRight")
	row.Funds:SetPoint("LEFT", row, "LEFT", COL_FUNDS_X - 5, 0)
	row.Funds:SetWidth(COL_FUNDS_W - 8)
	row.Funds:SetJustifyH("RIGHT")
	row.Funds:SetJustifyV("MIDDLE")
	row.Funds:SetText("")

    GC_CharRows[index] = row
	
end

function GC_CreateRows()

	if #GC_CharRows > 0 then
		return
	end

	for i = 1, ROW_COUNT do
		GC_CreateOneRow(i)
	end

end

function GoldCounterCharPanel_Build()

	--- Copy Table ---
	local GC_TempListTable = {}
	local GC_TempListTable2 = {}
	local idx = 0

	local Funds = GetMoney()
	
	GC_GetJobsData()

	GC_CharListTable = { }
	
local function GetData( i )

	idx = idx + 1
	GC_TempListTable[idx] = {}

	GC_TempListTable[idx].Name		= GC_GetClassColor( My_GC_CharMoneyDB[i].Name, My_GC_CharMoneyDB[i].Class )
	GC_TempListTable[idx].NameRaw	= My_GC_CharMoneyDB[i].Name
	GC_TempListTable[idx].Level		= My_GC_CharMoneyDB[i].Level
	GC_TempListTable[idx].ItemLevel	= My_GC_CharMoneyDB[i].ItemLevel
	GC_TempListTable[idx].Faction	= My_GC_CharMoneyDB[i].Faction
	GC_TempListTable[idx].Realm		= "|cffffa500"..My_GC_CharMoneyDB[i].Realm.."|r"
	GC_TempListTable[idx].RealmRaw  = My_GC_CharMoneyDB[i].Realm
	GC_TempListTable[idx].Counter	= GC_MoneyCoinsTextures( nil, My_GC_CharMoneyDB[i].Balance, false )
	GC_TempListTable[idx].Funds		= "|cff00ff00" .. GC_MoneyCoinsTextures( nil, My_GC_CharMoneyDB[i].Funds, true ) .."|r"
	GC_TempListTable[idx].FundsRaw	= My_GC_CharMoneyDB[i].Funds
	GC_TempListTable[idx].GuildName	= My_GC_CharMoneyDB[i].GuildName
			
	GC_TempListTable[idx].Job1				= My_GC_CharMoneyDB[i].Job1
	GC_TempListTable[idx].Job1Icon			= My_GC_CharMoneyDB[i].Job1Icon
	GC_TempListTable[idx].Job1SkillLevel	= My_GC_CharMoneyDB[i].Job1SkillLevel
	GC_TempListTable[idx].Job1MaxSkillLevel	= My_GC_CharMoneyDB[i].Job1MaxSkillLevel
	GC_TempListTable[idx].Job2				= My_GC_CharMoneyDB[i].Job2
	GC_TempListTable[idx].Job2Icon			= My_GC_CharMoneyDB[i].Job2Icon
	GC_TempListTable[idx].Job2SkillLevel	= My_GC_CharMoneyDB[i].Job2SkillLevel
	GC_TempListTable[idx].Job2MaxSkillLevel	= My_GC_CharMoneyDB[i].Job2MaxSkillLevel
	
	GC_TempListTable[idx].MM_Plus_OverallScore			= My_GC_CharMoneyDB[i].MM_Plus_OverallScore
	GC_TempListTable[idx].MM_Plus_HighestKeyLevel		= My_GC_CharMoneyDB[i].MM_Plus_HighestKeyLevel
	GC_TempListTable[idx].MM_Plus_HighestKeyDungeonName	= My_GC_CharMoneyDB[i].MM_Plus_HighestKeyDungeonName

	GC_TempListTable[idx].IsCurrentChar	= false

		if My_GC_CharMoneyDB[i].NormName == GC_PlayerNormName then
			GC_TempListTable[idx].Level		= GC_PlayerLevel
			GC_TempListTable[idx].ItemLevel	= GC_GetPlayerItemLevel()
			GC_TempListTable[idx].Counter	= GC_MoneyCoinsTextures( nil, MySavedMoney, false )
			GC_TempListTable[idx].Funds		= "|cff00ff00" .. GC_MoneyCoinsTextures( nil, Funds, true ) .."|r"
			GC_TempListTable[idx].FundsRaw	= Funds
			GC_TempListTable[idx].GuildName	= GC_PlayerGuildName
			
			GC_TempListTable[idx].Job1				= GC_Job1
			GC_TempListTable[idx].Job1Icon			= GC_Job1Icon
			GC_TempListTable[idx].Job1SkillLevel	= GC_Job1SkillLevel
			GC_TempListTable[idx].Job1MaxSkillLevel	= GC_Job1MaxSkillLevel
			GC_TempListTable[idx].Job2				= GC_Job2
			GC_TempListTable[idx].Job2Icon			= GC_Job2Icon
			GC_TempListTable[idx].Job2SkillLevel	= GC_Job2SkillLevel
			GC_TempListTable[idx].Job2MaxSkillLevel	= GC_Job2MaxSkillLevel
			
			GC_TempListTable[idx].MM_Plus_OverallScore			= GC_MM_Plus_OverallScore
			GC_TempListTable[idx].MM_Plus_HighestKeyLevel		= GC_MM_Plus_HighestKeyLevel
			GC_TempListTable[idx].MM_Plus_HighestKeyDungeonName	= GC_MM_Plus_HighestKeyDungeonName
			
			GC_TempListTable[idx].IsCurrentChar	= true
		end

		if GC_TempListTable[idx].Level == GC_MaxPlayerLevel then
			GC_TempListTable[idx].Level = "|cfff0f000" .. GC_TempListTable[idx].Level .. "|r"
		elseif GC_TempListTable[idx].Level >= ( GC_MaxPlayerLevel - 9 ) then
			GC_TempListTable[idx].Level = "|cff00f000" .. GC_TempListTable[idx].Level .. "|r"
		end
			
end

	--- For current server
	for i = 1, #My_GC_CharMoneyDB, 1 do

		if My_GC_CharMoneyDB[i].Realm == GC_PlayerRealm then

			if GC_WOW_isRetail then
				GetData( i )
			else

			if GC_PlayerFactionEN == My_GC_CharMoneyDB[i].Faction then GetData( i )
			end

			end
		end
	end

	--- Sort by Name
	tsort(GC_TempListTable, function(a, b)
	return a.NameRaw < b.NameRaw
	end)

	idx = 0

	--- For other servers if Retail
	if GC_WOW_isRetail then
	for i = 1, #My_GC_CharMoneyDB, 1 do

		if My_GC_CharMoneyDB[i].Realm ~= GC_PlayerRealm then
		
		idx = idx + 1
		
			GC_TempListTable2[idx] = {}

			GC_TempListTable2[idx].Name		 = GC_GetClassColor( My_GC_CharMoneyDB[i].Name, My_GC_CharMoneyDB[i].Class )
			GC_TempListTable2[idx].NameRaw	 = My_GC_CharMoneyDB[i].Name
			GC_TempListTable2[idx].Level	 = My_GC_CharMoneyDB[i].Level
			GC_TempListTable2[idx].ItemLevel = My_GC_CharMoneyDB[i].ItemLevel
			GC_TempListTable2[idx].Faction	 = My_GC_CharMoneyDB[i].Faction
			GC_TempListTable2[idx].Realm	 = "|cffffa500"..My_GC_CharMoneyDB[i].Realm.."|r"
			GC_TempListTable2[idx].RealmRaw	 = My_GC_CharMoneyDB[i].Realm
			GC_TempListTable2[idx].Counter	 = GC_MoneyCoinsTextures( nil, My_GC_CharMoneyDB[i].Balance, false )
			GC_TempListTable2[idx].Funds	 = "|cff00ff00" .. GC_MoneyCoinsTextures( nil, My_GC_CharMoneyDB[i].Funds, true ) .."|r"
			GC_TempListTable2[idx].FundsRaw	 = My_GC_CharMoneyDB[i].Funds
			GC_TempListTable2[idx].GuildName = My_GC_CharMoneyDB[i].GuildName
			
			GC_TempListTable2[idx].Job1				 = My_GC_CharMoneyDB[i].Job1
			GC_TempListTable2[idx].Job1Icon			 = My_GC_CharMoneyDB[i].Job1Icon
			GC_TempListTable2[idx].Job1SkillLevel	 = My_GC_CharMoneyDB[i].Job1SkillLevel
			GC_TempListTable2[idx].Job1MaxSkillLevel = My_GC_CharMoneyDB[i].Job1MaxSkillLevel
			GC_TempListTable2[idx].Job2				 = My_GC_CharMoneyDB[i].Job2
			GC_TempListTable2[idx].Job2Icon			 = My_GC_CharMoneyDB[i].Job2Icon
			GC_TempListTable2[idx].Job2SkillLevel	 = My_GC_CharMoneyDB[i].Job2SkillLevel
			GC_TempListTable2[idx].Job2MaxSkillLevel = My_GC_CharMoneyDB[i].Job2MaxSkillLevel
			
			GC_TempListTable2[idx].MM_Plus_OverallScore			 = My_GC_CharMoneyDB[i].MM_Plus_OverallScore
			GC_TempListTable2[idx].MM_Plus_HighestKeyLevel		 = My_GC_CharMoneyDB[i].MM_Plus_HighestKeyLevel
			GC_TempListTable2[idx].MM_Plus_HighestKeyDungeonName = My_GC_CharMoneyDB[i].MM_Plus_HighestKeyDungeonName
			
			GC_TempListTable2[idx].IsCurrentChar = false

			if GC_TempListTable2[idx].Level == GC_MaxPlayerLevel then
				GC_TempListTable2[idx].Level = "|cfff0f000" .. GC_TempListTable2[idx].Level .. "|r"
			elseif GC_TempListTable2[idx].Level >= ( GC_MaxPlayerLevel - 9 ) then
				GC_TempListTable2[idx].Level = "|cff00f000" .. GC_TempListTable2[idx].Level .. "|r"
			end

		end
	end
	
	--- Sorting Table2 below Table
	--- Method = Server/Name
	tsort(GC_TempListTable2, function(a, b)
	if a.RealmRaw == b.RealmRaw then
		return a.NameRaw < b.NameRaw
	end
	return a.RealmRaw < b.RealmRaw
	end)
	
	else GC_TempListTable2 = nil
	end

	idx = 0

	-- Concatenate tables with current server at top
	for i = 1, #GC_TempListTable, 1 do

		GC_CharListTable[i] = {}

		GC_CharListTable[i].Name		= GC_TempListTable[i].Name
		GC_CharListTable[i].NameRaw		= GC_TempListTable[i].NameRaw
		GC_CharListTable[i].Level		= GC_TempListTable[i].Level
		GC_CharListTable[i].ItemLevel	= GC_TempListTable[i].ItemLevel
		GC_CharListTable[i].Faction		= GC_TempListTable[i].Faction
		GC_CharListTable[i].Realm		= GC_TempListTable[i].Realm
		GC_CharListTable[i].RealmRaw    = GC_TempListTable[i].RealmRaw
		GC_CharListTable[i].Counter		= GC_TempListTable[i].Counter
		GC_CharListTable[i].Funds		= GC_TempListTable[i].Funds
		GC_CharListTable[i].FundsRaw	= GC_TempListTable[i].FundsRaw
		GC_CharListTable[i].GuildName	= GC_TempListTable[i].GuildName

		GC_CharListTable[i].Job1				= GC_TempListTable[i].Job1
		GC_CharListTable[i].Job1Icon			= GC_TempListTable[i].Job1Icon
		GC_CharListTable[i].Job1SkillLevel		= GC_TempListTable[i].Job1SkillLevel
		GC_CharListTable[i].Job1MaxSkillLevel	= GC_TempListTable[i].Job1MaxSkillLevel
		GC_CharListTable[i].Job2				= GC_TempListTable[i].Job2
		GC_CharListTable[i].Job2Icon			= GC_TempListTable[i].Job2Icon
		GC_CharListTable[i].Job2SkillLevel		= GC_TempListTable[i].Job2SkillLevel
		GC_CharListTable[i].Job2MaxSkillLevel	= GC_TempListTable[i].Job2MaxSkillLevel

		GC_CharListTable[i].MM_Plus_OverallScore			= GC_TempListTable[i].MM_Plus_OverallScore
		GC_CharListTable[i].MM_Plus_HighestKeyLevel			= GC_TempListTable[i].MM_Plus_HighestKeyLevel
		GC_CharListTable[i].MM_Plus_HighestKeyDungeonName	= GC_TempListTable[i].MM_Plus_HighestKeyDungeonName

		GC_CharListTable[i].IsCurrentChar = GC_TempListTable[i].IsCurrentChar
	end
	---
	if GC_TempListTable2 and #GC_TempListTable2 > 0 then
	
	local MaxValue = #GC_CharListTable + #GC_TempListTable2

	for i = #GC_CharListTable+1, MaxValue, 1 do

		idx = idx + 1

		GC_CharListTable[i] = {}

		GC_CharListTable[i].Name		= GC_TempListTable2[idx].Name
		GC_CharListTable[i].NameRaw		= GC_TempListTable2[idx].NameRaw
		GC_CharListTable[i].Level		= GC_TempListTable2[idx].Level
		GC_CharListTable[i].ItemLevel	= GC_TempListTable2[idx].ItemLevel
		GC_CharListTable[i].Faction		= GC_TempListTable2[idx].Faction
		GC_CharListTable[i].Realm		= GC_TempListTable2[idx].Realm
		GC_CharListTable[i].RealmRaw	= GC_TempListTable2[idx].RealmRaw
		GC_CharListTable[i].Counter		= GC_TempListTable2[idx].Counter
		GC_CharListTable[i].Funds		= GC_TempListTable2[idx].Funds
		GC_CharListTable[i].FundsRaw	= GC_TempListTable2[idx].FundsRaw
		GC_CharListTable[i].GuildName	= GC_TempListTable2[idx].GuildName

		GC_CharListTable[i].Job1				= GC_TempListTable2[idx].Job1
		GC_CharListTable[i].Job1Icon			= GC_TempListTable2[idx].Job1Icon
		GC_CharListTable[i].Job1SkillLevel		= GC_TempListTable2[idx].Job1SkillLevel
		GC_CharListTable[i].Job1MaxSkillLevel	= GC_TempListTable2[idx].Job1MaxSkillLevel
		GC_CharListTable[i].Job2				= GC_TempListTable2[idx].Job2
		GC_CharListTable[i].Job2Icon			= GC_TempListTable2[idx].Job2Icon
		GC_CharListTable[i].Job2SkillLevel		= GC_TempListTable2[idx].Job2SkillLevel
		GC_CharListTable[i].Job2MaxSkillLevel	= GC_TempListTable2[idx].Job2MaxSkillLevel

		GC_CharListTable[i].MM_Plus_OverallScore			= GC_TempListTable2[idx].MM_Plus_OverallScore
		GC_CharListTable[i].MM_Plus_HighestKeyLevel			= GC_TempListTable2[idx].MM_Plus_HighestKeyLevel
		GC_CharListTable[i].MM_Plus_HighestKeyDungeonName	= GC_TempListTable2[idx].MM_Plus_HighestKeyDungeonName
		
		GC_CharListTable[i].IsCurrentChar = GC_TempListTable2[idx].IsCurrentChar
	end
	
	end

	-- if not Retail then remove Opposite faction
	if not GC_WOW_isRetail then
	
		for i = #GC_CharListTable, 1 , -1 do

			if GC_PlayerFactionEN ~= GC_CharListTable[i].Faction then tremove( GC_CharListTable, i )
			end
		end

	end

	--- Current Server not on top but mixed
	if My_GC_Settings_Local.SortServerOnTop == false then
	
		--- Method = Server/Name
		tsort(GC_CharListTable, function(a, b)
		if a.RealmRaw == b.RealmRaw then
			return a.NameRaw < b.NameRaw
		end
		return a.RealmRaw < b.RealmRaw
		end)

	end

	GoldCounterWarband_Total_Update() -- Update Total money sub-Panel
	GC_UpdateCharList()
end

function GoldCounterWarband_Total_OnLoad( frame )

	frame:SetBackdrop( {
       	bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true,
		tileSize = 16,
		edgeSize = 16,
		insets = { left=2, right=2, top=2, bottom=2 } } )

		frame:SetBackdropColor(0,0,0,1)
		frame:SetBackdropBorderColor(1,1,1,0.8)

end
function GoldCounterCurrentChar_OnLoad( frame )

	frame:SetBackdrop( {
       	bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true,
		tileSize = 16,
		edgeSize = 16,
		insets = { left=2, right=2, top=2, bottom=2 } } )

		frame:SetBackdropColor(0,0,0,1)
		frame:SetBackdropBorderColor(1,1,1,0.8)

		GC_CurrentCharName:SetText( UnitName("player") )
end

function GoldCounterWarband_Total_Update()

	local MyBandSavedMoney = 0
	local WarbandBankFormatted = "0"
	
	local TotalNum = 0
	local TOTAL = "0"
	GC_CurrentFunds = GetMoney()
	
		--- Check WarBandBank
		if GC_WOW_isRetail and C_Bank and C_Bank.FetchDepositedMoney and Enum and Enum.BankType then --- Warband bank only in Retail

		MyBandSavedMoney = C_Bank.FetchDepositedMoney(Enum.BankType.Account) or 0

		WarbandBankFormatted = GC_MoneyCoinsTextures( nil, MyBandSavedMoney, true )
		
		else WarbandBankFormatted = "0"
		end
		GC_CharPanel_Warband_Num:SetText( WarbandBankFormatted )
		
		--- Check Total Funds
		for i = 1, #My_GC_CharMoneyDB do

			if My_GC_CharMoneyDB[i].NormName ~= GC_PlayerNormName then
				TotalNum = TotalNum + My_GC_CharMoneyDB[i].Funds
			end
		end
		TotalNum = TotalNum + MyBandSavedMoney + GC_CurrentFunds
		TOTAL = GC_MoneyCoinsTextures( nil, TotalNum, true )
		GC_CharPanel_Total_Num:SetText( TOTAL )

		--- Sub-panel Current char
		GC_CurrentCharFunds:SetText("|cff00ff00" .. GC_MoneyCoinsTextures( nil, GC_CurrentFunds, true ) .."|r")
end

function GC_ListPanelSetup_OnLoad( frame )

	---------------------------------------
	--- Prepare the frame and scrollbar ---
	---------------------------------------
	frame:SetBackdrop( {
       	bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true,
		tileSize = 16,
		edgeSize = 16,
		insets = { left=2, right=2, top=2, bottom=2 } } )

		frame:SetBackdropColor(0,0,0,1)
		frame:SetBackdropBorderColor(1,1,1,0.8)

		GC_CharScrollScrollBar:ClearAllPoints()
		GC_CharScrollScrollBar:SetPoint("TOPRIGHT", GC_CharScroll, "TOPRIGHT", 28, -22)
		GC_CharScrollScrollBar:SetPoint("BOTTOMRIGHT", GC_CharScroll, "BOTTOMRIGHT", 28, 18)

		--- Change Scrollbar arrows textures ---
		GC_CharScrollScrollBarScrollUpButton:SetNormalTexture("Interface\\Buttons\\UI-ScrollBar-ScrollUpButton-Up")
		GC_CharScrollScrollBarScrollUpButton:SetPushedTexture("Interface\\Buttons\\UI-ScrollBar-ScrollUpButton-Down")
		GC_CharScrollScrollBarScrollUpButton:SetDisabledTexture("Interface\\Buttons\\UI-ScrollBar-ScrollUpButton-Disabled")
		GC_CharScrollScrollBarScrollUpButton:SetHighlightTexture("Interface\\Buttons\\UI-ScrollBar-ScrollUpButton-Highlight", "ADD")

		GC_CharScrollScrollBarScrollDownButton:SetNormalTexture("Interface\\Buttons\\UI-ScrollBar-ScrollDownButton-Up")
		GC_CharScrollScrollBarScrollDownButton:SetPushedTexture("Interface\\Buttons\\UI-ScrollBar-ScrollDownButton-Down")
		GC_CharScrollScrollBarScrollDownButton:SetDisabledTexture("Interface\\Buttons\\UI-ScrollBar-ScrollDownButton-Disabled")
		GC_CharScrollScrollBarScrollDownButton:SetHighlightTexture("Interface\\Buttons\\UI-ScrollBar-ScrollDownButton-Highlight", "ADD")

		if not GC_CharScrollScrollBarScrollUpButton.BG then
		GC_CharScrollScrollBarScrollUpButton.BG = GC_CharScrollScrollBarScrollUpButton:CreateTexture(nil, "BACKGROUND", nil, -1)
		GC_CharScrollScrollBarScrollUpButton.BG:SetAllPoints(GC_CharScrollScrollBarScrollUpButton)
		GC_CharScrollScrollBarScrollUpButton.BG:SetColorTexture(1, 0.647, 0, 0.12)
		end

		if not GC_CharScrollScrollBarScrollDownButton.BG then
		GC_CharScrollScrollBarScrollDownButton.BG = GC_CharScrollScrollBarScrollDownButton:CreateTexture(nil, "BACKGROUND", nil, -1)
		GC_CharScrollScrollBarScrollDownButton.BG:SetAllPoints(GC_CharScrollScrollBarScrollDownButton)
		GC_CharScrollScrollBarScrollDownButton.BG:SetColorTexture(1, 0.647, 0, 0.12)
		end
		
		--- Change Scrollbar track color ---
		if not GC_CharScrollScrollBar.TrackBG then
		GC_CharScrollScrollBar.TrackBG = GC_CharScrollScrollBar:CreateTexture(nil, "BACKGROUND", nil, -1)
		GC_CharScrollScrollBar.TrackBG:SetPoint("TOPLEFT", GC_CharScrollScrollBarScrollUpButton, "BOTTOMLEFT", 0, -1)
		GC_CharScrollScrollBar.TrackBG:SetPoint("TOPRIGHT", GC_CharScrollScrollBarScrollUpButton, "BOTTOMRIGHT", 0, -1)
		GC_CharScrollScrollBar.TrackBG:SetPoint("BOTTOMLEFT", GC_CharScrollScrollBarScrollDownButton, "TOPLEFT", 0, 1)
		GC_CharScrollScrollBar.TrackBG:SetPoint("BOTTOMRIGHT", GC_CharScrollScrollBarScrollDownButton, "TOPRIGHT", 0, 1)
		GC_CharScrollScrollBar.TrackBG:SetColorTexture(1, 0.647, 0, 0.12)
		end
end

function GC_UpdateCharList()

	GC_NumRows:SetText( #GC_CharListTable )
	
	local totalRows = #GC_CharListTable
	local offset = FauxScrollFrame_GetOffset(GC_CharScroll)

	FauxScrollFrame_Update(GC_CharScroll, totalRows, ROW_COUNT, ROW_HEIGHT + ROW_GAP)
	
	for i = 1, ROW_COUNT do
	
		local row = GC_CharRows[i]
		local dataIndex = offset + i
		local data = GC_CharListTable[dataIndex]

		if data then
		
			row:Show()
			row:EnableMouse(true)
			row.IsActive = true
			row.IsCurrentChar = data.IsCurrentChar and true or false
			row.Highlight:Hide()

			row.Name:SetText(data.Name or "")
			row.Level:SetText(data.Level or "")
			row.ItemLevel:SetText(data.ItemLevel or "")
			row.Realm:SetText(data.Realm or "")
			row.Counter:SetText(data.Counter or "")
			row.Funds:SetText(data.Funds or "")
			row.Data = data
			
			if data.Faction == "Alliance" then
				row.Faction:SetTexture(GC_FactionIconAlliance)
			elseif data.Faction == "Horde" then
				row.Faction:SetTexture(GC_FactionIconHorde)
			else
				row.Faction:SetTexture(GC_FactionIconNeutral)
			end
			
			if row.IsCurrentChar then
				row.CurrentCharBG:Show()
			else
				row.CurrentCharBG:Hide()
			end
			
		else
		
			row.Name:SetText("")
			row.Level:SetText("")
			row.ItemLevel:SetText("")
			row.Realm:SetText("")
			row.Counter:SetText("")
			row.Data = nil
			row.Funds:SetText("")
			row.Faction:SetTexture(nil)

			row:EnableMouse(false)
			row.IsActive = false
			row.IsCurrentChar = false
			row.CurrentCharBG:Hide()
			row.Highlight:Hide()
			
		end
		
	end
	
end
function GC_BuildCharRowTooltip(row)
	if not row or not row.Data then return
	end

	local index = 0
	
	GoldCounter_Custom_Tooltip:SetOwner(row, "ANCHOR_RIGHT")
	GoldCounter_Custom_Tooltip:ClearLines()
	
	-- Name
	index = index + 1
	_G["GoldCounter_Custom_TooltipTextLeft"..index]:SetFontObject( GameTooltipText )
	GoldCounter_Custom_Tooltip:AddLine( row.Data.Name, nil, nil, nil, false )

	-- Guild
	if ( row.Data.GuildName ) then
	index = index + 1
	_G["GoldCounter_Custom_TooltipTextLeft"..index]:SetFontObject( GameTooltipText )
	GoldCounter_Custom_Tooltip:AddLine( "|cff40ff40"..row.Data.GuildName.."|r", nil, nil, nil, false )
	end

	-- Jobs
	if row.Data.Job1 or row.Data.Job2 then

	local FullJobData

	index = index + 1 -- Empty line
	_G["GoldCounter_Custom_TooltipTextLeft"..index]:SetFontObject( GameTooltipTextSmall )
	GoldCounter_Custom_Tooltip:AddLine( " " )

		if ( row.Data.Job1 ) then
			index = index + 1
			FullJobData = row.Data.Job1 .. " (" .. row.Data.Job1SkillLevel .." / " .. row.Data.Job1MaxSkillLevel .. ")"
			
			_G["GoldCounter_Custom_TooltipTextLeft"..index]:SetFontObject( GameTooltipTextSmall )
			GoldCounter_Custom_Tooltip:AddLine( FullJobData , 1, 1, 1, false )
			if row.Data.Job1Icon then
				GoldCounter_Custom_Tooltip:AddTexture( row.Data.Job1Icon )
			end
		end
		if ( row.Data.Job2 ) then
			index = index + 1
			FullJobData = row.Data.Job2 .. " (" .. row.Data.Job2SkillLevel .." / " .. row.Data.Job2MaxSkillLevel .. ")"
			
			_G["GoldCounter_Custom_TooltipTextLeft"..index]:SetFontObject( GameTooltipTextSmall )
			GoldCounter_Custom_Tooltip:AddLine( FullJobData , 1, 1, 1, false )
			if row.Data.Job2Icon then
				GoldCounter_Custom_Tooltip:AddTexture( row.Data.Job2Icon )
			end
		end
	end

	if GC_WOW_isRetail and row.Data.MM_Plus_OverallScore and row.Data.MM_Plus_OverallScore > 0 then

		index = index + 1 -- Empty line
		_G["GoldCounter_Custom_TooltipTextLeft"..index]:SetFontObject( GameTooltipTextSmall )
		GoldCounter_Custom_Tooltip:AddLine( " " )

		--- MM+ Global Score
		index = index + 1
		local Color = C_ChallengeMode.GetDungeonScoreRarityColor(row.Data.MM_Plus_OverallScore)
		local ColorCode = Color:GenerateHexColor()
		local ColoredScore = "|c"..ColorCode..row.Data.MM_Plus_OverallScore.."|r"
		_G["GoldCounter_Custom_TooltipTextLeft"..index]:SetFontObject( GameTooltipTextSmall )
		GoldCounter_Custom_Tooltip:AddLine( "|cffe6cc80" .. GC_Locale_Text.MM_Plus_OverallScore.." :|r " .. ColoredScore, nil, nil, nil, false )

		--- MM+ Highest key and dungeon name
		if row.Data.MM_Plus_HighestKeyLevel and row.Data.MM_Plus_HighestKeyLevel > 0 then
		index = index + 1
		_G["GoldCounter_Custom_TooltipTextLeft"..index]:SetFontObject( GameTooltipTextSmall )
		GoldCounter_Custom_Tooltip:AddLine( "+" .. row.Data.MM_Plus_HighestKeyLevel .. " " .. row.Data.MM_Plus_HighestKeyDungeonName, 1, 1, 1, false )
		end

	end

	GoldCounter_Custom_Tooltip:Show()

end

function GC_SortByFunds()
	tsort(GC_CharListTable, function(a, b)
		return a.FundsRaw > b.FundsRaw
		end)
	GC_UpdateCharList()
end