--[[
	Copyright (c) 2009, dr_AllCOM3
    All rights reserved.

    You're allowed to use this addon, free of monetary charge,
    but you are not allowed to modify, alter, or redistribute
    this addon without express, written permission of the author.
]]

local addon = LibStub( "AceAddon-3.0" ):NewAddon( "DocsDebugRunes", "AceEvent-3.0" )--
local L = LibStub( "AceLocale-3.0" ):GetLocale( "DocsDebugRunes" )

--##############################
--##############################
--############################## Config Start ###

local width = 20
local height = 20
local spacing = 4
local texture = "Interface\\Addons\\DocsDebugRunes\\statusbar" -- Standard: "Interface\\TargetingFrame\\UI-StatusBar"
local font = "Interface\\Addons\\DocsDebugRunes\\font.ttf" -- Standard: "Fonts\\FRIZQT__.TTF"
local enable_diseases = false
local enable_runicPower = false
local enable_procs = false


--############################## Config End ###
--##############################
--##############################

local temp
DocsDebugRunesDB = DocsDebugRunesDB or {}
local debug = true
local expansionLevel = GetAccountExpansionLevel()

local function p( text ) -- Print
	ChatFrame3:AddMessage( tostring( text ) )
end

local function lightenColor( r, g, b )
    local modifier = 1.5
    
    return r+( 1-r )/modifier, g+( 1-g )/modifier, b+( 1-b )/modifier
end

function round( num, idp ) --number, decimal places
	local mult = 10^( idp or 0 )
	return math.floor( num * mult + 0.5 ) / mult
end

local function formatTime( time )
    if time>60 then
        time = floor( time/60 ).."m"
    elseif time>=3 then
        time = floor( time )
    else
        time = round( time, 1 )
    end
    
    return time
end

local function createImprovedStatusbar( self, existingBar ) -- Creates a texture that acts like a statusbar. This gets rid of the ugly tiling.
    local function UpdateBar( self )
        local fraction = .01
        local range = self.MaxVal-self.MinVal 
        local value = self.Value-self.MinVal
        local barsize = self.Dim or 100
        
        if range>0 and value>0 and range>=value then
            fraction = value/range
        else
            fraction = .0001
        end
        
        if self.Orientation=="VERTICAL" then 
            self.Bar:SetHeight( barsize*fraction )
            self.Bar:SetTexCoord( 0, 1, 1-fraction, 1 )
        else 
            self.Bar:SetWidth( barsize*fraction ) 
            self.Bar:SetTexCoord( 0, fraction, 0, 1 )
        end
    end
    
    local function UpdateSize( self ) 
        if self.Orientation=="VERTICAL" then
            self.Dim = self:GetHeight()
        else
            self.Dim = self:GetWidth()
        end
        
        UpdateBar( self ) 
    end
    
    local function SetValue( self, value )
        if value>=self.MinVal and value<=self.MaxVal then self.Value = value end
        
        UpdateBar( self ) 
    end
    
    local function GetValue( self )
        return self.Value
    end
    
    local function SetMinMaxValues( self, minval, maxval )
        if maxval>minval then
            self.MinVal = minval
            self.MaxVal = maxval
        else 
            self.MinVal = 0
            self.MaxVal = 100
        end
        
        if self.Value>self.MaxVal then self.Value = self.MaxVal
        elseif self.Value<self.MinVal then self.Value = self.MinVal end
        
        UpdateBar( self ) 
    end
    
    
    local function GetMinMaxValues( self )
        return self.MinVal, self.MaxVal
    end
    
    
    local function SetOrientation( self, orientation ) 
        if orientation=="VERTICAL" then
            self.Orientation = orientation
            self.Bar:ClearAllPoints()
            self.Bar:SetPoint( "BOTTOMLEFT" )
            self.Bar:SetPoint( "BOTTOMRIGHT" )
        else
            self.Orientation = "HORIZONTAL"
            self.Bar:ClearAllPoints()
            self.Bar:SetPoint( "TOPLEFT" )
            self.Bar:SetPoint( "BOTTOMLEFT" )
        end
        
        UpdateSize( self )
    end
    
    local function SetStatusBarColor( self, r, g, b, a )
        self.Bar:SetVertexColor( r, g, b, a )
    end
    
    local function SetStatusBarTexture( self, texture )
        self.Bar:SetTexture( texture )
    end
    
    local statusbar
    if not existingBar then
        statusbar = CreateFrame( "Frame", nil, self )
    else
        statusbar = existingBar
    end
    --statusbar:SetPoint( "TOP", self, "TOP", 0, 0 )
    statusbar:SetWidth( width )
    statusbar:SetHeight( height )
    
    if not statusbar.Bar then
        temp = statusbar:CreateTexture( nil, "ARTWORK" )
    else
        temp = statusbar.Bar
    end
    statusbar.Bar = temp
    
    statusbar.Value = 100
    statusbar.MinVal = 0
    statusbar.MaxVal = 100
    statusbar.Orientation = "HORIZONTAL"
    statusbar.UpdateSize = UpdateSize
	statusbar.SetValue = SetValue
    statusbar.GetValue = GetValue
	statusbar.SetMinMaxValues = SetMinMaxValues
    statusbar.GetMinMaxValues = GetMinMaxValues
	statusbar.SetOrientation = SetOrientation
	statusbar.SetStatusBarColor = SetStatusBarColor
	statusbar.SetStatusBarTexture = SetStatusBarTexture
    
    --statusbar:SetFrameLevel( level )
    statusbar:SetOrientation( "HORIZONTAL" )
    statusbar:SetStatusBarTexture( texture )
    --DocsUI_Nameplates.setBorder( statusbar )
	
	statusbar:SetScript( "OnSizeChanged", UpdateSize )
	UpdateSize( statusbar )
    
    return statusbar
end

local runes = {
    {
        { rune=1, ready = true, runeType = 1, duration = 10, start = 0 },    -- RUNETYPE_BLOOD = 1
        { rune=2, ready = true, runeType = 1, duration = 10, start = 0 },    -- RUNETYPE_DEATH = 2
    },
    {
        { rune=1, ready = true, runeType = 2, duration = 10, start = 0 },    -- RUNETYPE_FROST = 3
        { rune=2, ready = true, runeType = 2, duration = 10, start = 0 },    -- RUNETYPE_CHROMATIC = 4
    },
    {
        { rune=1, ready = true, runeType = 3, duration = 10, start = 0 },
        { rune=2, ready = true, runeType = 3, duration = 10, start = 0 },
    },
}

local colors = {
    { 0.51, 0.0, 0.0, 1 }, -- RUNETYPE_BLOOD = 1
    { 0.05, 0.43, 0.0, 1 }, -- RUNETYPE_DEATH = 2
    { 0, 0.31, 0.56, 1 }, -- RUNETYPE_FROST = 3
    { 0.51, 0.23, 0.65, 1 }, -- RUNETYPE_CHROMATIC = 4
}

local colorsCooldown = {
    { 1, 0, 0, 0.33 }, -- RUNETYPE_BLOOD = 1
    { 0, 1, 0, 0.33 }, -- RUNETYPE_DEATH = 2
    { 0, 0, 1, 0.33 }, -- RUNETYPE_FROST = 3
    { 1, 0, 1, 0.33 }, -- RUNETYPE_CHROMATIC = 4
}

-- onUpdate
local function onUpdate( self, elapsed )
    local time = GetTime()
    
    if enable_diseases and self.timer then -- Diseases
        self:SetValue( self.expirationTime-time )
        self.timer:SetText( formatTime( round( self.expirationTime-time, 1 ) ) )
        if debug then
            self.text:SetText( formatTime( round( self.expirationTime-time, 1 ) ) .." | ".. formatTime( round( self.duration, 1 ) ) )
        end
        
        if self.expirationTime-time<=0 then
            self.timer:SetText( nil )
            self.text:SetText( nil )
            self:Hide()
        end
    else -- Runes
        local duration = self.duration+elapsed
        
        if duration>=self.max then
            self.text:SetText( nil )
            self:SetScript( "OnUpdate", nil )
        else
            if debug then
                self.text:SetText( formatTime( round( (self.max - self.duration) , 1 ) ) --[[.." | ".. formatTime( round( self.max, 1 ) )]] )
            end
            self.duration = duration
            self:SetValue( duration )
        end
    end
end

local frames = {}
local function createFrames()
    temp = CreateFrame( "Frame", "DocsDebugRunesFrame", UIParent )
    temp:SetWidth( 146.5 )
    temp:SetHeight( 26 )
   temp:SetBackdrop( { 
      bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",--[[, 
      edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border", tile = false, tileSize = 16, edgeSize = 16,  ]]
      insets = { left = -3, right = 3, top = -3, bottom = 2 }
    } )
    temp:SetBackdropBorderColor( 0.5, 0.5, 0.5, 1 )
    temp:SetBackdropColor( 0.1, 0.1, 0.1, 1 )
    temp:EnableMouse( true )
    temp:SetClampedToScreen( true )
    temp:SetMovable( true )
    --temp:SetPoint( DocsDebugRunesDB.point or "CENTER", UIParent, DocsDebugRunesDB.point or "CENTER", DocsDebugRunesDB.xOfs or 0, DocsDebugRunesDB.yOfs or 0 )
    temp:SetScript( "OnMouseDown", function( self ) if IsShiftKeyDown() then self:StartMoving() end end)
    temp:SetScript( "OnMouseUp", function( self, button )
            if button=="RightButton" then
                debug = debug
                --ChatFrame1:AddMessage( "DocsDebugRunes Debug: "..tostring( debug ) )
            else
                self:StopMovingOrSizing()
                
                local point, relativeTo, relativePoint, xOfs, yOfs = self:GetPoint( 1 )
                DocsDebugRunesDB.point = point
                DocsDebugRunesDB.xOfs = xOfs
                DocsDebugRunesDB.yOfs = yOfs
            end
        end )
    frames.bg = temp
    
    for i=1,6 do
        frames[i] = createImprovedStatusbar( frames.bg, frames[i] )
        frames[i]:SetWidth( width )
        frames[i]:SetHeight( height )
        frames[i]:SetStatusBarColor( 1, 1, 1, 1 )
        frames[i]:SetMinMaxValues( 0, 1 )
        frames[i]:SetValue( 1 )
        frames[i]:UpdateSize( frames[i] )
        
        frames[i].bg = frames[i]:CreateTexture( nil, "BACKGROUND" )
        frames[i].bg:SetAllPoints( frames[i] )
        frames[i].bg:SetTexture( texture )
        
        frames[i].text = frames[i]:CreateFontString( nil, "OVERLAY", "GameFontNormal" )
        --frames[i].text:SetWidth( width )
        --frames[i].text:SetHeight( height )
        frames[i].text:SetFont( font, 12, "OUTLINE" )
        frames[i].text:SetJustifyH( "CENTER" )
        frames[i].text:SetJustifyV( "CENTER" )
        frames[i].text:SetTextColor( 1, 1, 1, 1 )
        if i==1 or i==3 or i==5 then
            frames[i].text:SetPoint( "CENTER", frames[i], "CENTER", 1, 0 )
        else
            frames[i].text:SetPoint( "CENTER", frames[i], "CENTER", 1, 0 )
        end
        --frames[i].text:SetText( "test" )
        
        frames[i].start = 0
        frames[i].max = 0
        frames[i].duration = 0
        frames[i].switched = false
        frames[i].lastUpdate = 0
        frames[i].onUpdate = onUpdate
    end
    
    -- Rune order
    do
    frames[5]:SetPoint( "TOPLEFT", frames.bg, "TOPLEFT", 0, 0 )
    frames[5]:SetStatusBarColor( unpack(colors[3] ) )
    frames[6]:SetPoint( "TOPLEFT", frames[5], "TOPLEFT", 24, 0 )
    frames[6]:SetStatusBarColor( unpack(colors[3] ) )
    frames[3]:SetPoint( "TOPLEFT", frames[5], "TOPLEFT", 24*2, 0 )
    frames[3]:SetStatusBarColor( unpack(colors[2] ) )
    frames[4]:SetPoint( "TOPLEFT", frames[5], "TOPLEFT", 24*3, 0 )
    frames[4]:SetStatusBarColor( unpack(colors[2] ) )
    frames[1]:SetPoint( "TOPLEFT", frames[5], "TOPLEFT", 24*4, 0 )
    frames[1]:SetStatusBarColor( unpack(colors[1] ) )
    frames[2]:SetPoint( "TOPLEFT", frames[5], "TOPLEFT", 24*5, 0 )
    frames[2]:SetStatusBarColor( unpack(colors[1] ) )
    end
    
    -- Runic power
    do
    frames.rp = createImprovedStatusbar( frames.bg, frames.rp )
    frames.rp:SetWidth( width*2+spacing*1 )
    frames.rp:SetHeight( height )
    frames.rp:UpdateSize( frames.rp )
    frames.rp:SetStatusBarColor( 0, 0.82, 1, 1 )
    frames.rp:SetPoint( "TOP", frames.bg, "BOTTOM", 0, 0 )
    frames.rp:SetMinMaxValues( 0, UnitManaMax( "player" ) )
    if not enable_runicPower then frames.rp:Hide() end
    
    frames.rp.text = frames.rp:CreateFontString( nil, "OVERLAY", "GameFontNormal" )
    --frames.rp.text:SetWidth( width )
    --frames.rp.text:SetHeight( height )
    frames.rp.text:SetFont( font, 14, "THINOUTLINE" )
    frames.rp.text:SetJustifyH( "CENTER" )
    frames.rp.text:SetJustifyV( "CENTER" )
    frames.rp.text:SetTextColor( 0, 0.82, 1, 1 )
    frames.rp.text:SetPoint( "CENTER", frames.rp, "CENTER", 0, 0 )
    
    frames.rp.bg = frames.rp:CreateTexture( nil, "BACKGROUND" )
    frames.rp.bg:SetTexture( "Interface\\Buttons\\WHITE8X8" )
    frames.rp.bg:SetAllPoints( frames.rp )
    frames.rp.bg:SetVertexColor( 0, 0.205, 0.25, 1 )
    end
    
    -- Diseases
    do
    frames.bp = createImprovedStatusbar( frames.bg, frames.bp )
    frames.bp:SetWidth( width )
    frames.bp:SetHeight( height )
    frames.bp:UpdateSize( frames.bp )
    frames.bp:SetStatusBarColor( 0.5, 0, 0.6, 1 )
    frames.bp:SetPoint( "BOTTOMLEFT", frames.bg, "TOPLEFT", spacing*2, 0 )
    frames.bp.lastUpdate = 0
    frames.bp:SetScript( "OnUpdate", onUpdate )
    frames.bp:Hide()
    
    frames.bp.timer = frames.bp:CreateFontString( nil, "OVERLAY", "GameFontNormal" )
    frames.bp.timer:SetFont( font, 14, "THINOUTLINE" )
    frames.bp.timer:SetJustifyH( "CENTER" )
    frames.bp.timer:SetJustifyV( "CENTER" )
    frames.bp.timer:SetTextColor( lightenColor( 0.5, 0, 0.6 ) )
    frames.bp.timer:SetPoint( "CENTER", frames.bp, "TOP", 0, 0 )
    
    frames.bp.text = frames.bp:CreateFontString( nil, "OVERLAY", "GameFontNormal" )
    --frames.bp.text:SetWidth( width )
    --frames.bp.text:SetHeight( height )
    frames.bp.text:SetFont( font, 12, "THINOUTLINE" )
    frames.bp.text:SetJustifyH( "CENTER" )
    frames.bp.text:SetJustifyV( "CENTER" )
    frames.bp.text:SetTextColor( 0.66, 0.66, 0.66, 1 )
    frames.bp.text:SetPoint( "RIGHT", frames.bp, "LEFT", -spacing*2, 0 )
    
    frames.bp.bg = frames.bp:CreateTexture( nil, "BACKGROUND" )
    frames.bp.bg:SetTexture( "Interface\\Buttons\\WHITE8X8" )
    frames.bp.bg:SetAllPoints( frames.bp )
    frames.bp.bg:SetVertexColor( 0.125, 0, 0.15, 1 )
    
    frames.ff = createImprovedStatusbar( frames.bg, frames.ff )
    frames.ff:SetWidth( width )
    frames.ff:SetHeight( height )
    frames.ff:UpdateSize( frames.ff )
    frames.ff:SetStatusBarColor( 0, 0.45, 0.6, 1 )
    frames.ff:SetPoint( "BOTTOMRIGHT", frames.bg, "TOPRIGHT", -spacing*2, 0 )
    frames.ff.lastUpdate = 0
    frames.ff:SetScript( "OnUpdate", onUpdate )
    frames.ff:Hide()
    
    frames.ff.timer = frames.ff:CreateFontString( nil, "OVERLAY", "GameFontNormal" )
    frames.ff.timer:SetFont( font, 14, "THINOUTLINE" )
    frames.ff.timer:SetJustifyH( "CENTER" )
    frames.ff.timer:SetJustifyV( "CENTER" )
    frames.ff.timer:SetTextColor( lightenColor( 0, 0.45, 0.6 ) )
    frames.ff.timer:SetPoint( "CENTER", frames.ff, "TOP", 0, 0 )
    
    frames.ff.text = frames.ff:CreateFontString( nil, "OVERLAY", "GameFontNormal" )
    --frames.ff.text:SetWidth( width )
    --frames.ff.text:SetHeight( height )
    frames.ff.text:SetFont( font, 12, "THINOUTLINE" )
    frames.ff.text:SetJustifyH( "CENTER" )
    frames.ff.text:SetJustifyV( "CENTER" )
    frames.ff.text:SetTextColor( 0.66, 0.66, 0.66, 1 )
    frames.ff.text:SetPoint( "LEFT", frames.ff, "RIGHT", spacing*2, 0 )
    
    frames.ff.bg = frames.ff:CreateTexture( nil, "BACKGROUND" )
    frames.ff.bg:SetTexture( "Interface\\Buttons\\WHITE8X8" )
    frames.ff.bg:SetAllPoints( frames.ff )
    frames.ff.bg:SetVertexColor( 0, 0.1125, 0.15, 1 )
    end
    
    -- Procs
    do
    frames.proc = frames.bg:CreateTexture( nil, "ARTWORK" )
    frames.proc:SetWidth( width )
    frames.proc:SetHeight( width )
    frames.proc:SetPoint( "BOTTOM", frames.bg, "TOP", 0, 20 )
    frames.proc:SetVertexColor( 1,1,1,1 )
    frames.proc:Hide()
    end
end

-- Update diseases
local debuffs = { target = {}, player = {} }
local function aura( unit )
    local counter = 1
    
    if unit=="target" then
        wipe( debuffs[unit] )
        
        for i=1,40 do
            local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitAura( unit, i, "HARMFUL" )
            
            if name and unitCaster=="player" and ( spellId==59921 or name==L["Frost Fever"] ) then
                debuffs[unit].ff = { spellId, name, icon, count, debuffType, duration, expirationTime, isMine }
            elseif name and unitCaster=="player" and ( spellId==59879 or name==L["Blood Plague"] ) then
                debuffs[unit].bp = { spellId, name, icon, count, debuffType, duration, expirationTime, isMine }
            elseif not name then
                break
            end
        end
        
        if enable_diseases and debuffs[unit].ff then --"Frost Fever"
            local time = GetTime()
            
            frames.ff:SetMinMaxValues( 0, debuffs[unit].ff[6] )
            frames.ff:SetValue( time-( debuffs[unit].ff[7]-time ) )
            
            frames.ff.expirationTime = debuffs[unit].ff[7]
            frames.ff.duration = debuffs[unit].ff[6]
            frames.ff:SetMinMaxValues( 0, debuffs[unit].ff[6] )
            frames.ff:Show()
        else
            frames.ff:Hide()
        end
        
        if enable_diseases and debuffs[unit].bp then --"Blood Plague"
            local time = GetTime()
            
            frames.bp:SetMinMaxValues( 0, debuffs[unit].bp[6] )
            frames.bp:SetValue( time-( debuffs[unit].bp[7]-time ) )
            
            frames.bp.expirationTime = debuffs[unit].bp[7]
            frames.bp.duration = debuffs[unit].bp[6]
            frames.bp:SetMinMaxValues( 0, debuffs[unit].bp[6] )
            frames.bp:Show()
        else
            frames.bp:Hide()
        end
    elseif unit=="player" then
        wipe( debuffs[unit] )
        
        for i=1,40 do
            local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitAura( unit, i, "HELPFUL" )
            
            if name and unitCaster=="player" and ( spellId==51124 or name==L["Killing Machine"] ) then
                debuffs[unit].km = { spellId, name, icon, count, debuffType, duration, expirationTime, isMine }
            elseif name and unitCaster=="player" and ( spellId==81340 or name==L["Sudden Doom"] ) then
                debuffs[unit].sd = { spellId, name, icon, count, debuffType, duration, expirationTime, isMine }
            elseif not name then
                break
            end
        end
        
        if enable_procs and debuffs[unit].km then --"Killing Machine"
            frames.proc:SetTexture( debuffs[unit].km[3] )
            frames.proc:Show()
        elseif enable_procs and debuffs[unit].sd then --"Sudden Doom"
            frames.proc:SetTexture( debuffs[unit].sd[3] )
            frames.proc:Show()
        else
            frames.proc:Hide()
        end
    end
end

-- On Rune Update
local function onRuneCooldownUpdate( self, rune )
    if not rune or rune>6 then
        return
    end
    
    local place, pool, modificator
    if rune==1 then
        pool = 1
        place = 1
    elseif rune==2 then
        pool = 1
        place = 2
    elseif rune==3 then
        pool = 2
        place = 1
    elseif rune==4 then
        pool = 2
        place = 2
    elseif rune==5 then
        pool = 3
        place = 1
    elseif rune==6 then
        pool = 3
        place = 2
    end
    if place==1 then modificator = 1 else modificator = -1 end
    
    local runeOther = rune+modificator
    
    local time = GetTime()
    
    local start, duration, runeReady = GetRuneCooldown( rune )
    runes[pool][place].ready = runeReady
    runes[pool][place].duration = time-start
    runes[pool][place].start = start
    runes[pool][place].max = duration
    
    local start, duration, runeReady = GetRuneCooldown( runeOther )
    runes[pool][place+modificator].ready = runeReady
    runes[pool][place+modificator].duration = time-start
    runes[pool][place+modificator].start = start
    runes[pool][place+modificator].max = duration
    
    if time-runes[pool][1].start<time-runes[pool][2].start then
        rune, runeOther = runeOther, rune
    end
    
    if runes[pool][place].ready then
        frames[rune]:SetMinMaxValues( 0, 1 )
        frames[rune]:SetValue( 1 )
        frames[rune]:SetScript( "OnUpdate", nil )
        frames[rune].text:SetText( nil )
        frames[rune]:SetAlpha( 0.5 )
    else
        frames[rune].duration = time-runes[pool][place].start
        frames[rune].max = runes[pool][place].max
        frames[rune].start = runes[pool][place].start
        frames[rune]:SetMinMaxValues( 0, frames[rune].max )
        frames[rune]:SetScript( "OnUpdate", frames[rune].onUpdate )
        frames[rune]:SetAlpha( 1 )
        
        if frames[rune].duration<0 then frames[rune]:SetValue( 0 ) end
    end
    frames[rune]:SetStatusBarColor( unpack( colors[runes[pool][place].runeType] ) )
    frames[rune].bg:SetVertexColor( unpack( colorsCooldown[runes[pool][place].runeType] ) )
    
    if runes[pool][place+modificator].ready then
        frames[runeOther]:SetMinMaxValues( 0, 1 )
        frames[runeOther]:SetValue( 1 )
        frames[runeOther]:SetScript( "OnUpdate", nil )
        frames[runeOther].text:SetText( nil )
        frames[runeOther]:SetAlpha( 0.5 )
    else
        frames[runeOther].duration = time-runes[pool][place+modificator].start
        frames[runeOther].max = runes[pool][place+modificator].max
        frames[runeOther].start = runes[pool][place+modificator].start
        frames[runeOther]:SetMinMaxValues( 0, frames[runeOther].max )
        frames[runeOther]:SetScript( "OnUpdate", frames[runeOther].onUpdate )
        frames[runeOther]:SetAlpha( 1 )
        
        if frames[runeOther].duration<0 then frames[runeOther]:SetValue( 0 ) end
    end
    frames[runeOther]:SetStatusBarColor( unpack( colors[runes[pool][place+modificator].runeType] ) )
    frames[runeOther].bg:SetVertexColor( unpack( colorsCooldown[runes[pool][place+modificator].runeType] ) )
end

-- Events
function addon:UNIT_POWER( event, unit, type )
    if not unit then unit = "player" end
    
    if enable_runicPower and unit=="player" then
        local power = UnitMana( unit )
        
        frames.rp:SetValue( power )
        frames.rp.text:SetText( power )
    end
end

function addon:UNIT_RUNIC_POWER( event, unit )
    if not unit then unit = "player" end
    
    if unit=="player" then
        local power = UnitMana( unit )
        
        frames.rp:SetValue( power )
        frames.rp.text:SetText( power )
    end
end

function addon:RUNE_POWER_UPDATE( event, rune )
    onRuneCooldownUpdate( self, rune )
end

function addon:RUNE_TYPE_UPDATE( event, rune )
    if not rune or rune>6 then return end
    
    local runeType = GetRuneType( rune )
    
    local place, pool, modificator
    if rune==1 then
        pool = 1
        place = 1
    elseif rune==2 then
        pool = 1
        place = 2
    elseif rune==3 then
        pool = 2
        place = 1
    elseif rune==4 then
        pool = 2
        place = 2
    elseif rune==5 then
        pool = 3
        place = 1
    elseif rune==6 then
        pool = 3
        place = 2
    end
    if place==1 then modificator = 1 else modificator = -1 end
    
    runes[pool][place].runeType = runeType
    
    local runeOther = rune+modificator
    local time = GetTime()
    if time-runes[pool][1].start<time-runes[pool][2].start then
        rune, runeOther = runeOther, rune
    end
    
    frames[rune]:SetStatusBarColor( unpack( colors[runes[pool][place].runeType] ) )
    frames[rune].bg:SetVertexColor( unpack( colorsCooldown[runes[pool][place].runeType] ) )
    frames[runeOther]:SetStatusBarColor( unpack( colors[runes[pool][place+modificator].runeType] ) )
    frames[runeOther].bg:SetVertexColor( unpack( colorsCooldown[runes[pool][place+modificator].runeType] ) )
end

function addon:UNIT_AURA( event, unit )
    if unit=="target" or unit=="player" then aura( unit ) end
end

function addon:PLAYER_TARGET_CHANGED( event )
    aura( "target" )
end

function addon:PLAYER_ENTERING_WORLD( event )
	RuneFrame:Hide()
    for i=1,6 do
        addon:RUNE_TYPE_UPDATE( nil, i )
    end
    for i=1,6 do
        addon:RUNE_POWER_UPDATE( nil, i )
    end
end

-- OnInitialize
function addon:OnInitialize()
    
end
-- OnEnable
function addon:OnEnable()
    if select( 2,UnitClass( "player" ) )~="DEATHKNIGHT" then return end
    
    createFrames()
    frames.bg:ClearAllPoints()
    frames.bg:SetPoint( DocsDebugRunesDB.point or "CENTER", UIParent, DocsDebugRunesDB.point or "CENTER", DocsDebugRunesDB.xOfs or 0, DocsDebugRunesDB.yOfs or 0 )
    
    addon:RegisterEvent( "RUNE_POWER_UPDATE" )
    addon:RegisterEvent( "RUNE_TYPE_UPDATE" )
    addon:RegisterEvent( "UNIT_AURA" )
    addon:RegisterEvent( "PLAYER_TARGET_CHANGED" )
    addon:RegisterEvent( "PLAYER_ENTERING_WORLD" )
    if expansionLevel==2 then
        addon:RegisterEvent( "UNIT_RUNIC_POWER" )
        addon:UNIT_RUNIC_POWER()
    else
        addon:RegisterEvent( "UNIT_POWER" )
        addon:UNIT_POWER()
    end
    
    ChatFrame1:AddMessage( "DocsDebugRunes: Move with Shift+Drag, Debug with Rightclick." )
	RuneFrame:Hide()
end
function addon:OnDisable()
	
end
