local f = CreateFrame("Frame")
local sounds = {
	["Bankrupt (Wheel of Fortune)"] = "Interface\\AddOns\\Awwwww!\\sounds\\Wheel-of-Fortune-Bankrupt.ogg",
	["Donkey Kong"] = "Interface\\AddOns\\Awwwww!\\sounds\\DonkeyKong-Die.ogg",
	["Foghorn"] = "Interface\\AddOns\\Awwwww!\\sounds\\foghorn.ogg",
	["Mega Man"] = "Interface\\AddOns\\Awwwww!\\sounds\\Mega Man Dies_3.ogg",
	["Ms. PacMan"] = "Interface\\AddOns\\Awwwww!\\sounds\\MsPacMan-Death.ogg",
	["PacMan"] = "Interface\\AddOns\\Awwwww!\\sounds\\PacMan-Killed.ogg",
	["Porky Pig"] = "Interface\\AddOns\\Awwwww!\\sounds\\Porky Pig- That's All Folks.mp3",
	["Sad Trombone"] = "Interface\\AddOns\\Awwwww!\\sounds\\Sad-Trombone.ogg",
	["Sega Rally Game Over"] = "Interface\\AddOns\\Awwwww!\\sounds\\Sega Rally-Game Over.ogg",
	["Super Mario Death"] = "Interface\\AddOns\\Awwwww!\\sounds\\SuperMario-Die.ogg",
	["Super Mario Game Over"] = "Interface\\AddOns\\Awwwww!\\sounds\\Super Mario - Game Over.ogg",
	["The Price Is Right"] = "Interface\\AddOns\\Awwwww!\\sounds\\PriceIsRight-GameOver.ogg",
	["Whammy (Press Your Luck)"] = "Interface\\AddOns\\Awwwww!\\sounds\\PressYourLuck-Whammy.ogg",
	["Zelda"] = "Interface\\AddOns\\Awwwww!\\sounds\\LegendOfZelda-Die.ogg",
}
local defaults = {
	channel = 1,
	sounds = {
		["Bankrupt (Wheel of Fortune)"] = true,
		["Donkey Kong"] = true,
		["Foghorn"] = true,
		["Mega Man"] = true,
		["Ms. PacMan"] = true,
		["PacMan"] = true,
		["Porky Pig"] = true,
		["Sad Trombone"] = true,
		["Sega Rally Game Over"] = true,
		["Super Mario Death"] = true,
		["Super Mario Game Over"] = true,
		["The Price Is Right"] = true,
		["Whammy (Press Your Luck)"] = true,
		["Zelda"] = true,
	},
}
local channels = {
	"Master",
	"SFX",
	"Ambience",
	"Music",
}

--local 
function DeathSound()
	local rndSound = f.buttons[random(#f.buttons)].text:GetText()
	while not AwwwwwDB.sounds[rndSound] do
		rndSound = f.buttons[random(#f.buttons)].text:GetText()
	end
	PlaySoundFile(sounds[rndSound])
	
end


local function AddOptions()
	local OnEnter = function(self, title, text)
			GameTooltip:SetOwner(self, "ANCHOR_TOPRIGHT")
			GameTooltip:SetText(title)
			GameTooltip:AddLine(text,1,1,1,true)
			GameTooltip:Show()
		end

	local header = f:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
		header:SetParent(f)
		header:SetPoint("TOPLEFT", 20, -15)
		header:SetText("Awwwww!")

	local subheader = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
		subheader:SetParent(f)
		subheader:SetPoint("TOPLEFT", 20, -32)
		subheader:SetPoint("RIGHT", -20, -32)
		subheader:SetHeight(30)
		subheader:SetNonSpaceWrap(true)
		subheader:SetJustifyH("LEFT")
		subheader:SetText("This addon will play the selected sounds at random upon your death.")

	f.buttons = {}
	local i = 1
	for sound, path in pairs(sounds) do
		--checkboxes
		f.buttons[i] = CreateFrame("CheckButton", nil, f, "UICheckButtonTemplate")
		local box = f.buttons[i]
		if i == 1 then
			box:SetPoint("TOPLEFT", 56, -85)
		else
			box:SetPoint("TOPLEFT", f.buttons[i-1], "TOPLEFT", 0, -30)
		end
		box:SetChecked(AwwwwwDB.sounds[sound])
		box:SetScript("OnClick", function(self)
				self:SetChecked(not AwwwwwDB.sounds[sound])
				AwwwwwDB.sounds[sound] = not AwwwwwDB.sounds[sound]
			end)
		
		--play buttons
		box.play = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
		box.play:SetPoint("RIGHT", box, "LEFT", -4, 0)
		box.play:SetText("|TInterface\\OPTIONSFRAME\\VoiceChat-Play:14|t")
		box.play:SetSize(32, 28)
		box.play:SetScript("OnEnter", function(self) OnEnter(self,"Test","Hear this sound.") end)
		box.play:SetScript("OnLeave", function() GameTooltip:Hide() end)
		box.play:SetScript("OnClick", function() PlaySoundFile(path) end)
		
		--labels
		box.text = box:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
		box.text:SetPoint("LEFT", box, "RIGHT", 5, 0)
		box.text:SetText(sound)
		
		i = i+1
	end

	local dropdown2 = CreateFrame("Frame", "Awwwww_Dropdown2", f, "UIDropDownMenuTemplate")
		local dropbutton2 = Awwwww_Dropdown2Button
		local dropTable2 = {}
		for k,v in pairs(channels) do
			dropTable2[k] = {
				text = v,
				checked = function() return AwwwwwDB.channel == k end,
				func = function()
						AwwwwwDB.channel = k
						UIDropDownMenu_SetText(dropdown2, v)
					end,
			}
		end
		UIDropDownMenu_SetText(dropdown2, channels[AwwwwwDB.channel])
		UIDropDownMenu_SetWidth(dropdown2, 175)
		dropdown2:SetPoint("TOPRIGHT", f, "TOPRIGHT", -30, -100)
		dropdown2.point = "TOPRIGHT"
		dropdown2.relativePoint = "BOTTOMRIGHT"
		dropbutton2:SetScript("OnClick", function(self, button, down)
				EasyMenu(dropTable2, dropdown2, self:GetName(), 0, 5, nil, 5)
			end)
		dropbutton2:SetScript("OnEnter", function(self) OnEnter(self,"Sound Channel","Select which channel the sound should be played on.  The volume of individual channels and the Master game volume may be adjusted by going to: Escape > Options > Sound.") end)
		dropbutton2:SetScript("OnLeave", function() GameTooltip:Hide() end)
	local dropTitle2 = dropdown2:CreateFontString(nil, "ARTWORK", "GameFontNormal")
		--dropTitle2:SetParent(f)
		dropTitle2:SetPoint("BOTTOMLEFT", dropdown2, "TOPLEFT", 20,5)
		dropTitle2:SetText("Sound Channel")
end

local function OnInitialize()
	AwwwwwDB = AwwwwwDB or {}
	for k,v in pairs(defaults) do
	    if type(AwwwwwDB[k]) == "nil" then
	        AwwwwwDB[k] = v
	    end
	end
	
	f.name = "Awwwww!"
	InterfaceOptions_AddCategory(f)
	AddOptions()
	
	f:UnregisterEvent("PLAYER_LOGIN")
	f:RegisterEvent("PLAYER_DEAD")
	f:SetScript("OnEvent", DeathSound)
	
	AddOptions = nil
	OnInitialize = nil
end


f:SetScript("OnEvent", OnInitialize)
f:RegisterEvent("PLAYER_LOGIN")