<Ui>

	<Frame name="TellMeWhen_CustomTex" inherits="TellMeWhen_OptionsModuleContainer" virtual="true">
		<Frames>
			<EditBox name="$parentCustomTex" inherits="TellMeWhen_InputBoxTemplate" multiline="true" parentKey="CustomTex" autoFocus="false">
				<Size y="15"/>
				<Anchors>
					<Anchor point="TOPLEFT" x="9" y="-5"/>
					<Anchor point="TOPRIGHT" x="-7" y="-5"/>
				</Anchors>
				<Scripts>
					<OnLoad>
						TMW.SUG:EnableEditBox(self, "texture_withVarTex", true)	
						self.BackgroundText:SetWidth(self:GetWidth())
						self.label = TMW.L["ICONMENU_CUSTOMTEX"]
						
						self.__ttMinWidth = 300
						TMW:CInit(self, {
							setting = "CustomTex",
							title = TMW.L["ICONMENU_CUSTOMTEX"],
							tooltip = TMW.L["ICONMENU_CUSTOMTEX_DESC"],
							OnTextChanged = function(self)
								self:GetParent():AdjustHeight()
							end,
							ModifySettingValue = function(self, value)
								return value:gsub([[\\]], [[\]])
							end,
						})
						
						
						function self:DragReceived(icon, t, data, subType)
							local ics = icon:GetSettings()

							local _, input
							if t == "spell" then
								_, input = GetSpellBookItemInfo(data, subType)
							elseif t == "item" then
								input = GetItemIcon(data)
							end
							if not input then
								return
							end

							ics.CustomTex = TMW:CleanString(input)
							return true -- signal success
						end
						
						TMW.Classes.ChatEdit_InsertLink_Hook:New(self, function(self, text, linkType, linkData)
							-- if the custom texture box is active,
							-- attempt to extract either a spellID or a texture path from the data to use.

							local tex
							local linkID = strmatch(linkData, "^(%d+)")

							if linkType == "spell" or linkType == "enchant" then
								-- spells and enchants can just use their spellID
								tex = linkID
							elseif linkType == "item" then
								-- items must get the texture path
								tex = GetItemIcon(linkID)
							elseif linkType == "achievement" then
								-- achievements also must get their texture path
								tex = select(10, GetAchievementInfo(linkID))
							end

							if tex then
								-- clean off the first part of the path, it does not need to be saved
								-- it will be appended when the texture is used.
								tex = gsub(tex, "INTERFACE\\ICONS\\", "")
								tex = gsub(tex, "Interface\\Icons\\", "")

								-- set the text
								self.editbox:SetText(tex)

								-- notify success
								return true
							end
						end)

					</OnLoad>
					<OnReceiveDrag>
						TMW.IE:SpellItemToIcon(TMW.CI.icon, self.DragReceived, self)
					</OnReceiveDrag>
					<OnMouseUp>
						TMW.IE:SpellItemToIcon(TMW.CI.icon, self.DragReceived, self)
					</OnMouseUp>
				</Scripts>
			</EditBox>
		</Frames>
		<Scripts>
			<OnLoad>
				self.Header:SetText(TMW.L["ICONMENU_CUSTOMTEX"])
				
				function self:AdjustHeight()
				--	self:SetHeight(11 + self.CustomTex:GetHeight())
					TMW:AnimateHeightChange(self, 11 + self.CustomTex:GetHeight(), 0.1)
				end
			</OnLoad>
		</Scripts>
	</Frame>
	
</Ui>