local framik = CreateFrame("Frame", nil, self)
framik:RegisterEvent("ADDON_LOADED")
framik:RegisterEvent("UNIT_AURA")

local p, p2, p3, p4, p5 = {}, {}, {}, {}, {}
local positive, negative, fail = 396369, 396364, 396411
local n_positive, n_negative = 0, 0
local bar_offset = {0, 0}
local ratio_x = UIParent:GetWidth() / 1920
function bool_to_number(bool)
	if bool then return 1
	else return 0 end
end

local size = CreateFrame("Frame", nil, UIParent)
size:SetSize(1, 1)
size:SetPoint('CENTER')

local empty = CreateFrame("Frame", nil, size)
empty:SetFrameStrata('FULLSCREEN_DIALOG')
empty:SetSize(264*ratio_x, 24*ratio_x)
empty:SetPoint('CENTER')
empty.tex = empty:CreateTexture()
empty.tex:SetPoint('CENTER')
empty.tex:SetSize(264*ratio_x, 24*ratio_x)
empty.tex:SetTexture("Interface\\AddOns\\Happy_thundering\\files\\empty",false)
empty.tex:SetTexCoord(0,0.515625,0,0.75)

local cdown = CreateFrame("Frame", nil, size)
cdown:SetFrameStrata('FULLSCREEN_DIALOG')
cdown:SetSize(64*ratio_x, 32*ratio_x)
cdown:SetPoint('CENTER',0,(28 + 4)*ratio_x)
cdown.tex = cdown:CreateTexture()
cdown.tex:SetAllPoints(cdown)
cdown.tex:SetTexture("Interface\\AddOns\\Happy_thundering\\files\\3",false)
cdown.tex:SetDrawLayer("OVERLAY", 7)
cdown:SetAlpha(0)

local b_rot = CreateFrame("Frame", nil, size)
b_rot:SetFrameStrata('FULLSCREEN_DIALOG')
b_rot:SetSize(24*ratio_x, 24*ratio_x)
b_rot:SetPoint('CENTER')--,0,(28 + 4)*ratio_x)
b_rot.tex = b_rot:CreateTexture()
b_rot.tex:SetAllPoints(b_rot)
b_rot.tex:SetTexture("Interface\\AddOns\\Happy_thundering\\files\\rotate",false)
b_rot.tex:SetTexCoord(0,0.75,0,0.75)
b_rot.tex:SetDrawLayer("OVERLAY", 7)
--b_rot:Hide()

local fireworks = CreateFrame("Frame", nil, size)
fireworks:SetFrameStrata('FULLSCREEN_DIALOG')
fireworks:SetSize(114*ratio_x, 32*ratio_x)
fireworks:SetPoint('CENTER',0,(28 + 4)*ratio_x)
fireworks.tex = fireworks:CreateTexture()
fireworks.tex:SetAllPoints(fireworks)
fireworks.tex:SetTexCoord(0,0.890625,0,1)
fireworks.tex:SetTexture("Interface\\AddOns\\Happy_thundering\\files\\stars",false)
fireworks.tex:SetDrawLayer("OVERLAY", 7)
fireworks:SetAlpha(0)

local bar = CreateFrame("Frame", nil, size)
bar:SetFrameStrata('FULLSCREEN_DIALOG')
bar:SetSize(264*ratio_x, 24*ratio_x)
bar:SetPoint('CENTER')
bar.tex = bar:CreateTexture()
bar.tex:SetPoint('CENTER')
bar.tex:SetSize(264*ratio_x, 24*ratio_x)
bar.tex:SetTexture("Interface\\AddOns\\Happy_thundering\\files\\full",false)
bar.tex:SetDrawLayer("OVERLAY", 7)
bar.tex:SetTexCoord(0,0.515625,0,0.375)

local bar_time = GetTime()
local safe = false
local check_end = false
local check_posibilities = false
local sound_effect_played = -1
local fw_animation_end = -1
bar:SetAlpha(0)
--empty:SetAlpha(0)
empty:Hide()

function rotate(rotation)
	rotated = rotation
	if rotated then
		empty:SetSize(24*ratio_x, 264*ratio_x)
		empty.tex:SetRotation(1.5708)
		bar:SetSize(24*ratio_x, 264*ratio_x)
		bar.tex:SetRotation(1.5708)
		b_rot:SetPoint('CENTER', (bar_offset[1] + 24 + 4)*ratio_x, (bar_offset[2]-120)*ratio_x)
	else
		empty:SetSize(264*ratio_x, 24*ratio_x)
		empty.tex:SetRotation(0)
		bar:SetSize(264*ratio_x, 24*ratio_x)
		bar.tex:SetRotation(0)
		b_rot:SetPoint('CENTER', (bar_offset[1] + 144 + 4)*ratio_x, bar_offset[2]*ratio_x)
	end
	cdown:SetPoint('CENTER', bar_offset[1]*ratio_x, (28 + 120*bool_to_number(rotated) + 4 + bar_offset[2])*ratio_x)
	fireworks:SetPoint('CENTER', bar_offset[1]*ratio_x, (28 + 120*bool_to_number(rotated) + 4 + bar_offset[2])*ratio_x)
end

function bar_update(bo_x,bo_y)
	bar_offset[1] = bo_x
	bar_offset[2] = bo_y
	bar_position = bar_offset
	empty:SetPoint('CENTER', bar_offset[1]*ratio_x, bar_offset[2]*ratio_x)
	if rotated then
		b_rot:SetPoint('CENTER', (bar_offset[1] + 24 + 4)*ratio_x, (bar_offset[2]-120)*ratio_x)
	else
		b_rot:SetPoint('CENTER', (bar_offset[1] + 144 + 4)*ratio_x, bar_offset[2]*ratio_x)
	end
	cdown:SetPoint('CENTER', bar_offset[1]*ratio_x, (28 + 120*bool_to_number(rotated) + 4 + bar_offset[2])*ratio_x)
	fireworks:SetPoint('CENTER', bar_offset[1]*ratio_x, (28 + 120*bool_to_number(rotated) + 4 + bar_offset[2])*ratio_x)
end

local original = {0, 0}
local moving_cursor = false
empty:SetScript('OnMouseDown', function(self, button)
	if button == 'LeftButton' and empty:IsVisible() == true then
		original[1], original[2] = GetCursorPosition()
		moving_cursor = true
	end
end)

empty:SetScript('OnMouseUp', function(self, button)
	if button == 'LeftButton' and empty:IsVisible() == true then
		local current_x, current_y = GetCursorPosition()
		current_x = (current_x - original[1])/UIParent:GetEffectiveScale()/size:GetScale()
		current_y = (current_y - original[2])/UIParent:GetEffectiveScale()/size:GetScale()

		bar_update(bar_offset[1] + current_x/ratio_x, bar_offset[2] + current_y/ratio_x)

		original[1], original[2] = 0, 0
		moving_cursor = false
	end
end)

function outQuad(t, b, c, d)
	t = t / d
	return -c * t * (t - 2) + b
end

function inQuad(t, b, c, d) return c * math.pow(t / d, 2) + b end

bar:SetScript('OnUpdate',function()
	local hitbox = MouseIsOver(empty, nil, nil, nil, 28*ratio_x)
	if (hitbox or moving_cursor) and empty:IsVisible() then b_rot:Show() else b_rot:Hide() end
	local current_x, current_y = GetCursorPosition()
	if moving_cursor then
		current_x = (current_x - original[1])/UIParent:GetEffectiveScale()/size:GetScale()
		current_y = (current_y - original[2])/UIParent:GetEffectiveScale()/size:GetScale()
		empty:SetPoint('CENTER', bar_offset[1]*ratio_x + current_x, bar_offset[2]*ratio_x + current_y)
		cdown:SetPoint('CENTER', bar_offset[1]*ratio_x + current_x, (28 + 120*bool_to_number(rotated) + 4 + bar_offset[2])*ratio_x + current_y)
		fireworks:SetPoint('CENTER', bar_offset[1]*ratio_x + current_x, (28	+ 120*bool_to_number(rotated) + 4 + bar_offset[2])*ratio_x + current_y)
		b_rot:SetPoint('CENTER', (bar_offset[1] + 24 + 120*bool_to_number(not rotated) + 4)*ratio_x + current_x, (bar_offset[2] - 120*bool_to_number(rotated))*ratio_x + current_y)
	end
	if safe == false and check_end == true then
        for n = 1, start_number - 1 do
            if bar_time - GetTime() < n and sound_effect_played == n + 1 then
                cdown.tex:SetTexture("Interface\\AddOns\\Happy_thundering\\files\\" .. n, false)
				if voice then PlaySoundFile("Interface\\Addons\\Happy_thundering\\files\\" .. n .. ".wav", "Master")
				else PlaySoundFile("Interface\\Addons\\Happy_thundering\\files\\warning.wav", "Master") end
                sound_effect_played = n
                if p[2] == true then SendChatMessage("{rt6} " .. n .. " {rt6}")
                else SendChatMessage("{rt7} " .. n .. " {rt7}", "SAY") end
                break
            end
        end
        if bar_time - GetTime() < start_number and sound_effect_played == -1 then
			cdown.tex:SetTexture("Interface\\AddOns\\Happy_thundering\\files\\" .. start_number, false)
			if voice then PlaySoundFile("Interface\\Addons\\Happy_thundering\\files\\" .. start_number .. ".wav", "Master")
			else PlaySoundFile("Interface\\Addons\\Happy_thundering\\files\\warning.wav", "Master") end
			sound_effect_played = start_number
			if p[2] == true then SendChatMessage("{rt6} " .. start_number .. " {rt6}")
			else SendChatMessage("{rt7} " .. start_number .. " {rt7}", "SAY") end
			cdown:SetAlpha(1)
		end
	end
	if GetTime() < bar_time then
		local bar_full = (bar_time-GetTime())/15
		bar.tex:SetTexCoord(0, bar_full*0.515625, 0.375*bool_to_number(not p[2]), 0.375 + 0.375*bool_to_number(not p[2]))
		--bar.tex:SetTexCoord(0, bar_full*0.515625, 0.375, 0.75)
		bar.tex:SetSize((264*bar_full)*ratio_x, 24*ratio_x)
		--bar:SetSize((264*bar_full)*ratio_x, 24*ratio_x)
		if moving_cursor then
			if rotated then
				bar:SetPoint('CENTER',bar_offset[1]*ratio_x + current_x, (-132+132*bar_full + bar_offset[2])*ratio_x + current_y)
			else
				bar:SetPoint('CENTER',(-132+132*bar_full + bar_offset[1])*ratio_x + current_x, bar_offset[2]*ratio_x + current_y)
			end
		else
			if rotated then
				bar:SetPoint('CENTER',bar_offset[1]*ratio_x, (-132+132*bar_full + bar_offset[2])*ratio_x)
			else
				bar:SetPoint('CENTER',(-132+132*bar_full + bar_offset[1])*ratio_x, bar_offset[2]*ratio_x)
			end
		end

		if fw_animation_end ~= -1 then
			if GetTime() < fw_animation_end then
				local num = (fw_animation_end - GetTime())/1
				num = outQuad(num,0,1,1)
				--print(num)
				fireworks:SetAlpha(num)
			else
				fw_animation_end = -1
				fireworks:SetAlpha(0)
			end
		end
	elseif check_end == true then
		check_end = false
		check_posibilities = false
		safe = false
		n_negative = 0
		n_positive = 0
		bar:SetAlpha(0)
		--empty:SetAlpha(0)
		empty:Hide()
		b_rot:Hide()
		--empty:SetPoint('CENTER', bar_offset[1]*ratio_x, bar_offset[2]*ratio_x)
		original[1], original[2] = 0, 0
		if moving_cursor then
			bar_update(bar_offset[1] + current_x/ratio_x, bar_offset[2] + current_y/ratio_x)
			moving_cursor = false
		end
		p, p2, p3, p4, p5 = {}, {}, {}, {}, {}
		cdown:SetAlpha(0)
		fireworks:SetAlpha(0)
		fw_animation_end = -1
	end
end)

function full(expiration)
	bar_time = expiration
	safe = false
	check_end = true
	sound_effect_played = -1
	check_posibilities = true
	cdown:SetAlpha(0)
	fireworks:SetAlpha(0)
	fw_animation_end = -1
	bar:SetAlpha(1)
	--empty:SetAlpha(1)
	empty:Show()
	--b_rot:Show()
end

function clean()
	safe = true
	check_posibilities = false
	cdown.tex:SetTexture("Interface\\AddOns\\Happy_thundering\\files\\smile",false)
	PlaySoundFile("Interface\\Addons\\Happy_thundering\\files\\gj.flac", "Master")
	SendChatMessage("Safe!")
	fireworks:SetAlpha(1)
	fw_animation_end = GetTime() + 1
	cdown:SetAlpha(1)
end

local bool_to_text = {[true] = "enabled", [false] = "disabled"}
SLASH_HT1 = "/ht"
SlashCmdList["HT"] = function(arg)
	local arg_scale = arg:sub(1, 6)
	if arg == "test" then
		full(GetTime() + 15)
	elseif arg == "voice" then
		voice = not voice
		print("Voice has been " .. bool_to_text[voice] .. ".")
	elseif arg_scale == "scale " then
		local n = tonumber(arg:sub(7))
		if n ~= nil then
			if n >= 0.5 and n <= 2 then
				size:SetScale(n)
				bar_update(0, 0)
				scale = n
				print("Scale has been set to " .. n .. ".")
			else
				print("Please choose a number from 0.5 to 2.")
			end
		end
	else
		local num_start = tonumber(arg)
		if num_start ~= nil and floor(num_start) == num_start then
			if num_start >= 3 and num_start <= 10 then
				start_number = num_start
				print("Countdown set for the last " .. num_start .. " seconds.")
			else
				print("Please choose a number from 3 to 10.")
			end
		end
	end
end

framik:SetScript("OnEvent", function(self, event, ...)
	if event == "UNIT_AURA" then
		local target,ua = ...
		local added = ua.addedAuras
		local removed = ua.removedAuraInstanceIDs
		if added then
			for _,auraInfo in pairs(added) do
				if auraInfo.spellId == positive or auraInfo.spellId == negative then
					if target == "player" or target == "party1" or target == "party2"
					or target == "party3" or target == "party4" then
						if auraInfo.spellId == positive then n_positive = n_positive + 1
						else n_negative = n_negative + 1 end
					end

					if target == "player" then
						p[1] = auraInfo.auraInstanceID
						p[2] = auraInfo.spellId == positive
						full(auraInfo.expirationTime)
						if p[2] == true then
							SendChatMessage("{rt6} Positive! {rt6}")
						else
							if UnitGroupRolesAssigned("player") == "TANK" then SendChatMessage("{rt7} Negative tank! {rt7}")
							else SendChatMessage("{rt7} Negative! {rt7}") end
						end
					elseif target == "party1" then
						p2[1] = auraInfo.auraInstanceID
						p2[2] = auraInfo.spellId == positive
					elseif target == "party2" then
						p3[1] = auraInfo.auraInstanceID
						p3[2] = auraInfo.spellId == positive
					elseif target == "party3" then
						p4[1] = auraInfo.auraInstanceID
						p4[2] = auraInfo.spellId == positive
					elseif target == "party4" then
						p5[1] = auraInfo.auraInstanceID
						p5[2] = auraInfo.spellId == positive
					end
				elseif auraInfo.spellId == fail then
					check_posibilities = false
				end
			end
		end
		if removed and check_posibilities == true then
			for i = 1, #removed do
				local auraInstanceID = removed[i]
				if target == "player" and p[1] == auraInstanceID then
					if bar_time - GetTime() > 1 then
						bar:SetAlpha(0)
						--empty:SetAlpha(0)
						empty:Hide()
						b_rot:Hide()
						empty:SetPoint('CENTER', bar_offset[1]*ratio_x, bar_offset[2]*ratio_x)
						original[1], original[2] = 0, 0
						moving_cursor = false
						cdown:SetAlpha(0)
						fireworks:SetAlpha(0)
						fw_animation_end = -1
						PlaySoundFile("Interface\\Addons\\Happy_thundering\\files\\gj.flac", "Master")
						check_posibilities = false
						--print("ODSTRANIL SOM Z PLAYERA")
						SendChatMessage("Cleared!")
						safe = true
						break
					end
				elseif target == "party1" and p2[1] == auraInstanceID then
					if p2[2] == true then n_positive = n_positive - 1
					else n_negative = n_negative - 1 end
					--print("ODSTRANIL SOM Z P2")
				elseif target == "party2" and p3[1] == auraInstanceID then
					if p3[2] == true then n_positive = n_positive - 1
					else n_negative = n_negative - 1 end
					--print("ODSTRANIL SOM Z P3")
				elseif target == "party3" and p4[1] == auraInstanceID then
					if p4[2] == true then n_positive = n_positive - 1
					else n_negative = n_negative - 1 end
					--print("ODSTRANIL SOM Z P4")
				elseif target == "party4" and p5[1] == auraInstanceID then
					if p5[2] == true then n_positive = n_positive - 1
					else n_negative = n_negative - 1 end
					--print("ODSTRANIL SOM Z P5")
				end
				
				if (p[2] == true and n_negative == 0) or (p[2] == false and n_positive == 0) then
					clean()
					check_posibilities = false
					--print("SAFE")
					break
				end
			end
		end
	elseif event == "ADDON_LOADED" and ... == "Happy_thundering" then
		if bar_position == nil then
			bar_position = {0, 0}
		else
			bar_update(bar_position[1],bar_position[2])
		end

        if start_number == nil then start_number = 10 end
        if voice == nil then voice = true end

		if rotated == nil then rotated = false
		elseif rotated then rotate(true) end

		if scale == nil then scale = 1
		elseif scale ~= 1 then size:SetScale(scale) end
	end
end)

b_rot:SetScript("OnMouseDown", function(self, button)
	if button == 'LeftButton' then
		rotate(not rotated)
	end
end)