local _, addon = ...
local L = TomTomLocals

local function initPasteWindow()
    if addon.pasteWindow then
        return addon.pasteWindow
    end

    addon.pasteWindow = CreateFrame("Frame", "TomTomPaste", UIParent, "DefaultPanelTemplate,ClickToDragTemplate")

    local frame = addon.pasteWindow
    frame:SetHeight(450)
    frame:SetWidth(465)
    frame:SetFrameStrata("MEDIUM")
    frame:ClearAllPoints()
    frame.TitleContainer.TitleText:SetText(L["TomTom Paste"])
    frame:SetPoint("CENTER", 0, 200)
    frame:Hide()

    -- Edit box time!
    frame.EditBox = CreateFrame("Frame", "TomTomPasteEditBox", frame, "TomTomScrollingEditBoxTemplate")
    local editBox = frame.EditBox

    editBox:SetPoint("TOPLEFT", frame, "TOPLEFT", 14, -80)
    editBox:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -10, 40)

    editBox:SetWidth(435)
    editBox:SetHeight(85)

    local label = L["Add several /way commands here and click Paste"]
    editBox.Label = editBox:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    editBox.Label:SetPoint("BOTTOMLEFT", editBox, "TOPLEFT", 0, 5)
    editBox.Label:SetPoint("BOTTOMRIGHT", editBox, "TOPRIGHT", 0, 5)
    editBox.Label:SetWordWrap(true)
    editBox.Label:SetMaxLines(9)
    editBox.Label:SetJustifyV("BOTTOM")
    editBox.Label:SetJustifyH("LEFT")
    editBox.Label:SetText(label)
    editBox.Label:Hide()

    local function OnTextChanged(o, editBox, userChanged)
        local text = editBox:GetText()
    end

    local function OnEscapePressed(o, editBox)
        editBox:ClearFocus()
    end

    local function OnEnterPressed(o, editBox)
        local text = editBox:GetText()
        text = text .. "\n"
        editBox:SetText(text)
     end

    editBox.ScrollingEditBox:RegisterCallback("OnTextChanged", OnTextChanged, editBox)
    editBox.ScrollingEditBox:RegisterCallback("OnEscapePressed", OnEscapePressed, editBox)
    editBox.ScrollingEditBox:RegisterCallback("OnEnterPressed", OnEnterPressed, editBox)

    local scrollBox = editBox.ScrollingEditBox:GetScrollBox()
    ScrollUtil.RegisterScrollBoxWithScrollBar(scrollBox, editBox.ScrollBar)

    local scrollBoxAnchorsWithBar = {
        CreateAnchor("TOPLEFT", editBox.ScrollingEditBox, "TOPLEFT", 0, 0),
        CreateAnchor("BOTTOMRIGHT", editBox.ScrollingEditBox, "BOTTOMRIGHT", -18, -1),
    }
    local scrollBoxAnchorsWithoutBar = {
        scrollBoxAnchorsWithBar[1],
        CreateAnchor("BOTTOMRIGHT", editBox.ScrollingEditBox, "BOTTOMRIGHT", -2, -1),
    }
    ScrollUtil.AddManagedScrollBarVisibilityBehavior(scrollBox, editBox.ScrollBar, scrollBoxAnchorsWithBar, scrollBoxAnchorsWithoutBar)

    frame.CloseButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
    frame.CloseButton:SetText(L["Close"])
    frame.CloseButton:SetHeight(23)
    frame.CloseButton:SetWidth(100)
    frame.CloseButton:ClearAllPoints()
    frame.CloseButton:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -6, 5)
    frame.CloseButton:SetScript("OnClick", function(button)
        frame:SetShown(false)
    end)

    frame.PasteButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
    frame.PasteButton:SetText(L["Paste"])
    frame.PasteButton:SetHeight(23)
    frame.PasteButton:SetWidth(100)
    frame.PasteButton:ClearAllPoints()
    frame.PasteButton:SetPoint("RIGHT", frame.CloseButton, "LEFT", 0, 0)
    frame.PasteButton:SetScript("OnClick", function(button)
        local text = frame.EditBox.ScrollingEditBox:GetText()
        local lines = {string.split("\n", text)}

        for idx, line in ipairs(lines) do
            -- remove the first token from the commands
            line = line:gsub("^%S+", "")

            addon.SlashWayCommand(line)
        end
    end)

    return addon.pasteWindow
end

-- editbox:SetScript('OnEnterPressed', function()
-- 	if IsControlKeyDown() then
-- 		submit:Click()
-- 	end
-- end)

SLASH_TOMTOM_PASTE1 = "/ttpaste"
SLASH_TOMTOM_PASTE2 = "/tomtompaste"
SLASH_TOMTOM_PASTE3 = "/ttp"

SlashCmdList["TOMTOM_PASTE"] = function(msg)
    local window = initPasteWindow()
    window:SetShown(not window:IsShown())
end
