local addonName, addon = ...





--[[-----------------------------------------------------------------------------
Configure Rep and Exp bars
-------------------------------------------------------------------------------]]
function addon:ConfigureExpBar()
	if addon.settings.showExpBar then
		addon:ShowFrame("GrimExpBar")
		_G["GrimRepBar"]:ClearAllPoints()
		_G["GrimRepBar"]:SetPoint('TOPRIGHT', "GrimExpBar", "BOTTOMRIGHT", 0, 0)
	else
		addon:HideFrame("GrimExpBar")
		_G["GrimRepBar"]:ClearAllPoints()
		_G["GrimRepBar"]:SetPoint('TOPRIGHT', "GUI_PlayerMana", "BOTTOMRIGHT", 0, 0)
	end
end

function addon:ConfigureRepBar()
	if addon.settings.showRepBar then
		addon:ShowFrame("GrimRepBar")
    else
		addon:HideFrame("GrimRepBar")
    end
	
end


---------------------------
-- Xp Bar
-----------------------
local GrimExpBar = CreateFrame('button', "GrimExpBar", UIParent)

GrimExpBar:RegisterEvent("PLAYER_XP_UPDATE")
GrimExpBar:RegisterEvent("PLAYER_LEVEL_UP", "level")
GrimExpBar:RegisterEvent("UPDATE_EXHAUSTION")
GrimExpBar:RegisterEvent("UNIT_PET", "player")
GrimExpBar:RegisterEvent("UNIT_PET_EXPERIENCE", "player")
GrimExpBar:RegisterEvent("PLAYER_GUILD_UPDATE")
GrimExpBar:RegisterEvent("GUILD_XP_UPDATE")

GrimExpBar:SetHeight(20)
GrimExpBar:SetWidth(235)
GrimExpBar:SetFrameStrata("HIGH")
GrimExpBar:SetFrameLevel("4")

local xpBar = CreateFrame("StatusBar", nil, GrimExpBar, "TextStatusBar")
xpBar:SetAllPoints("GrimExpBar")
xpBar:SetStatusBarTexture([[Interface\AddOns\]] .. addonName .. [[\Media\BarTexture]])
xpBar:SetStatusBarColor(0.5, 0.1, 0.9, 1)
xpBar:SetFrameLevel("3")

local xpText = xpBar:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
xpText:SetPoint("LEFT", GrimExpBar, "LEFT", 3, -2)
xpText:SetTextColor(1, 1, 1, 1)
xpText:SetFont([[Interface\AddOns\]] .. addonName .. [[\Media\Font-1.ttf]], 11.5, "OUTLINE")

local restXPbar = CreateFrame("StatusBar", nil, GrimExpBar, "TextStatusBar")
restXPbar:SetAllPoints("GrimExpBar")
restXPbar:SetStatusBarTexture([[Interface\AddOns\]] .. addonName .. [[\Media\BarTexture]])
restXPbar:SetStatusBarColor(.290, .305, .878, 1)
restXPbar:SetFrameLevel("2")

local bg = xpBar:CreateTexture(nil, "BACKGROUND")
bg:SetTexture(0, 0, 0, 0)
bg:SetAllPoints("GrimExpBar")

local function GXPBarEvent()
	local currXP, nextXP = UnitXP("Player"), UnitXPMax("Player") 
	local leftXP = (nextXP - currXP)
	local _, myClass = UnitClass("Player")
	local petXPcur, petXPnex = GetPetExperience()
	local petXPleft = (petXPnex - petXPcur)
	local restXP, percentXP = GetXPExhaustion(), floor(currXP / nextXP * 100) 
	
	local str 
	
	local currentXP, nextLevelXP, dailyXP, maxDailyXP, unitWeeklyXP, unitTotalXP = UnitGetGuildXP("player")
	local GXPTot = (nextLevelXP + currentXP)
	local GleftXP = (nextLevelXP - currentXP)
	
	if addon.settings.showGuild == true then
		if addon.settings.xpBarvar == "xpLeft" then
			str = ("%s left - Guild"):format(nextLevelXP)
		elseif addon.settings.xpBarvar == "xpTot" then
			str = ("%s/%s Guild"):format(currentXP, GXPTot)
		end
		xpText:SetText(str) 
		xpBar:SetMinMaxValues(min(0, currentXP), GXPTot) 
		xpBar:SetValue(currentXP)
		if maxDailyXP ~= nil then
		restXPbar:SetMinMaxValues(min(0, currentXP), maxDailyXP) -- dont forget restXP must be a value or nil must be delt with
		restXPbar:SetValue(maxDailyXP) -- dont forget restXP must be a value or nil must be delt with
		end
	else
		if restXP == nil then -- nill is being delt with
			if addon.settings.xpBarvar == "xpLeft" and myClass == "HUNTER" then 
			str = ("%s left  %s Pet"):format(leftXP,  petXPleft) 
			elseif addon.settings.xpBarvar == "xpLeft" and myClass ~= "HUNTER" then
			str = ("%s left "):format(leftXP)
			elseif addon.settings.xpBarvar == "xpTot" and myClass == "HUNTER" then
			str = ("%s/%s  P %s/%s"):format(currXP, nextXP, petXPcur, petXPnex)
			elseif addon.settings.xpBarvar == "xpTot" and myClass ~= "HUNTER" then
			str = ("%s/%s "):format(currXP, nextXP)
			end
			restXPbar:SetMinMaxValues(min(0, currXP), nextXP) -- dont forget restXP must be a value or nil must be delt with
			restXPbar:SetValue(currXP) -- dont forget restXP must be a value or nil must be delt with
		else 
			local percentRestXP = floor(restXP / nextXP * 100)
			if addon.settings.xpBarvar == "xpLeft" and myClass == "HUNTER" then 
			str = ("%s left (%s %s%%R) P %s"):format(leftXP, restXP, percentRestXP, petXPleft) 
			elseif addon.settings.xpBarvar == "xpLeft" and myClass ~= "HUNTER" then
			str = ("%s left (%s %s%%R)"):format(leftXP, restXP, percentRestXP)
			elseif addon.settings.xpBarvar == "xpTot" and myClass == "HUNTER" then
			str = ("%s/%s (%s%%R) P %s/%s"):format(currXP, nextXP, percentRestXP, petXPcur, petXPnex)
			elseif addon.settings.xpBarvar == "xpTot" and myClass ~= "HUNTER" then
			str = ("%s/%s (%s%%R)"):format(currXP, nextXP, percentRestXP)
			end
			restXPbar:SetMinMaxValues(min(0, restXP), nextXP) -- dont forget restXP must be a value or nil must be delt with
			restXPbar:SetValue(currXP + restXP) -- dont forget restXP must be a value or nil must be delt with
		
		end
		xpText:SetText(str) 
		xpBar:SetMinMaxValues(min(0, currXP), nextXP) 
		xpBar:SetValue(currXP) 
	end
end


GrimExpBar:SetScript("OnEvent", function(self) 
	GXPBarEvent()
end)

GrimExpBar:RegisterForClicks('LeftButtonUp', 'RightButtonUp')
GrimExpBar:SetScript('OnClick', function(self, button)
	if button == "LeftButton" then
		if addon.settings.xpBarvar == "xpLeft" then
			addon.settings.xpBarvar = "xpTot"
		elseif addon.settings.xpBarvar == "xpTot" then
			addon.settings.xpBarvar = "xpLeft"
		end
		
		
		
		GrimExpBar:GetScript("OnEvent")(GrimExpBar)
	else
		if addon.settings.showGuild == false then
			addon.settings.showGuild = true
		elseif addon.settings.showGuild == true then
			addon.settings.showGuild = false
		end
		GrimExpBar:GetScript("OnEvent")(GrimExpBar)
	end	
end)

------------------------------
-- Rep Bar
------------------------------

local GrimRepBar = CreateFrame('button', "GrimRepBar", UIParent)

GrimRepBar:RegisterEvent("UPDATE_FACTION")

GrimRepBar:SetHeight(20)
GrimRepBar:SetWidth(235)
GrimRepBar:SetFrameStrata("HIGH")
GrimRepBar:SetFrameLevel("4")

local repBar = CreateFrame("StatusBar", nil, GrimRepBar, "TextStatusBar")
repBar:SetAllPoints("GrimRepBar")
repBar:SetStatusBarTexture([[Interface\AddOns\]] .. addonName .. [[\Media\BarTexture]])

repBar:SetFrameLevel("3")

local repText = repBar:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
repText:SetPoint("LEFT", GrimRepBar, "LEFT", 3, -2)
repText:SetTextColor(1, 1, 1, 1)
repText:SetFont([[Interface\AddOns\]] .. addonName .. [[\Media\Font-1.ttf]], 11.5, "OUTLINE")

local function GREPBarEvent()
	local factionRep, curStanding, minRep, maxRep, curRep = GetWatchedFactionInfo()
	local leftRep = (maxRep - curRep)
	
	if factionRep == nil then
		repText:SetText("No Watched Faction")
	elseif factionRep ~= nil then
		if addon.settings.repBarvar == "repTot" then
			repstr = ("%s/%s %s"):format(curRep, maxRep, factionRep)
		elseif addon.settings.repBarvar == "repLeft" then
			repstr = ("%s left - %s"):format(leftRep, factionRep)
		end
		repText:SetText(repstr)
	end
	repBar:SetMinMaxValues(min(0, curRep), maxRep) 
	repBar:SetValue(curRep) 
	repBar:SetStatusBarColor(0.5, 0.1, 0.9, 1)
end

GrimRepBar:SetScript("OnEvent", function(self)
	GREPBarEvent()
end)

GrimRepBar:RegisterForClicks('LeftButtonUp', 'RightButtonUp')
GrimRepBar:SetScript('OnClick', function(self, button)
	if button == "LeftButton" then
		if addon.settings.repBarvar == "repLeft" then
			addon.settings.repBarvar = "repTot"
		elseif addon.settings.repBarvar == "repTot" then
			addon.settings.repBarvar = "repLeft"
		end
		GrimRepBar:GetScript("OnEvent")(GrimRepBar)
	end	
end)


function addon:ResetExpRepBar()
	_G["GrimExpBar"]:ClearAllPoints()
	_G["GrimExpBar"]:SetPoint('TOPRIGHT', 'GUI_PlayerMana', "BOTTOMRIGHT", 0, 0)
	
	if addon.settings.showExpBar then
		_G["GrimRepBar"]:ClearAllPoints()
		_G["GrimRepBar"]:SetPoint('TOPRIGHT', "GrimExpBar", "BOTTOMRIGHT", 0, 0)
	else
		_G["GrimRepBar"]:ClearAllPoints()
		_G["GrimRepBar"]:SetPoint('TOPRIGHT', "GUI_PlayerMana", "BOTTOMRIGHT", 0, 0)
	end
end

--[[-----------------------------------------------------------------------------
Initialize
-------------------------------------------------------------------------------]]
addon.RegisterEvent("InitializeExpRepBar", 'PLAYER_ENTERING_WORLD', function(self, event)
	addon.UnregisterEvent(self, event)
	
	addon:ResetExpRepBar()
	addon:ConfigureExpBar()
	addon:ConfigureRepBar()
	GXPBarEvent()
	GREPBarEvent()
	ToggleGuildFrame()
	ToggleGuildFrame()
end)