local E, L, V, P, G = unpack(ElvUI);
local D = E:GetModule('Distributor')
local mPlugin = "mMediaTag"
local mMT = E:GetModule(mPlugin);
local addon, ns = ...
local profileTypeItems = { profile = L["Profile"], private = L["Private (Character Settings)"], global = L["Global (Account Settings)"], filters = L["Aura Filters"], styleFilters = L["NamePlate Style Filters"] }


function mMT:mProfileWindow(ProfileText, opt1)
	local Frame = E.Libs.AceGUI:Create('Frame')
	Frame:SetTitle(format("%s: %s", L["Import Profile"], opt1))
	Frame:EnableResize(false)
	Frame:SetWidth(400)
	Frame:SetHeight(500)
	Frame.frame:SetFrameStrata('FULLSCREEN_DIALOG')
	Frame:SetLayout('flow')

	local Box = E.Libs.AceGUI:Create('MultiLineEditBox-ElvUI')
	Box:SetNumLines(20)
	Box:DisableButton(true)
	Box:SetWidth(400)
	Box:SetLabel('')
	Box:SetText(ProfileText)
	Frame:AddChild(Box)
	--Save original script so we can restore it later
	Box.editBox.OnTextChangedOrig = Box.editBox:GetScript('OnTextChanged')
	Box.editBox.OnCursorChangedOrig = Box.editBox:GetScript('OnCursorChanged')
	--Remove OnCursorChanged script as it causes weird behaviour with long text
	Box.editBox:SetScript('OnCursorChanged', nil)
	Box.scrollFrame:UpdateScrollChildRect()

	local Label1 = E.Libs.AceGUI:Create('Label')
	local font = GameFontHighlightSmall:GetFont()
	Label1:SetFont(font, 14)
	Label1:SetText('.') --Set temporary text so height is set correctly
	Label1:SetWidth(400)
	Frame:AddChild(Label1)

	local Label2 = E.Libs.AceGUI:Create('Label')
	font = GameFontHighlightSmall:GetFont()
	Label2:SetFont(font, 14)
	Label2:SetText('.|n.')
	Label2:SetWidth(400)
	Frame:AddChild(Label2)

	local importButton = E.Libs.AceGUI:Create('Button-ElvUI') --This version changes text color on SetDisabled
	importButton:SetDisabled(true)
	importButton:SetText(L["Import Now"])
	importButton:SetAutoWidth(true)
	importButton:SetCallback('OnClick', function()
		Label1:SetText('')
		Label2:SetText('')

		local success = D:ImportProfile(Box:GetText())
		Label1:SetText((success and L["Profile imported successfully!"]) or L["Error decoding data. Import string may be corrupted!"])
	end)
	Frame:AddChild(importButton)

	local decodeButton = E.Libs.AceGUI:Create('Button-ElvUI')
	decodeButton:SetDisabled(true)
	decodeButton:SetText(L["Decode Text"])
	decodeButton:SetAutoWidth(true)
	decodeButton:SetCallback('OnClick', function()
		Label1:SetText('')
		Label2:SetText('')

		local profileType, profileKey, profileData = D:Decode(Box:GetText())
		local decodedText = (profileData and E:TableToLuaString(profileData)) or nil
		local importText = D:CreateProfileExport(decodedText, profileType, profileKey)
		Box:SetText(importText)
	end)
	Frame:AddChild(decodeButton)

	local oldText = ''
	local function OnTextChanged(_, userInput)
		if userInput then
			--Prevent user from changing export string
			Box:SetText(ProfileText)
		else
			--Scroll frame doesn't scroll to the bottom by itself, so let's do that now
			Box.scrollFrame:SetVerticalScroll(Box.scrollFrame:GetVerticalScrollRange())
		end

		local text = Box:GetText()
		if text == '' then
			Label1:SetText('')
			Label2:SetText('')
			importButton:SetDisabled(true)
			decodeButton:SetDisabled(true)
		elseif oldText ~= text then
			local stringType = D:GetImportStringType(text)
			if stringType == 'Base64' then
				decodeButton:SetDisabled(false)
			else
				decodeButton:SetDisabled(true)
			end

			local profileType, profileKey = D:Decode(text)
			if not profileType or (profileType and profileType == 'profile' and not profileKey) then
				Label1:SetText(L["Error decoding data. Import string may be corrupted!"])
				Label2:SetText('')
				importButton:SetDisabled(true)
				decodeButton:SetDisabled(true)
			else
				Label1:SetText(format('%s: %s%s|r', L["Importing"], E.media.hexvaluecolor, profileTypeItems[profileType] or ''))
				if profileType == 'profile' then
					Label2:SetText(format('%s: %s%s|r', L["Profile Name"], E.media.hexvaluecolor, profileKey))
				end

				--Scroll frame doesn't scroll to the bottom by itself, so let's do that now
				Box.scrollFrame:UpdateScrollChildRect()
				Box.scrollFrame:SetVerticalScroll(Box.scrollFrame:GetVerticalScrollRange())

				importButton:SetDisabled(false)
			end

			oldText = text
		end
	end

	Box.editBox:SetFocus()
	Box.editBox:SetScript('OnChar', nil)
	Box.editBox:SetScript('OnTextChanged', OnTextChanged)

	Frame:SetCallback('OnClose', function(widget)
		--Restore changed scripts
		Box.editBox:SetScript('OnChar', nil)
		Box.editBox:SetScript('OnTextChanged', Box.editBox.OnTextChangedOrig)
		Box.editBox:SetScript('OnCursorChanged', Box.editBox.OnCursorChangedOrig)
		Box.editBox.OnTextChangedOrig = nil
		Box.editBox.OnCursorChangedOrig = nil

		E.Libs.AceGUI:Release(widget)
		E:Config_OpenWindow()
	end)

	--Clear default text
	Label1:SetText('')
	Label2:SetText('')

	--Close ElvUI OptionsUI
	E.Libs.AceConfigDialog:Close('ElvUI')

	GameTooltip_Hide() --The tooltip from the Export/Import button stays on screen, so hide it
end
