<Ui>
	<CheckButton name="TellMeWhen_SoundSelectButton" inherits="TellMeWhen_FlatFrame" virtual="true" >
		<Size y="13"/>
		<Anchors>
			<Anchor point="LEFT"/>
			<Anchor point="RIGHT"/>
		</Anchors>
		<Frames>
			<Button parentKey="Play" virtual="true">
				<Size x="14" y="14"/>
				<Anchors>
					<Anchor point="RIGHT" x="-2" y="0"/>
				</Anchors>
				<Scripts>
					<OnClick>
						local Sound = TMW.EVENTS:GetEventHandler("Sound")
						Sound:TestSound(self, self:GetParent().soundFile)
					</OnClick>
					<OnEnter>
						self:GetParent():LockHighlight()
					</OnEnter>
					<OnLeave>
						self:GetParent():UnlockHighlight()
					</OnLeave>
				</Scripts>
				<NormalTexture file="Interface\Common\VoiceChat-Speaker"/>
				<HighlightTexture file="Interface\Common\VoiceChat-On"/>
			</Button>
		</Frames>
		<Layers>
			<Layer level="OVERLAY">
				<FontString parentKey="Name" inherits="GameFontHighlightSmall" justifyH="LEFT">
					<Anchors>
						<Anchor point="LEFT" x="3"/>
						<Anchor point="RIGHT" relativeKey="$parent.Play" relativePoint="LEFT" x="-1"/>
					</Anchors>
				</FontString>
			</Layer>
		</Layers>
	</CheckButton>
	
	<Frame parentKey="Sound" name="$parentSound" inherits="TellMeWhen_EventContainer">
		<Frames>
			<Frame parentKey="SoundList">
				<Anchors>
					<Anchor point="TOPLEFT" y="0"/>
					<Anchor point="BOTTOMRIGHT" x="-20" y="50"/>
				</Anchors>

				<Scripts>
					<OnLoad>
						local ScrollFrame = TMW:ConvertContainerToScrollFrame(self, true, 5, 8)
						ScrollFrame:SetWheelStepAmount(25)
					</OnLoad>
				</Scripts>
			</Frame>

			<EditBox parentKey="Custom" inherits="TellMeWhen_InputBoxTemplate">
				<Size y="15"/>
				<Anchors>
					<Anchor point="LEFT" relativeKey="$parent.SoundList.ScrollFrame" x="5" />
					<Anchor point="RIGHT" relativeKey="$parent.SoundList.ScrollFrame" x="-15" />
					<Anchor point="TOP" relativeKey="$parent.SoundList.ScrollFrame" relativePoint="BOTTOM" y="-20"/>
				</Anchors>
				<Frames>
					<Button parentKey="Play" virtual="true">
						<Size x="20" y="20"/>
						<Anchors>
							<Anchor point="LEFT" relativePoint="RIGHT" x="5"/>
						</Anchors>

						<NormalTexture file="Interface\Common\VoiceChat-Speaker"/>
						<HighlightTexture file="Interface\Common\VoiceChat-On"/>

						<Scripts>
							<OnClick>
								local Sound = TMW.EVENTS:GetEventHandler("Sound")
								Sound:TestSound(self, self:GetParent():GetText())
							</OnClick>
						</Scripts>
					</Button>
				</Frames>
				<Layers>
					<Layer level="OVERLAY">
						<FontString parentKey="Header" inherits="GameFontNormal" >
							<Anchors>
								<Anchor point="BOTTOM" relativePoint="TOP" y="1"/>
							</Anchors>
						</FontString>
					</Layer>
				</Layers>
				<Scripts>
					<OnLoad>
						TMW:CInit(self)

						self:SetSetting("Sound")
						self:SetTexts(TMW.L["SOUND_CUSTOM"], TMW.L["SOUND_CUSTOM_DESC"])
						self.Header:SetText(TMW.L["SOUND_CUSTOM"])

						self:CScriptAdd("ModifyValueForSave", function(self, value)
							value = TMW:CleanPath(value)

							local number = value:match("script PlaySoundKitID%((.*)%)")
							if number then
								return number
							end

							return value
						end)

						self:CScriptAdd("ModifyValueForLoad", function(self, value)
							local Sound = TMW.EVENTS:GetEventHandler("Sound")

							local listID = Sound.List and TMW.tContains(Sound.List, value)

							-- If the sound is found in the LSM sounds, dont display it in this editbox.
							if listID then
								value = ""
							end

							return value
						end)


						self:CScriptAdd("SettingSaved", function(self)
							local soundFile = self:GetText()

							local warningText
							if tonumber(soundFile) then
								-- this is ok - its a soundkit ID
							elseif not strfind(soundFile, "%.[^\\/]+$") then
								-- Warn about no extensions
								warningText = soundFile ~= "" and TMW.L["SOUNDERROR1"]
							elseif strfind(strlower(soundFile), "%.wav$") and select(4, GetBuildInfo()) > 40000 then
								-- Custom wav files are no longer allowed.
								-- Don't force it to be nothing, because the user might be entering a built-in wav file,
								-- which is still valid.
								warningText = TMW.L["SOUNDERROR2"]
							elseif not (strfind(strlower(soundFile), "%.ogg$") or strfind(strlower(soundFile), "%.mp3$")) then
								-- Only ogg and mp3 are supported.
								warningText = TMW.L["SOUNDERROR3"]
							end

							if warningText then
								TMW.HELP:Show{
									code = "SND_INVALID_CUSTOM",
									icon = nil,
									relativeTo = self,
									x = 0,
									y = 0,
									text = format(warningText)
								}
							else
								TMW.HELP:Hide("SND_INVALID_CUSTOM")
							end
						end)
					</OnLoad>
					<OnUpdate>
						local SoundList = self:GetParent().SoundList
						-- Keep the custom box anchored to the end of the list if the scroll frame
						-- isn't extending to the bottom of the page.
						if SoundList.ScrollFrame:GetVerticalScrollRange() == 0 then
							local Sound = TMW.EVENTS:GetEventHandler("Sound")
							self:SetPoint("TOP", SoundList.frames[#Sound.List], "BOTTOM", 0, -20)
						else
							self:SetPoint("TOP", SoundList.ScrollFrame, "BOTTOM", 0, -20)
						end
					</OnUpdate>
				</Scripts>
			</EditBox>
		</Frames>

		<Layers>
			<Layer level="OVERLAY">
				<FontString parentKey="Header" inherits="GameFontNormalLarge" justifyH="CENTER" text="">
					<Anchors>
						<Anchor point="BOTTOM" relativeKey="$parent.SoundList.ScrollFrame" relativePoint="TOP" y="1"/>
					</Anchors>
				</FontString>
			</Layer>
		</Layers>
	</Frame>
	
</Ui>
