--== Settings  ==--
stSettings.Gold = true
stSettings.Gold_Format = "Short" -- can be Short, Full and Extended

--== Coding ==--
local f = CreateFrame("Frame",nil,UIParent)
f:RegisterEvent"VARIABLES_LOADED"
-- Colors
local pClass = select(2, UnitClass("player"))
local CColor = { RAID_CLASS_COLORS[pClass].r, RAID_CLASS_COLORS[pClass].g, RAID_CLASS_COLORS[pClass].b }
local db = stSettings
local color
if db.ColorClass then
	color = CColor
else
	color = db.Color
end

--== Utils ==--
local function Hex(r, g, b)
	if type(r) == "table" then
		if r.r then r, g, b = r.r, r.g, r.b else r, g, b = unpack(r) end
	end
	return string.format("|cff%02x%02x%02x", r*255, g*255, b*255)
end
local colorCode = Hex(color)

local function FormatMoney(money, display)
   local gold, silver, copper = abs(money / 10000), abs(mod(money / 100, 100)), abs(mod(money, 100))
   local cash = ""

   if ( display == "Extended" ) then
     cash = format("%d |cffffd700".."Gold".."|r %d |cffc7c7cf".."Silver".."|r %d |cffeda55f".."Copper".."|r", gold, silver, copper)
   elseif ( display == "Full" ) then
     cash = format("%d|cffffd700g|r %d|cffc7c7cfs|r %d|cffeda55fc|r", gold, silver, copper)
   elseif ( display == "Short" ) then
     cash = format("%.1f|cffffd700g|r", gold)
   end
   return cash
end

f:SetScript("OnEvent",function()
  --== Gold ==--
   if(db.Gold) then
  	   local gold_f, gold_text, bg = stCore:CreateText("Gold")
	   local gold_ismoving, gold_movable = false, false
	   local gold_tip = CreateFrame("Button","st_GoldButton",gold_f)
  	   
      local money, sessionGoldStart, sessionGoldEarned = 0, 0, 0
      local gold, silver, copper = 0,0,0
      local cash = ""
      local loggedOn = true
      
      local myPlayerRealm = GetCVar("realmName");
      local myPlayerName  = UnitName("player");
      
   	if (stText_Gold_DB == nil) then stText_Gold_DB = {}; end
      if (stText_Gold_DB[myPlayerRealm]==nil) then stText_Gold_DB[myPlayerRealm]={}; end
      stText_Gold_DB[myPlayerRealm][myPlayerName] = GetMoney();

	   gold_tip:SetAllPoints()
	   gold_tip:SetScript("OnMouseDown",function(self) if(gold_movable) then  if (IsAltKeyDown()) then gold_f:ClearAllPoints(); gold_f:StartMoving(); gold_ismoving = true; GameTooltip:Hide()  end end end)
	   gold_tip:SetScript("OnMouseUp",function(self) gold_f:StopMovingOrSizing(); gold_ismoving = false; stTextDB.Gold = {gold_f:GetPoint()} end)
	   gold_tip:SetScript("OnLeave", function()  GameTooltip:Hide() end)

  	   local UpdateGold = function(self,elapsed)
         money = GetMoney()
         if ( loggedOn ) then
            sessionGoldStart = money
            loggedOn = false
         else
            if ( money - sessionGoldStart ~= 0 ) then
              sessionGoldEarned = money - sessionGoldStart
            end
         end
         stText_Gold_DB[myPlayerRealm][myPlayerName] = money
         gold_text:SetText( FormatMoney(money, db.Gold_Format) )
  	   end
  	   
   	gold_f:SetScript("OnUpdate",UpdateGold)

  	 	gold_tip:SetScript("OnEnter", function(self) if not gold_ismoving and not InCombatLockdown() then
   		local showBelow = select(2, self:GetCenter()) > UIParent:GetHeight()/2
         GameTooltip:SetOwner( self, "ANCHOR_NONE" )
         GameTooltip:SetPoint( showBelow and "TOP" or "BOTTOM", self, showBelow and "BOTTOM" or "TOP" )
         
         GameTooltip:AddLine("Gold Stats")
         GameTooltip:AddDoubleLine(". . . . . . . . . . .", ". . . . . . . . . . .", 1, 1, 1, 1, 1, 1)
         
         GameTooltip:AddDoubleLine("Starting Gold",FormatMoney(money, "Full"), 1, 1, 1, 1, 1, 1)
         GameTooltip:AddDoubleLine("Current Gold",FormatMoney(sessionGoldStart, "Full"), 1, 1, 1, 1, 1, 1)
         if ( sessionGoldEarned < 0 ) then
            GameTooltip:AddDoubleLine("Gold Earned","-("..FormatMoney(sessionGoldEarned, "Full")..")", 1, 1, 1, 1, 1, 1)
         else
            GameTooltip:AddDoubleLine("Gold Earned",FormatMoney(sessionGoldEarned, "Full"), 1, 1, 1, 1, 1, 1)
         end
         
         GameTooltip:AddLine(" ")

         GameTooltip:AddLine("Server Gold")
         GameTooltip:AddDoubleLine(". . . . . . . . . . .", ". . . . . . . . . . .", 1, 1, 1, 1, 1, 1)
         
         local totalGold = money
         
         if (stText_Gold_DB == nil) then
            GameTooltip:AddLine("Gold Data Unavailable")
         else
            local thisRealmList = stText_Gold_DB[myPlayerRealm];
            for k,v in pairs(thisRealmList) do
               if k ~= myPlayerName then
                  GameTooltip:AddDoubleLine(k, FormatMoney(v, "Full"), 1, 1, 1, 1, 1, 1)
			         totalGold=totalGold+v;
               end
            end --thisRealmList
         end
         GameTooltip:AddDoubleLine(". . . . . . . . . . .", ". . . . . . . . . . .", 1, 1, 1, 1, 1, 1)
         GameTooltip:AddDoubleLine("Total Gold", FormatMoney(totalGold, "Full"), 1, 1, 1, 1, 1, 1)

        	GameTooltip:Show()
    	end
    	end)

   gold_f.Slash = function()
		if bg:IsShown() then
			bg:Hide()
			--dura_f:EnableMouse("false")
			--dura_f:SetMovable"false"
			gold_movable = false
			print("|cff00efffstText: |rGold module locked")
		else
			bg:Show()
			--dura_f:EnableMouse()
			--dura_f:SetMovable()
			gold_movable = true
			print("|cff00efffstText: |rGold module locked")
		end
	end
  	
  	
  end

end)

