local GlobalAddonName, BH = ...

local math_ceil, IsEncounterInProgress, abs, GetTime, format = math.ceil, IsEncounterInProgress, abs,  GetTime, format
local SendAddonMessage = C_ChatInfo.SendAddonMessage
local VBH = nil

local module = BH:New("Timers",BH.L.timers,nil,true)
local BHLib,L = BH.lib,BH.L

module.db.bhlasttimertopull = 0

function module:timer(elapsed)
	if VBH.Timers.enabled then
		if not module.frame.encounter and IsEncounterInProgress() then
			module.frame.encounter = true
			module.frame.total = 0
			
			if VBH.Timers.OnlyInCombat then
				module.frame:Show()
			end
		elseif module.frame.encounter and not IsEncounterInProgress() then
			module.frame.encounter = nil
			
			if VBH.Timers.OnlyInCombat and not module.frame.inCombat then
				module.frame:Hide()
			end
		end
	end
end

function module:slash(arg,msgDeformatted)
	if VBH.Timers.enabled and arg:find("^mytimer ") then
		local id = arg:match("%d+")
		if id then
			module.frame.total = -tonumber(id)
		end
	end
end

function module.options:Load()
	self:CreateTilte()

	
	--self.shtml1 = BHLib:Text(self,L.timerstxt1,12):Size(650,200):Point(55,-30):Top()
	--self.shtml2 = BHLib:Text(self,L.timerstxt2,12):Size(550,200):Point(105,-30):Top():Color()
	
	self.TabTimerFrame = BHLib:OneTab(self):Size(400,100):Point("TOPLEFT",10,-20)
	
	self.chkEnable = BHLib:Check(self.TabTimerFrame,L.timerTimerFrame,VBH.Timers.enabled,0):Point(10,-10):OnClick(function(self) 
		if self:GetChecked() then
			VBH.Timers.enabled = true
			module.frame:Show()
			module.frame:SetScript("OnUpdate", module.frame.OnUpdateFunc)
			module:RegisterEvents('PLAYER_REGEN_DISABLED','PLAYER_REGEN_ENABLED')
		else
			VBH.Timers.enabled = nil
			module.frame:Hide() 
			module.frame:SetScript("OnUpdate", nil)
			module:UnregisterEvents('PLAYER_REGEN_DISABLED','PLAYER_REGEN_ENABLED')
		end
	end)
	
	self.chkOnlyInCombat = BHLib:Check(self.TabTimerFrame,L.TimerOnlyInCombat,VBH.Timers.OnlyInCombat,0):Point(150,-10):OnClick(function(self) 
		if self:GetChecked() then
			VBH.Timers.OnlyInCombat = true
			if not (module.frame.inCombat or module.frame.encounter) then
				module.frame:Hide()
			end
		else
			VBH.Timers.OnlyInCombat = nil
			if VBH.Timers.enabled then
				module.frame:Show()
			end
		end
	end)
	
	self.chkFixate = BHLib:Check(self.TabTimerFrame,L.cd2fix,VBH.Timers.Lock,0):Point(290,-10):OnClick(function(self) 
		if self:GetChecked() then
			VBH.Timers.Lock = true
			module.frame:SetMovable(false)
			module.frame:EnableMouse(false)
		else
			VBH.Timers.Lock = nil
			module.frame:SetMovable(true)
			module.frame:EnableMouse(true)
		end
	end)
	
	self.ButtonToCenter = BHLib:Button(self.TabTimerFrame,L.TimerResetPos):Size(100,20):Point(15,-50):Tooltip(L.TimerResetPosTooltip):OnClick(function()
		VBH.Timers.Left = nil
		VBH.Timers.Top = nil

		module.frame:ClearAllPoints()
		module.frame:SetPoint("CENTER",UIParent, "CENTER", 0, 0)
	end) 
	

	self.setTypeText = BHLib:Text(self.TabTimerFrame,TYPE..":",11):Point(150,-50):Size(60,25)
	
	self.setType1 = BHLib:Radio(self.TabTimerFrame,"1",VBH.Timers.Type == 1 or not VBH.Timers.Type):Point("LEFT",self.setTypeText,"RIGHT", 15, 0):OnClick(function(self) 
		self:SetChecked(true)
		module.options.setType2:SetChecked(false)
		VBH.Timers.Type = 1
		if VBH.Timers.enabled then
			module.frame:SetScript("OnUpdate", module.frame.OnUpdateFunc)
		end
	end)
	
	self.setType2 = BHLib:Radio(self.TabTimerFrame,"2",VBH.Timers.Type == 2):Point("LEFT",self.setType1,"RIGHT", 75, 0):OnClick(function(self) 
		self:SetChecked(true)
		module.options.setType1:SetChecked(false)
		VBH.Timers.Type = 2
		if VBH.Timers.enabled then
			module.frame:SetScript("OnUpdate", module.frame.OnUpdateFunc)
		end
	end)
	
	
end

function module.main:ADDON_LOADED()
	VBH = _G.VBH
	VBH.Timers = VBH.Timers or {
		Type = 2,
	}

	if VBH.Timers.Left and VBH.Timers.Top then 
		module.frame:ClearAllPoints()
		module.frame:SetPoint("TOPLEFT",UIParent,"BOTTOMLEFT",VBH.Timers.Left,VBH.Timers.Top) 
	end

	if VBH.Timers.enabled then
		if not VBH.Timers.OnlyInCombat then
			module.frame:Show()
		end
		module.frame:SetScript("OnUpdate", module.frame.OnUpdateFunc)
		module:RegisterEvents('PLAYER_REGEN_DISABLED','PLAYER_REGEN_ENABLED')		
	end
	if VBH.Timers.Lock then
		module.frame:SetMovable(false)
		module.frame:EnableMouse(false)
	end
	VBH.Timers.Strata = VBH.Timers.Strata or "HIGH"
	
	module.frame:SetFrameStrata("HIGH")
	
	module:RegisterTimer()
	module:RegisterSlash()
end

function module.main:PLAYER_REGEN_DISABLED()
	if not module.frame.encounter then 
		module.frame.total = 0 
	end
	module.frame.inCombat = true
	
	if VBH.Timers.OnlyInCombat then
		module.frame:Show()
	end
end

function module.main:PLAYER_REGEN_ENABLED()
	module.frame.inCombat = nil
	
	if VBH.Timers.OnlyInCombat and not module.frame.encounter then
		module.frame:Hide()
	end
end

module.frame = CreateFrame("Frame",nil,UIParent)
module.frame:Hide()
module.frame:SetSize(77,27)
module.frame:SetPoint("CENTER", 0, 0)
module.frame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",edgeFile = BH.F.defBorder,tile = false,edgeSize = 4})
module.frame:SetBackdropBorderColor(0.1,0.1,0.1,0.7)
module.frame:SetBackdropColor(0,0,0,0.7)
module.frame:EnableMouse(true)
module.frame:SetMovable(true)
module.frame:RegisterForDrag("LeftButton")
module.frame:SetScript("OnDragStart", function(self)
	self:StartMoving()
end)
module.frame:SetScript("OnDragStop", function(self)
	self:StopMovingOrSizing()
	VBH.Timers.Left = self:GetLeft()
	VBH.Timers.Top = self:GetTop()
end)
module.frame.total = 0
module.frame.tmr = 0
module.frame.killTmr = 0
module.frame.txt = BHLib:Text(module.frame,"00:00.0"):Size(77,27):Point("LEFT",11,0):Left():Font(BH.F.defFont,16):Color():Shadow():Outline()
module.frame.txt_ms = BHLib:Text(module.frame,""):Size(77,27):Point("LEFT",11,0):Left():Font(BH.F.defFont,16):Color():Shadow():Outline()
module.frame.txt_s = BHLib:Text(module.frame,""):Size(77,27):Point("LEFT",11,0):Left():Font(BH.F.defFont,16):Color():Shadow():Outline()

module.db.bhTTK = {}

-- fonction d'affichage de timer
function module:UpdateView(t)
	local self = module.frame
	if t == 1 then
		self:SetSize(77,27)
		self:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",edgeFile = BH.F.defBorder,tile = false,edgeSize = 4})
		self:SetBackdropBorderColor(0.1,0.1,0.1,0.7)
		self:SetBackdropColor(0,0,0,0.7)

		self.txt:SetText("00:00.0")
		self.txt_ms:SetText("")
		self.txt_s:SetText("")
		self.txt:Size(77,27):Point("LEFT",11,0):Left():Font(BH.F.defFont,16):Color():Shadow():Outline()
		
	elseif t == 2 then
		self:SetSize(77,27)
		self:SetBackdropBorderColor(0.1,0.1,0.1,0)
		self:SetBackdropColor(0,0,0,0)

		self.txt:SetText("0:")
		self.txt_s:SetText("00")
		self.txt_ms:SetText(".0")
		self.txt:Size(29+4,27):Point("LEFT",-4,0):Right():Font(BH.F.defFont,20):Color():Shadow():Outline()
		self.txt_s:Size(25,27):Point("LEFT",25,0):Left():Font(BH.F.defFont,20):Color():Shadow():Outline()
		self.txt_ms:Size(0,27):Point("LEFT",45,-3):Left():Font(BH.F.defFont,12):Color():Shadow():Outline()
		
		
	end
end

do
	local hpSnapshots,timeSnapshots,iSnapshot,guidSnapshot = {},{},0
	local tmr = 0
	local tmr2 = 0
	local MAX_SEGMENTS = 90

	local function timerType1(self,elapsed)
		tmr2 = tmr2 + elapsed
		if tmr2 > 0.05 and (self.inCombat or self.encounter or self.total < 0) then
			self.total = self.total + tmr2
			self.txt:SetFormattedText("%2.2d:%2.2d\.%1.1d",abs(self.total)/60,abs(self.total)%60,(abs(self.total)*10)%10)
			tmr2 = 0
		elseif tmr2 > 0.05 then
			tmr2 = 0
		end
		
	end

	local function timerType2(self,elapsed)
		tmr2 = tmr2 + elapsed
		if tmr2 > 0.05 and (self.inCombat or self.encounter or self.total < 0) then
			self.total = self.total + tmr2
			self.txt:SetFormattedText("%1.1d:",abs(self.total)/60)
			self.txt_s:SetFormattedText("%2.2d",abs(self.total)%60)
			self.txt_ms:SetFormattedText("\.%1.1d",(abs(self.total)*10)%10)
			tmr2 = 0
		elseif tmr2 > 0.05 then
			tmr2 = 0
		end
	end
	
	function module.frame.OnUpdateFunc(self,elapsed)
		if not VBH.Timers.Type or VBH.Timers.Type == 1 then
			module:UpdateView(1)
			self:SetScript("OnUpdate",timerType1)
			return
		elseif VBH.Timers.Type == 2 then
			module:UpdateView(2)
			self:SetScript("OnUpdate",timerType2)
			return
		end
		self:SetScript("OnUpdate",nil)
	end
end