local addonName, addon = ...

addon:RegisterDefaultSetting("hudShow", true)
addon:RegisterDefaultSetting("hudWidth", 150)
addon:RegisterDefaultSetting("hudHeight", 250)



local GHudMain = CreateFrame("Frame", "GHudMain", UIParent)

function addon:SetupGHud()
	if addon.settings.hudShow then 
		GHudMain:Show()
	else
		GHudMain:Hide()
	end
end



local PlayerHealthEvents = CreateFrame("Frame", nil, UIParent)
PlayerHealthEvents:RegisterEvent("PLAYER_ENTERING_WORLD")
PlayerHealthEvents:RegisterEvent("UNIT_HEALTH")
PlayerHealthEvents:RegisterEvent("PLAYER_LEVEL_UP")
PlayerHealthEvents:RegisterEvent("UNIT_AURA", "Player")
PlayerHealthEvents:RegisterEvent("UNIT_MAXHEALTH")



local healthBar = CreateFrame("frame", nil, GHudMain)
healthBar:SetPoint("CENTER", UIParent, "CENTER", -100, 0)
healthBar:SetHeight(250)
healthBar:SetWidth(125)
healthBar:SetFrameLevel("3")
local healthbarBG = healthBar:CreateTexture(nil, "BACKGROUND")
healthbarBG:SetTexture("Interface\\AddOns\\GrimUI\\HudSkins\\bg_1")
healthbarBG:SetAllPoints(healthBar)
local healthbarOverlay = healthBar:CreateTexture(nil, "OVERLAY")
healthbarOverlay:SetTexture("Interface\\AddOns\\GrimUI\\HudSkins\\1")
healthbarOverlay:SetWidth(125)

healthbarOverlay:SetAllPoints(healthBar)

--healthBar:SetStatusBarTexture([[Interface\AddOns\]] .. addonName .. [[\HudSkins\2]])
--healthBar:SetStatusBarColor(.231, .682, .419, 1)

PlayerHealthEvents:SetScript("OnEvent", function(self) 
    local mycurhealth
	local currHealth, maxHealth = UnitHealth("Player"), UnitHealthMax("Player") 
   
    --healthBar:SetMinMaxValues(min(0, currHealth), maxHealth) 
    --healthBar:SetValue(currHealth) 

end)


local manaBarEventFrame = CreateFrame("Frame", nil, UIParent)
manaBarEventFrame:RegisterEvent('PLAYER_ENTERING_WORLD')
manaBarEventFrame:RegisterEvent('UNIT_HEALTH')
manaBarEventFrame:RegisterEvent('PLAYER_LEVEL_UP')
manaBarEventFrame:RegisterEvent('UNIT_MANA')
manaBarEventFrame:RegisterEvent('UNIT_AURA')
manaBarEventFrame:RegisterEvent('UNIT_DISPLAYPOWER')
manaBarEventFrame:RegisterEvent('UNIT_ENERGY')
manaBarEventFrame:RegisterEvent('UNIT_MAXENERGY')
manaBarEventFrame:RegisterEvent('UNIT_MAXMANA')
manaBarEventFrame:RegisterEvent('UNIT_MAXRUNICPOWER')
manaBarEventFrame:RegisterEvent('UNIT_RUNIC_POWER')
manaBarEventFrame:RegisterEvent('UNIT_RAGE')
manaBarEventFrame:RegisterEvent('UPDATE_SHAPESHIFT_FORM')

local manaBar = CreateFrame("StatusBar", "manaBar", GHudMain, "TextStatusBar")
manaBar:SetHeight(250)
manaBar:SetWidth(125)
manaBar:SetPoint("CENTER", UIParent, "CENTER", 100, 0)
manaBar:SetStatusBarTexture([[Interface\AddOns\]] .. addonName .. [[\HudSkins\2]])
manaBar:SetFrameLevel("3")
manaBar:SetOrientation("vertical")
--manaBar:SetTexCoord(1, 0, 0, 1)

manaBarEventFrame:SetScript("OnEvent", function(self) 
    local currMana, maxMana = UnitMana("Player"), UnitManaMax("Player") 
    local powerType = UnitPowerType("Player")
    local currPower = UnitPower("Player")
    local maxPower = UnitPowerMax("Player")

    if powerType == 0 then
        manaBar:SetMinMaxValues(min(0, currMana), maxMana) 
        manaBar:SetValue(currMana) 
        manaBar:SetStatusBarColor(.556, .556, .921, 1)
    elseif powerType == 1 or 3 or 6 then
        manaBar:SetMinMaxValues(min(0, currPower), maxPower) 
        manaBar:SetValue(currPower) 
        manaBar:SetStatusBarColor(.656, .456, .456, 1)
    end
end)














addon.RegisterEvent("HUD-Initialize", 'PLAYER_ENTERING_WORLD', function(self, event)
	addon.UnregisterEvent(self, event)

	addon:SetupGHud()
	

end)