local Addon = LibStub("AceAddon-3.0"):GetAddon("BFClassMod")
if not Addon then return end

local A = Addon:NewModule("RuneFrame","AceEvent-3.0","AceHook-3.0","AceTimer-3.0")
if not A then return end

local runeFrame,header

local L = {}
setmetatable(L,Addon.localMt)

if GetLocale()=='zhTW' then
	L["符文条"] = "符文條"
	L["鼠标拖拽以移动符文条"] = "鼠標拖拽以移動符文條"
	L["使用滚轮来缩放符文条"] = "使用滾輪來縮放符文條"
elseif GetLocale() =='enUS' then
	L["符文条"] = "RuneFrame"
	L["鼠标拖拽以移动符文条"] = "Drag here to move Rune frame"
	L["使用滚轮来缩放符文条"] = "Mouse wheel to zoom Rune frame in/out"
end

local default = 
{
	profile = {
		point = "CENTER",
		ref = "UIParent",
		refPoint = "CENTER",
		x = 3,
		y = -155,
		scale = 1.8	
	}
}

local oldPoint
local powerText 

local db = {}

local function __OnHeaderEnter(frame)
	GameTooltip:SetOwner(frame, "ANCHOR_RIGHT");
	GameTooltip:AddLine(L["符文条"])
	GameTooltip:AddLine(L["鼠标拖拽以移动符文条"])
	GameTooltip:AddLine(L["使用滚轮来缩放符文条"])
	GameTooltip:Show()
end

local function __OnHeaderLeave(frame)
	GameTooltip:Hide()
end

local function __OnHeaderDragStart(frame)
	frame:StartMoving()
end

local function __OnHeaderDragStop(frame)
	frame:StopMovingOrSizing() 
	db.point,db.ref,db.refPoint,db.x,db.y = frame:GetPoint(1)
	if not db.ref then db.ref = UIParent end
	db.ref = db.ref:GetName()
end

local function __OnHeaderMouseWheel(frame,delta)
	if delta == 1 then
		if db.scale > 0.8 then
			db.scale = db.scale - 0.1
		end
	else
		if db.scale < 2.5 then
			db.scale = db.scale + 0.1
		end
	end
	runeFrame:SetScale(db.scale)
end

local function __InitHeader()
	header = _G["BFRuneHeader"]
	if header then return end
	header = CreateFrame("Button","BFRuneHeader",UIParent)
	header:RegisterForDrag("LeftButton")
	header:EnableMouseWheel(true)
	header:SetMovable(true)
	header:SetWidth(12)
	header:SetHeight(12)

	local backdrop  = {
		
		edgeFile = [[Interface\DialogFrame\UI-DialogBox-Border]],
		-- true to repeat the background texture to fill the frame, false to scale it
		tile = true,
		-- size (width or height) of the square repeating background tiles (in pixels)
		tileSize = 32,
		-- thickness of edge segments and square size of edge corners (in pixels)
		edgeSize = 8,
	  -- distance from the edges of the frame to those of the background texture (in pixels)
		insets = {
			left = 4,
			right = 4,
			top = 4,
			bottom = 4
		  }
	}
	header:SetBackdrop(backdrop)
	
	header:SetScript("OnEnter",__OnHeaderEnter)
	header:SetScript("OnLeave",__OnHeaderLeave)
	header:SetScript("OnDragStart",__OnHeaderDragStart)
	header:SetScript("OnDragStop",__OnHeaderDragStop)
	header:SetScript("OnMouseWheel",__OnHeaderMouseWheel)
	
	header:Hide()
end

local function __InitPowerText()
	powerText = runeFrame:CreateFontString("BFRunePowerText","ARTWORK","NumberFont_Outline_Large")
	powerText:SetPoint("TOP",runeFrame,"TOP",-4,19)
	powerText:SetTextColor(0.5,0.7,0.9)
	powerText:SetJustifyH("CENTER")
	powerText:SetText("Test")
	powerText:SetWidth(100)
	powerText:SetHeight(28)
	powerText:Hide()
end

local function __AttachToHeader()
	oldPoint = oldPoint or {runeFrame:GetPoint(1)}
	if not oldPoint[2] then 
		oldPoint[2]=UIParent 
	end
	runeFrame:SetScript("OnShow", nil) 
	runeFrame:SetParent(UIParent)
	runeFrame:ClearAllPoints()
	runeFrame:SetPoint("TOPLEFT",header,"TOPLEFT",5,-5) 
	runeFrame:SetScale(db.scale)	
	runeFrame:Show()
end

local function __DetachFromHeader()
	runeFrame:ClearAllPoints()
	runeFrame:SetPoint(unpack(oldPoint)) 
	runeFrame:SetParent(PlayerFrame)
	runeFrame:SetScale(1)
end

local function __OnUpdate()
	local power = UnitPower("player")
	powerText:SetText(power)
end

function A:SetSize(...)
	db.scale = ...
	runeFrame:SetScale(db.scale)
end

function A:ToggleHeader(flag)
	if flag then
		header:Show()
		self.showHeader = true
	else
		header:Hide()
		self.showHeader = false
	end
end

function A:OnInitialize()
	runeFrame = _G.RuneFrame
	self.db = LibStub("AceDB-3.0"):New("BFRuneFrame_Config",default)
	db = self.db.profile
	__InitHeader()
	__InitPowerText()
end

function A:UnitFrame_SetUnit(frame, unit, healthbar, manabar)
	if frame == PetFrame and unit =='player' then
		A.showHeader = header:IsShown()
		runeFrame:Hide()
		header:Hide()
	end

	if frame == PlayerFrame and unit =='player' then
		self:ScheduleTimer(__AttachToHeader,0.5)
		if A.showHeader then
			header:Show()
		else
			header:Hide()
		end

	end
end

function A:OnEnable()
	header:SetPoint(db.point,_G[db.ref],db.refPoint,db.x,db.y)
	header:Show()
	self:ScheduleTimer(__AttachToHeader,0.3)
	runeFrame:HookScript("OnUpdate",function(frame,elapsed)
		if not frame.time then frame.time = 0 end
		frame.time = frame.time + elapsed
		if frame.time < 0.2 then return end
		__OnUpdate()
	end)
	self:SecureHook("UnitFrame_SetUnit")
	powerText:Show()
end

function A:OnDisable()
	__DetachFromHeader()
	header:ClearAllPoints()
	header:Hide()
	powerText:Hide()
end

