--         ---------------------------------------------
--        |  TinyCasterStats by TheVaan and ArcaneMagus |
--         ---------------------------------------------

local AceAddon = LibStub("AceAddon-3.0")
local media = LibStub:GetLibrary("LibSharedMedia-3.0")
TinyCasterStats = AceAddon:NewAddon("TinyCasterStats", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("TinyCasterStats")
local isInFight = false

local ldb = LibStub:GetLibrary("LibDataBroker-1.1");
local TCSBroker = ldb:NewDataObject("TinyCasterStats", { 
		type = "data source",
		label = "TinyCasterStats", 
		icon = "Interface\\Icons\\Ability_Mage_ArcaneBarrage",
		text = "--"
	}
)

TinyCasterStats.fonts = {}

TinyCasterStats.defaults = {
	char = {
		Font = "Vera",
		FontEffect = "none",
		Size = 12,
		FrameLocked = true,
		yPosition = 200,
		xPosition = 200,
		Vis = .3,
		RecordMsg = true,
		HighestSpelldmg = 0,
		HighestCrit = 0,
		HighestHaste = 0,
		HighestHastePerc = 0,
		HighestHit = 0,
		HighestMP5if = 0,
		HighestMP5 = 0,
		Style = {
			Spelldmg = true,
			Crit = true,
			Haste = true,
			HastePerc = false,
			Hit = false,
			MP5 = false,
			MaxSpelldmg = false,
			MaxCrit = false,
			MaxHaste = false,
			MaxHastePerc = false,
			MaxHit = false,
			MaxMP5 = false,
		},
	}
}

tcsframe = CreateFrame("Frame","TinyCasterStatsFrame",UIParent)
tcsframe:SetWidth(100)
tcsframe:SetHeight(15)
tcsframe:SetFrameStrata("BACKGROUND")
tcsframe:EnableMouse(true)
tcsframe:RegisterForDrag("LeftButton")

tcsstring = tcsframe:CreateFontString()
tcsstring:SetFontObject(GameFontNormal)
tcsstring:SetPoint("CENTER", tcsframe)
tcsstring:SetJustifyH("CENTER")
tcsstring:SetJustifyV("MIDDLE")

function TinyCasterStats:OnInitialize()
	local AceConfigReg = LibStub("AceConfigRegistry-3.0")
	local AceConfigDialog = LibStub("AceConfigDialog-3.0")
	
	self.db = LibStub("AceDB-3.0"):New("TinyCasterStats", TinyCasterStats.defaults, "char")
	LibStub("AceConfig-3.0"):RegisterOptionsTable("TinyCasterStats", self:Options(), "tcscmd")
	media.RegisterCallback(self, "LibSharedMedia_Registered")
	
	self:RegisterChatCommand("tcs", function() AceConfigDialog:Open("TinyCasterStats") end)	
	self:RegisterChatCommand("TinyCasterStats", function() AceConfigDialog:Open("TinyCasterStats") end)	
	self.optionsFrame = AceConfigDialog:AddToBlizOptions("TinyCasterStats", "TinyCasterStats")
	self.db:RegisterDefaults(self.defaults)
	local version = GetAddOnMetadata("TinyCasterStats","Version")
	local loaded = L["Konfigurationsmen\195\188 mit |c00ff00ff/tcs|cffffd700 oder |c00ff00ff/tinycasterstats|cffffd700 \195\181ffnen|r"]
	DEFAULT_CHAT_FRAME:AddMessage("|cffffd700TinyCasterStats |cff00ff00~v"..version.."~|cffffd700:\n"..loaded)
end

function TinyCasterStats:OnEnable()
	self:LibSharedMedia_Registered()
	self:RegisterEvent("PLAYER_ENTERING_WORLD", "OnEvent")
	self:RegisterEvent("ADDON_LOADED", "OnEvent")
	self:RegisterEvent("VARIABLES_LOADED", "OnEvent")
	self:RegisterEvent("UNIT_AURA", "OnEvent")
	self:RegisterEvent("UPDATE_SHAPESHIFT_FORM", "OnEvent")
	self:RegisterEvent("UNIT_INVENTORY_CHANGED", "OnEvent")
	self:RegisterEvent("UNIT_LEVEL", "OnEvent")
	self:RegisterEvent("PLAYER_REGEN_ENABLED", "OnEvent")
	self:RegisterEvent("PLAYER_REGEN_DISABLED", "OnEvent")
	self:RegisterEvent("PLAYER_TALENT_UPDATE", "OnEvent")
end

function TinyCasterStats:LibSharedMedia_Registered()
	media:Register("font", "BaarSophia", [[Interface\Addons\TinyCasterStats\Fonts\BaarSophia.ttf]])
	media:Register("font", "LucidaSD", [[Interface\Addons\TinyCasterStats\Fonts\LucidaSD.ttf]])
	media:Register("font", "Teen", [[Interface\Addons\TinyCasterStats\Fonts\Teen.ttf]])
	media:Register("font", "Vera", [[Interface\Addons\TinyCasterStats\Fonts\Vera.ttf]])
	
	for k, v in pairs(media:List("font")) do
		self.fonts[v] = v
	end
end

function TinyCasterStats:OnEvent(event, arg1)
	if (event == "ADDON_LOADED") then
		tcsframe:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", self.db.char.xPosition, self.db.char.yPosition)
		local font = media:Fetch("font", self.db.char.Font)
		tcsstring:SetFont(font, self.db.char.Size, self.db.char.FontEffect)
	end
	if ((event == "PLAYER_REGEN_ENABLED") or (event == "PLAYER_ENTERING_WORLD")) then
		tcsframe:SetAlpha(self.db.char.Vis)
		isInFight = false
	end
	if (event == "PLAYER_REGEN_DISABLED") then
		tcsframe:SetAlpha(1)
		isInFight = true
	end
	if (self.db.char.FrameLocked == true) then 
		local fixed = L["|cffFF0000Der Text ist fixiert. Freigeben mit |c00ff00ff/tcs |c001E90FF[Textzeile sperren]|cffFF0000!|r"]
		tcsframe:SetScript("OnDragStart", function() DEFAULT_CHAT_FRAME:AddMessage(fixed) end)
	end
	if (event == "UNIT_AURA" and arg1 == "player") then
		self:ScheduleTimer("Stats", .8)
	end
  	self:Stats()
end

local function GetHaste()
	local haste = GetCombatRating(CR_HASTE_SPELL)
	local bloodlust = GetSpellInfo(UnitFactionGroup("player") == FACTION_ALLIANCE and 32182 or 2825)
	local totem = GetSpellInfo(2895)
	if (UnitAura("player",bloodlust)) then
		haste = (haste * 1.3)
	end
	if (UnitAura("player",totem)) then
		haste = (haste * 1.05)
	end
	return haste
end

local function GetHastePerc()
	local hasteperc = GetCombatRatingBonus(CR_HASTE_SPELL)
	local bloodlust = GetSpellInfo(UnitFactionGroup("player") == FACTION_ALLIANCE and 32182 or 2825)
	local totem = GetSpellInfo(2895)
	if (UnitAura("player",bloodlust)) then
		hasteperc = (hasteperc + 30 * (1 + hasteperc / 100))
	end
	if (UnitAura("player",totem)) then
		hasteperc = (hasteperc + 5 * (1 + hasteperc / 100))
	end
	return hasteperc
end

local function GetSpellDamage()
	local spelldamage = 0
	for i = 2, 7, 1 do
		if (spelldamage < GetSpellBonusDamage(i)) then
			spelldamage = GetSpellBonusDamage(i)
		end
	end
	if (spelldamage < GetSpellBonusHealing()) then 
		spelldamage = GetSpellBonusHealing() 
	end 
	return spelldamage
end

local function GetCrit()
	local critchance = 0
	for i = 2, 7, 1 do
		if (critchance < GetSpellCritChance(i)) then
			critchance = GetSpellCritChance(i)
		end
	end
	return critchance
end

function TinyCasterStats:Stats()
	local style = self.db.char.Style
	local spelldmg = GetSpellDamage()
	local crit = GetCrit()
	local haste = GetHaste()
	local hasteperc = GetHastePerc()
	local hit = GetCombatRatingBonus(CR_HIT_SPELL)
	local base, casting = GetManaRegen()
	base = base * 5
	casting = casting * 5
	local mp5
	local highMP5
	local recordbrocken = L["|cffFF0000Rekord gebrochen!|r"]
	
	if (isInFight == false) then
		mp5 = base
		highMP5 = self.db.char.HighestMP5
	elseif (isInFight == true) then
		mp5 = casting
		highMP5 = self.db.char.HighestMP5if
	end
	
	if (spelldmg > tonumber(self.db.char.HighestSpelldmg)) then
		self.db.char.HighestSpelldmg = spelldmg
		if (self.db.char.RecordMsg == true) then
			DEFAULT_CHAT_FRAME:AddMessage(recordbrocken)
			DEFAULT_CHAT_FRAME:AddMessage("|cffFF0000"..L["Zaubermacht"]..": |c00ffef00"..self.db.char.HighestSpelldmg.."|r")
		end
	end
	if (crit > tonumber(self.db.char.HighestCrit)) then
		self.db.char.HighestCrit = crit
		if (self.db.char.RecordMsg == true) then
			DEFAULT_CHAT_FRAME:AddMessage(recordbrocken)
			DEFAULT_CHAT_FRAME:AddMessage("|cffFF0000"..L["Kritischetrefferchance"]..": |c00ffef00"..string.format("%.2f", self.db.char.HighestCrit).."%|r")
		end
	end
	if (haste > tonumber(self.db.char.HighestHaste)) then
		self.db.char.HighestHaste = haste
		self.db.char.HighestHastePerc = hasteperc
		if (self.db.char.RecordMsg == true) then
			DEFAULT_CHAT_FRAME:AddMessage(recordbrocken)
			DEFAULT_CHAT_FRAME:AddMessage("|cffFF0000"..L["Tempowertung"]..": |c00ffef00"..string.format("%.0f", self.db.char.HighestHaste).."|r")
			DEFAULT_CHAT_FRAME:AddMessage("|cffFF0000"..L["Tempowertung in Prozent"]..": |c00ffef00"..string.format("%.2f", self.db.char.HighestHastePerc).."%|r")
		end
	end
	if (hit > tonumber(self.db.char.HighestHit)) then
		self.db.char.HighestHit = hit
		if (self.db.char.RecordMsg == true) then
			DEFAULT_CHAT_FRAME:AddMessage(recordbrocken)
			DEFAULT_CHAT_FRAME:AddMessage("|cffFF0000"..L["Trefferchance"]..": |c00ffef00"..string.format("%.2f", self.db.char.HighestHit).."%|r")
		end
	end
	if (casting > tonumber(self.db.char.HighestMP5if)) then
		self.db.char.HighestMP5if = casting
		self.db.char.HighestMP5 = base
		if (self.db.char.RecordMsg == true) then
			DEFAULT_CHAT_FRAME:AddMessage(recordbrocken)
			DEFAULT_CHAT_FRAME:AddMessage("|cffFF0000"..L["Manaregeneration pro 5 Sekunden"].." "..L["im Kampf"]..": |c00ffef00"..string.format("%.0f", self.db.char.HighestMP5if).."|r")
			DEFAULT_CHAT_FRAME:AddMessage("|cffFF0000"..L["Manaregeneration pro 5 Sekunden"].." "..L["au\195\159erhalb des Kampfes"]..": |c00ffef00"..string.format("%.0f", self.db.char.HighestMP5).."|r")
		end
	end
			
	local stat = ""
	local oldstat = ""
	
	if (style.Spelldmg == true) then
		stat = "|c00ffef00"..spelldmg.."|r"
	end
	if (style.Crit == true) then
		oldstat = stat
		stat = oldstat.." |c00ff00ff"..string.format("%.2f", crit).."%|r"
	end
	if (style.Hit == true) then
		oldstat = stat
		stat = oldstat.." |c0000CD00"..string.format("%.2f", hit).."%|r"
	end
	if (style.Haste == true) then
		oldstat = stat
		stat = oldstat.." |c001E90FF"..string.format("%.0f", haste).."|r"
	end
	if (style.HastePerc == true) then
		oldstat = stat
		stat = oldstat.." |c001E90FF"..string.format("%.2f", hasteperc).."%|r"
	end
	if (style.MP5 == true) then
		oldstat = stat
		stat = oldstat.." |c00FFFFFF"..string.format("%.0f", mp5).."mp5|r"
	end
	if (style.MaxSpelldmg or style.MaxCrit or style.MaxHaste or style.MaxHastePerc or style.MaxHit) then
		oldstat = stat
		stat = oldstat.."\n"
	end
	if (style.MaxSpelldmg == true) then
		oldstat = stat
		stat = oldstat.."|c00ffef00"..self.db.char.HighestSpelldmg.."|r"
	end
	if (style.MaxCrit == true) then
		oldstat = stat
		stat = oldstat.." |c00ff00ff"..string.format("%.2f", self.db.char.HighestCrit).."%|r"
	end
	if (style.MaxHit == true) then
		oldstat = stat
		stat = oldstat.." |c0000CD00"..string.format("%.2f", self.db.char.HighestHit).."%|r"
	end
	if (style.MaxHaste == true) then
		oldstat = stat
		stat = oldstat.." |c001E90FF"..string.format("%.0f", self.db.char.HighestHaste).."|r"
	end
	if (style.MaxHastePerc == true) then
		oldstat = stat
		stat = oldstat.." |c001E90FF"..string.format("%.2f", self.db.char.HighestHastePerc).."%|r"
	end
	if (style.MaxMP5 == true) then
		oldstat = stat
		stat = oldstat.." |c00FFFFFF"..string.format("%.0f", highMP5).."mp5|r"
	end
	
	tcsstring:SetText(stat)
	TCSBroker.text = stat
end