local _, NSI = ... -- Internal namespace

local SoundList = {
    -- [spellID] = "SoundName", use false to remove a sound

    -- Midnight S1
    [1284527] = "Targeted", -- Galvanize
    [1283236] = "Targeted", --Void Expulsion
    [1283069] = "Fixate", -- Weakened
    [1281184] = "Spread", -- Criticality
    [1280023] = "Targeted", -- Void Marked
    --[1279512] = "idk", -- Shatterglass - maybe adding this later
    [1249609] = "Rune", -- Dark Rune
    [1268992] = "Targeted", -- Shattering Twilight
    [1253024] = "Targeted", -- Shattering Twilight (Tank)
    [1270497] = "Spread", -- Shadowmark
    [1264756] = "Targeted", -- Rift Madness
    [1260027] = "Targeted", -- Grasp of Emptiness
    [1232470] = "Targeted", -- Grasp of Emptiness (idk which one is correct)
    [1260203] = "Soak", -- Umbral Collapse
    [1249265] = "Soak", -- Umbral Collapse (one of them is 2nd cast I think?)
    [1259861] = "Targeted", -- Ranger Captain's Mark
    [1237623] = "Targeted", -- Ranger Captain's Mark(idk which one is correct)
 --   [1262983] = "Light", -- Twilight Seal (Light) - maybe adding this later, not sure if this is used at all
 --   [1262972] = "Void", -- Twilight Seal (Void) - maybe adding this later, not sure if this is used at all
    [1257087] = "Clear", -- Consuming Miasma
    [1255612] = "Targeted", -- Dread Breath
    [1248697] = "Debuff", -- Despotic Command
    [1248994] = "Targeted", -- Execution Sentence
    [1248985] = "Targeted", -- Execution Sentence (not sure if this one is used)
    [1246487] = "Spread", -- Avenger's Shield
    [1242091] = "Targeted", -- Void Quill
    [1241992] = "Targeted", -- Light Quill
    [1241339] = "Void", -- Void Dive
    [1241292] = "Light", -- Light Dive
    [1239111] = "Break", -- Aspect of the End
    [1233887] = "Debuff", -- Null Corona
    [1254113] = "Fixate", -- Vorasius Fixate
}

function NSI:AddPASound(spellID, sound)
    if (not spellID) or (not (C_UnitAuras.AuraIsPrivate(spellID))) then return end
    C_UnitAuras.RemovePrivateAuraAppliedSound(spellID)
    if not sound then return end -- essentially calling the function without a soundpath removes the sound (when user removes it in the UI)
    local soundPath = NSI.LSM:Fetch("sound", sound)
    if soundPath and soundPath ~= 1 then
        C_UnitAuras.AddPrivateAuraAppliedSound({
            unitToken = "player",
            spellID = spellID,
            soundFileName = soundPath,
            outputChannel = "master",
        })
    end
end

function NSI:ApplyDefaultPASounds()
    for spellID, sound in pairs(SoundList) do
        local curSound = NSRT.PASounds[spellID]
        if (not curSound) or (not curSound.edited) then -- only add default sound if user hasn't edited it prior
            if not sound then -- if sound is false in the table I have marked it to be removed to clean up the table from old content
                NSRT.PASounds[spellID] = nil
                self:AddPASound(spellID, nil)
            else
                sound = "|cFF4BAAC8"..sound.."|r"
                NSRT.PASounds[spellID] = {sound = sound, edited = false}
                self:AddPASound(spellID, sound)
            end
        end
    end
end

function NSI:SavePASound(spellID, sound)
    if (not spellID) or (not (C_UnitAuras.AuraIsPrivate(spellID))) then return end
    NSRT.PASounds[spellID] = {sound = sound, edited = true}
    self:AddPASound(spellID, sound)
end

function NSI:InitTextPA()   
    if not self.PATextMoverFrame then
        self.PATextMoverFrame = CreateFrame("Frame", nil, self.NSRTFrame)     
        self.PATextMoverFrame:SetPoint(NSRT.PATextSettings.Anchor, self.NSRTFrame, NSRT.PATextSettings.relativeTo, NSRT.PATextSettings.xOffset, NSRT.PATextSettings.yOffset)
        
        self.PATextMoverFrame.Text = self.PATextMoverFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")   
        self.PATextMoverFrame.Text:SetFont(NSI.LSM:Fetch("font", "Expressway"), NSRT.PATextSettings.Scale*20, "OUTLINE")
        self.PATextMoverFrame.Text:SetText("<secret value> targets you with the spell <secret value>")
        self.PATextMoverFrame:SetSize(self.PATextMoverFrame.Text:GetStringWidth()*1, self.PATextMoverFrame.Text:GetStringHeight()*1.5)
        self.PATextMoverFrame.Text:SetPoint("CENTER", self.PATextMoverFrame, "CENTER", 0, 0)

        if not self.PATextMoverFrame.Border then
            self.PATextMoverFrame.Border = CreateFrame("Frame", nil, self.PATextMoverFrame, "BackdropTemplate") 
            self.PATextMoverFrame.Border:SetPoint("TOPLEFT", self.PATextMoverFrame, "TOPLEFT", -6, 6)
            self.PATextMoverFrame.Border:SetPoint("BOTTOMRIGHT", self.PATextMoverFrame, "BOTTOMRIGHT", 6, -6)
            self.PATextMoverFrame.Border:SetBackdrop({
                    edgeFile = "Interface\\Buttons\\WHITE8x8",
                    edgeSize = 2,
                })
            self.PATextMoverFrame.Border:SetBackdropBorderColor(1, 1, 1, 1)
            self.PATextMoverFrame.Border:Hide()
        end
        self.PATextMoverFrame:SetMovable(false)
        self.PATextMoverFrame:EnableMouse(false)
        self.PATextMoverFrame:SetClampedToScreen(true)
        self.PATextMoverFrame:RegisterForDrag("LeftButton")
        self.PATextMoverFrame:Hide()
        self.PATextMoverFrame.Border:Show()
        self.PATextMoverFrame:SetScript("OnDragStart", function(self)
            self:StartMoving()
        end)
        self.PATextMoverFrame:SetScript("OnDragStop", function(Frame)
            Frame:StopMovingOrSizing()       
            local Anchor, _, relativeTo, xOffset, yOffset = Frame:GetPoint()
            xOffset = Round(xOffset)
            yOffset = Round(yOffset)
            NSRT.PATextSettings.xOffset = xOffset     
            NSRT.PATextSettings.yOffset = yOffset  
            NSRT.PATextSettings.Anchor = Anchor    
            NSRT.PATextSettings.relativeTo = relativeTo    
        end)
    end
    if NSRT.PATextSettings.enabled then
        if not self.PATextWarning then
            self.PATextWarning = CreateFrame("Frame", nil, self.NSRTFrame)
        end

        local height = self.PATextMoverFrame:GetHeight()
        self.PATextWarning:SetPoint("TOPLEFT", self.PATextMoverFrame, "TOPLEFT", 0, -0.8*self.PATextMoverFrame:GetHeight()/NSRT.PATextSettings.Scale)
        self.PATextWarning:SetPoint("BOTTOMRIGHT", self.PATextMoverFrame, "BOTTOMRIGHT", 0, -0.8*self.PATextMoverFrame:GetHeight()/NSRT.PATextSettings.Scale)
        self.PATextWarning:SetScale(NSRT.PATextSettings.Scale)

        local textanchor = 
        {
            point = "CENTER", 
            relativeTo = self.PATextWarning, 
            relativePoint = "CENTER", 
            offsetX = 0, 
            offsetY = 0
        }
        C_UnitAuras.SetPrivateWarningTextAnchor(self.PATextWarning, textanchor)
    end
end

function NSI:InitPA()
    
    if not self.PAFrames then
        self.PAFrames = {}
    end
    
    if not self.AddedPA then self.AddedPA = {} end    
    local xDirection = (NSRT.PASettings.GrowDirection == "RIGHT" and 1) or (NSRT.PASettings.GrowDirection == "LEFT" and -1) or 0
    local yDirection = (NSRT.PASettings.GrowDirection == "DOWN" and -1) or (NSRT.PASettings.GrowDirection == "UP" and 1) or 0
    local borderSize = NSRT.PASettings.HideBorder and -100 or NSRT.PASettings.Width/16
    for auraIndex=1, 10 do
        if not self.PAFrames[auraIndex] then
            self.PAFrames[auraIndex] = CreateFrame("Frame", nil, self.NSRTFrame)
        end
        if NSRT.PASettings.HideTooltip then
            self.PAFrames[auraIndex]:SetSize(1, 1)
        else
            self.PAFrames[auraIndex]:SetSize(NSRT.PASettings.Width, NSRT.PASettings.Height)
        end
        self.PAFrames[auraIndex]:SetPoint(NSRT.PASettings.Anchor, self.NSRTFrame, NSRT.PASettings.relativeTo, 
        NSRT.PASettings.xOffset+(auraIndex-1) * (NSRT.PASettings.Width+NSRT.PASettings.Spacing) * xDirection, 
        NSRT.PASettings.yOffset+(auraIndex-1) * (NSRT.PASettings.Height+NSRT.PASettings.Spacing) * yDirection)

        local anchorID = "NSRT_PA"..auraIndex
        if self.AddedPA[anchorID] then
            C_UnitAuras.RemovePrivateAuraAnchor(self.AddedPA[anchorID])
            self.AddedPA[anchorID] = nil
        end    
        if NSRT.PASettings.enabled and NSRT.PASettings.Limit >= auraIndex then
            local privateAnchorArgs = {
                unitToken = "player",
                auraIndex = auraIndex,
                parent = self.PAFrames[auraIndex],
                showCountdownFrame = true,
                showCountdownNumbers = true,
                iconInfo = {
                    iconAnchor = {
                        point = "CENTER",
                        relativeTo = self.PAFrames[auraIndex],
                        relativePoint = "CENTER",
                        offsetX = 0,
                        offsetY = 0,
                    },
                    borderScale = borderSize,
                iconWidth = NSRT.PASettings.Width,
                iconHeight = NSRT.PASettings.Height,
                }
            }        
            self.AddedPA[anchorID] = C_UnitAuras.AddPrivateAuraAnchor(privateAnchorArgs)
        end
    end
end

function NSI:InitRaidPA(party, firstcall) -- still run this function if disabled to clean up old anchors
    if not self.PARaidFrames then self.PARaidFrames = {} end
    if not self.AddedPARaid then self.AddedPARaid = {} end
    local borderSize = NSRT.PARaidSettings.HideBorder and -100 or NSRT.PARaidSettings.Width/16
    for i=1, party and 5 or 40 do       
        local anchorID = party and "NSRT_PAParty"..i or "NSRT_PARaid"..i
        if self.AddedPARaid and self.AddedPARaid[anchorID] then
            for auraIndex = 1, 10 do
                if self.AddedPARaid[anchorID][auraIndex] then
                    C_UnitAuras.RemovePrivateAuraAnchor(self.AddedPARaid[anchorID][auraIndex])
                    self.AddedPARaid[anchorID][auraIndex] = nil
                end
            end
        end
        local u = party and "party"..i or "raid"..i
        if party and i == 5 then u = "player" end
        if NSRT.PARaidSettings.enabled and UnitExists(u) then 
            local F = self.LGF.GetUnitFrame(u)
            if firstcall and not F then
                C_Timer.After(5, function() self:InitRaidPA(party, false) end)
                return
            end
            if F then
                if not self.PARaidFrames[i] then
                    self.PARaidFrames[i] = CreateFrame("Frame", nil, self.NSRTFrame)
                end
                self.PARaidFrames[i]:SetSize(1, 1)
                self.PARaidFrames[i]:SetPoint(NSRT.PARaidSettings.Anchor, F, NSRT.PARaidSettings.relativeTo, NSRT.PARaidSettings.xOffset, NSRT.PARaidSettings.yOffset)
                local xDirection = (NSRT.PARaidSettings.GrowDirection == "RIGHT" and 1) or (NSRT.PARaidSettings.GrowDirection == "LEFT" and -1) or 0
                local yDirection = (NSRT.PARaidSettings.GrowDirection == "DOWN" and -1) or (NSRT.PARaidSettings.GrowDirection == "UP" and 1) or 0
                self.AddedPARaid[anchorID] = {}
                for auraIndex = 1, 10 do
                    if auraIndex > NSRT.PARaidSettings.Limit then break end
                    local privateAnchorArgs = {
                        unitToken = u,
                        auraIndex = auraIndex,
                        parent = self.PARaidFrames[i],
                        showCountdownFrame = true,
                        showCountdownNumbers = true,
                        iconInfo = {
                            iconAnchor = {
                                point = NSRT.PARaidSettings.Anchor,
                                relativeTo = self.PARaidFrames[i],
                                relativePoint = NSRT.PARaidSettings.relativeTo,
                                offsetX = 0 + (auraIndex-1) * (NSRT.PARaidSettings.Width+NSRT.PARaidSettings.Spacing) * xDirection,
                                offsetY = 0 + (auraIndex-1) * (NSRT.PARaidSettings.Height+NSRT.PARaidSettings.Spacing) * yDirection,
                            },
                            borderScale = borderSize,
                            iconWidth = NSRT.PARaidSettings.Width,
                            iconHeight = NSRT.PARaidSettings.Height,
                        }
                    }    
                    self.AddedPARaid[anchorID][auraIndex] = C_UnitAuras.AddPrivateAuraAnchor(privateAnchorArgs)
                end
            end
        end
    end
end

function NSI:RemoveTankPA()
    for i, anchortable in ipairs(self.AddedTankPA) do
        if self.AddedTankPA[i] then
            for anchorID, anchor in pairs(anchortable) do
                if self.AddedTankPA[i][anchorID] then
                    C_UnitAuras.RemovePrivateAuraAnchor(anchor)
                    self.AddedTankPA[i][anchorID] = nil
                end
            end
        end
    end
end

function NSAPI:StartTankAuras()
    NSI:InitTankPA()
end

function NSI:InitTankPA()
    -- initiated on ENCOUNTER_START for tank players
    if not self.PATankFrames then
        self.PATankFrames = {}
    end    

    if not self.AddedTankPA then self.AddedTankPA = {} end
    local xDirection = (NSRT.PATankSettings.GrowDirection == "RIGHT" and 1) or (NSRT.PATankSettings.GrowDirection == "LEFT" and -1) or 0
    local yDirection = (NSRT.PATankSettings.GrowDirection == "DOWN" and -1) or (NSRT.PATankSettings.GrowDirection == "UP" and 1) or 0

    local multiTankx = (NSRT.PATankSettings.MultiTankGrowDirection == "RIGHT" and 1) or (NSRT.PATankSettings.MultiTankGrowDirection == "LEFT" and -1) or 0
    local multiTanky = (NSRT.PATankSettings.MultiTankGrowDirection == "DOWN" and -1) or (NSRT.PATankSettings.MultiTankGrowDirection == "UP" and 1) or 0
    local units = {}
    for unit in self:IterateGroupMembers() do
        if UnitGroupRolesAssigned(unit) == "TANK" and not (UnitIsUnit("player", unit)) then
            table.insert(units, unit)
        end
    end
    -- remove any previous anchor, also calling this on ENCOUNTER_END
    self:RemoveTankPA()
    local borderSize = NSRT.PATankSettings.HideBorder and -100 or NSRT.PATankSettings.Width/16
    for i, unit in ipairs(units) do
        if not self.PATankFrames[i] then
            self.PATankFrames[i] = {}
        end
        self.AddedTankPA[i] = self.AddedTankPA[i] or {}
        for auraIndex = 1, 10 do
            if not self.PATankFrames[i][auraIndex] then
                self.PATankFrames[i][auraIndex] = CreateFrame("Frame", nil, self.NSRTFrame)
            end
            if NSRT.PATankSettings.HideTooltip then
                self.PATankFrames[i][auraIndex]:SetSize(1, 1)
            else
                self.PATankFrames[i][auraIndex]:SetSize(NSRT.PATankSettings.Width, NSRT.PATankSettings.Height)
            end
            self.PATankFrames[i][auraIndex]:SetPoint(NSRT.PATankSettings.Anchor, self.NSRTFrame, NSRT.PATankSettings.relativeTo, 
            NSRT.PATankSettings.xOffset+(auraIndex-1) * (NSRT.PATankSettings.Width+NSRT.PATankSettings.Spacing) * xDirection + (i-1) * (NSRT.PATankSettings.Width+NSRT.PATankSettings.Spacing) * multiTankx, 
            NSRT.PATankSettings.yOffset+(auraIndex-1) * (NSRT.PATankSettings.Height+NSRT.PATankSettings.Spacing) * yDirection + (i-1) * (NSRT.PATankSettings.Height+NSRT.PATankSettings.Spacing) * multiTanky)

            local anchorID = "NSRT_TankPA"..auraIndex
            if self.AddedTankPA[i][anchorID] then
                C_UnitAuras.RemovePrivateAuraAnchor(self.AddedTankPA[i][anchorID])
                self.AddedTankPA[i][anchorID] = nil
            end
            if NSRT.PATankSettings.enabled and NSRT.PATankSettings.Limit >= auraIndex then
                local privateAnchorArgs = {
                    unitToken = unit,
                    auraIndex = auraIndex,
                    parent = self.PATankFrames[i][auraIndex],
                    showCountdownFrame = true,
                    showCountdownNumbers = true,
                    iconInfo = {
                        iconAnchor = {
                            point = "CENTER",
                            relativeTo = self.PATankFrames[i][auraIndex],
                            relativePoint = "CENTER",
                            offsetX = 0,
                            offsetY = 0,
                        },
                        borderScale = borderSize,
                    iconWidth = NSRT.PATankSettings.Width,
                    iconHeight = NSRT.PATankSettings.Height,
                    }
                }        
                self.AddedTankPA[i][anchorID] = C_UnitAuras.AddPrivateAuraAnchor(privateAnchorArgs)
            end
        end
    end
end

function NSI:UpdatePADisplay(Personal, Tank)
    if Personal then
        if self.IsPAPreview then
            self:PreviewPA(true)
        else
            self:PreviewPA(false)
            self:InitPA()
            self:InitTextPA()
        end
    elseif Tank then
        if self.IsTankPAPreview then
            self:PreviewTankPA(true)
        else
            self:PreviewTankPA(false)
        end
    else
        if self.IsRaidPAPreview then
            self:PreviewRaidPA(true, true)
        else
            self:PreviewRaidPA(false)
            self:InitRaidPA(UnitInRaid("player"))
        end
    end
end

function NSI:PreviewPA(Show)
    if not self.PAFrames then self:InitPA() end
    if not Show then
        
        if self.PAFrames[1].Border then self.PAFrames[1].Border:Hide() end
        self.PAFrames[1]:SetMovable(false)
        self.PAFrames[1]:EnableMouse(false)
        self.PATextMoverFrame:SetMovable(false)
        self.PATextMoverFrame:EnableMouse(false)
        self.PATextMoverFrame.Border:Hide()
        self.PATextMoverFrame.Text:Hide()
        self.PAFrames[1]:SetSize(1, 1)
        self:InitPA()
        self:InitTextPA()
        if self.PAPreviewIcons then
            for _, icon in ipairs(self.PAPreviewIcons) do
                icon:Hide()
            end
        end
        return
    end
    if not self.PATextMoverFrame then
        self:InitTextPA()
    end    
    self.PAFrames[1]:SetSize((NSRT.PASettings.Width), (NSRT.PASettings.Height))
    self.PAFrames[1]:SetPoint(NSRT.PASettings.Anchor, self.NSRTFrame, NSRT.PASettings.relativeTo, NSRT.PASettings.xOffset, NSRT.PASettings.yOffset)
    if not self.PAFrames[1].Border then
        self.PAFrames[1].Border = CreateFrame("Frame", nil, self.PAFrames[1], "BackdropTemplate") 
        self.PAFrames[1].Border:SetPoint("TOPLEFT", self.PAFrames[1], "TOPLEFT", -6, 6)
        self.PAFrames[1].Border:SetPoint("BOTTOMRIGHT", self.PAFrames[1], "BOTTOMRIGHT", 6, -6)
        self.PAFrames[1].Border:SetBackdrop({
                edgeFile = "Interface\\Buttons\\WHITE8x8",
                edgeSize = 2,
            })
        self.PAFrames[1].Border:SetBackdropBorderColor(1, 1, 1, 1)
        self.PAFrames[1].Border:Hide()
    end
    
    self.PATextMoverFrame:SetMovable(true)
    self.PATextMoverFrame:EnableMouse(true)
    self.PATextMoverFrame:Show()
    self.PATextMoverFrame.Border:Show()
    self.PATextMoverFrame.Text:Show()
    self.PATextMoverFrame.Text:SetFont(NSI.LSM:Fetch("font", "Expressway"), NSRT.PATextSettings.Scale*20, "OUTLINE")
    self.PATextMoverFrame:SetSize(self.PATextMoverFrame.Text:GetStringWidth()*1, self.PATextMoverFrame.Text:GetStringHeight()*1.5)
    self.PAFrames[1]:SetMovable(true)
    self.PAFrames[1]:EnableMouse(true)
    self.PAFrames[1]:RegisterForDrag("LeftButton")
    self.PAFrames[1]:SetClampedToScreen(true)
    self.PAFrames[1].Border:Show()
    self.PAFrames[1]:SetScript("OnDragStart", function(self)
        self:StartMoving()
    end)
    self.PAFrames[1]:SetScript("OnDragStop", function(Frame)
        Frame:StopMovingOrSizing()       
        local Anchor, _, relativeTo, xOffset, yOffset = Frame:GetPoint()
        xOffset = Round(xOffset)
        yOffset = Round(yOffset)
        NSRT.PASettings.xOffset = xOffset     
        NSRT.PASettings.yOffset = yOffset  
        NSRT.PASettings.Anchor = Anchor    
        NSRT.PASettings.relativeTo = relativeTo    
    end)

    if not self.PAPreviewIcons then
        self.PAPreviewIcons = {}
    end
    for i=1, 10 do
        if not self.PAPreviewIcons[i] then
            self.PAPreviewIcons[i] = self.PAFrames[1]:CreateTexture(nil, "ARTWORK")
            self.PAPreviewIcons[i]:SetTexture(237555)
        end
        if NSRT.PASettings.Limit >= i then
            local xOffset = (NSRT.PASettings.GrowDirection == "RIGHT" and (i-1)*(NSRT.PASettings.Width+NSRT.PASettings.Spacing)) or (NSRT.PASettings.GrowDirection == "LEFT" and -(i-1)*(NSRT.PASettings.Width+NSRT.PASettings.Spacing)) or 0
            local yOffset = (NSRT.PASettings.GrowDirection == "UP" and (i-1)*(NSRT.PASettings.Height+NSRT.PASettings.Spacing)) or (NSRT.PASettings.GrowDirection == "DOWN" and -(i-1)*(NSRT.PASettings.Height+NSRT.PASettings.Spacing)) or 0
            self.PAPreviewIcons[i]:SetSize(NSRT.PASettings.Width, NSRT.PASettings.Height)
            self.PAPreviewIcons[i]:SetPoint("CENTER", self.PAFrames[1], "CENTER", xOffset, yOffset)
            self.PAPreviewIcons[i]:Show()
        else
            self.PAPreviewIcons[i]:Hide()
        end
    end
end

function NSI:PreviewTankPA(Show)
    if not self.PATankFrames then self:InitTankPA() end
    if not self.PATankFrames[1] or not self.PATankFrames[1][1] then
        self.PATankFrames[1] = self.PATankFrames[1] or {}
        self.PATankFrames[1][1] = CreateFrame("Frame", nil, self.NSRTFrame)
        self.PATankFrames[1][1]:SetPoint(NSRT.PATankSettings.Anchor, self.NSRTFrame, NSRT.PATankSettings.relativeTo, NSRT.PATankSettings.xOffset, NSRT.PATankSettings.yOffset)
    end
    if not Show then
        if self.PATankFrames[1][1].Border then self.PATankFrames[1][1].Border:Hide() end
        self.PATankFrames[1][1]:SetMovable(false)
        self.PATankFrames[1][1]:EnableMouse(false)
        self.PATankFrames[1][1]:SetSize(1, 1)
        if self.PATankPreviewIcons then
            for _, icon in ipairs(self.PATankPreviewIcons) do
                icon:Hide()
            end
        end
        self:RemoveTankPA()
        if UnitGroupRolesAssigned("player") == "TANK" or NSRT.Settings.Debug then
            self:InitTankPA()
        end
        return
    end
    self.PATankFrames[1][1]:SetSize((NSRT.PATankSettings.Width), (NSRT.PATankSettings.Height))
    self.PATankFrames[1][1]:SetPoint(NSRT.PATankSettings.Anchor, self.NSRTFrame, NSRT.PATankSettings.relativeTo, NSRT.PATankSettings.xOffset, NSRT.PATankSettings.yOffset)
    if not self.PATankFrames[1][1].Border then
        self.PATankFrames[1][1].Border = CreateFrame("Frame", nil, self.PATankFrames[1][1], "BackdropTemplate") 
        self.PATankFrames[1][1].Border:SetPoint("TOPLEFT", self.PATankFrames[1][1], "TOPLEFT", -6, 6)
        self.PATankFrames[1][1].Border:SetPoint("BOTTOMRIGHT", self.PATankFrames[1][1], "BOTTOMRIGHT", 6, -6)
        self.PATankFrames[1][1].Border:SetBackdrop({
                edgeFile = "Interface\\Buttons\\WHITE8x8",
                edgeSize = 2,
            })
        self.PATankFrames[1][1].Border:SetBackdropBorderColor(1, 1, 1, 1)
        self.PATankFrames[1][1].Border:Hide()
    end

    self.PATankFrames[1][1]:SetMovable(true)
    self.PATankFrames[1][1]:EnableMouse(true)
    self.PATankFrames[1][1]:RegisterForDrag("LeftButton")
    self.PATankFrames[1][1]:SetClampedToScreen(true)
    self.PATankFrames[1][1].Border:Show()
    self.PATankFrames[1][1]:SetScript("OnDragStart", function(self)
        self:StartMoving()
    end)
    self.PATankFrames[1][1]:SetScript("OnDragStop", function(Frame)
        Frame:StopMovingOrSizing()       
        local Anchor, _, relativeTo, xOffset, yOffset = Frame:GetPoint()
        xOffset = Round(xOffset)
        yOffset = Round(yOffset)
        NSRT.PATankSettings.xOffset = xOffset
        NSRT.PATankSettings.yOffset = yOffset
        NSRT.PATankSettings.Anchor = Anchor
        NSRT.PATankSettings.relativeTo = relativeTo
    end)

    if not self.PATankPreviewIcons then
        self.PATankPreviewIcons = {}
    end
    for i=1, 10 do
        if not self.PATankPreviewIcons[i] then
            self.PATankPreviewIcons[i] = self.PATankFrames[1][1]:CreateTexture(nil, "ARTWORK")
            self.PATankPreviewIcons[i]:SetTexture(236318)
        end
        if NSRT.PATankSettings.Limit >= i then
            local xOffset = (NSRT.PATankSettings.GrowDirection == "RIGHT" and (i-1)*(NSRT.PATankSettings.Width+NSRT.PATankSettings.Spacing)) or (NSRT.PATankSettings.GrowDirection == "LEFT" and -(i-1)*(NSRT.PATankSettings.Width+NSRT.PATankSettings.Spacing)) or 0
            local yOffset = (NSRT.PATankSettings.GrowDirection == "UP" and (i-1)*(NSRT.PATankSettings.Height+NSRT.PATankSettings.Spacing)) or (NSRT.PATankSettings.GrowDirection == "DOWN" and -(i-1)*(NSRT.PATankSettings.Height+NSRT.PATankSettings.Spacing)) or 0
            self.PATankPreviewIcons[i]:SetSize(NSRT.PATankSettings.Width, NSRT.PATankSettings.Height)
            self.PATankPreviewIcons[i]:SetPoint("CENTER", self.PATankFrames[1][1], "CENTER", xOffset, yOffset)
            self.PATankPreviewIcons[i]:Show()
        else
            self.PATankPreviewIcons[i]:Hide()
        end
    end
end

function NSI:PreviewRaidPA(Show, Init)
    if not Show then
        if self.PARaidPreviewFrame then self.PARaidPreviewFrame:Hide() end
        return
    end
    local MyFrame = self.LGF.GetUnitFrame("player")
    if not MyFrame then -- try again if no frame was found, as the first querry returns nil
        if Init then
            if self.RepeatRaidPAPreview then self.RepeatRaidPAPreview:Cancel() end
            self.RepeatRaidPAPreview = C_Timer.NewTimer(0.2, function() self:PreviewRaidPA(Show, false) end)
        else
            print("Couldn't find a matching raid frame for the player, aborting preview")
            self.IsRaidPAPreview = false
        end
        return
    end
    if not self.PARaidPreviewFrame then
        self.PARaidPreviewFrame = CreateFrame("Frame", nil, self.NSRTFrame)
        self.PARaidPreviewFrame:SetFrameStrata("DIALOG")
    end
    self.PARaidPreviewFrame:SetSize(NSRT.PARaidSettings.Width, NSRT.PARaidSettings.Height)
    self.PARaidPreviewFrame:SetPoint(NSRT.PARaidSettings.Anchor, MyFrame, NSRT.PARaidSettings.relativeTo, NSRT.PARaidSettings.xOffset, NSRT.PARaidSettings.yOffset)
    self.PARaidPreviewFrame:Show()

    if not self.PARaidPreviewIcons then
        self.PARaidPreviewIcons = {}
    end

    for i=1, 10 do
        if not self.PARaidPreviewIcons[i] then
            self.PARaidPreviewIcons[i] = self.PARaidPreviewFrame:CreateTexture(nil, "ARTWORK")
            self.PARaidPreviewIcons[i]:SetTexture(237555)
        end
        if NSRT.PARaidSettings.Limit >= i then
            local xOffset = (NSRT.PARaidSettings.GrowDirection == "RIGHT" and (i-1)*(NSRT.PARaidSettings.Width+NSRT.PARaidSettings.Spacing)) or (NSRT.PARaidSettings.GrowDirection == "LEFT" and -(i-1)*(NSRT.PARaidSettings.Width+NSRT.PARaidSettings.Spacing)) or 0
            local yOffset = (NSRT.PARaidSettings.GrowDirection == "UP" and (i-1)*(NSRT.PARaidSettings.Height+NSRT.PARaidSettings.Spacing)) or (NSRT.PARaidSettings.GrowDirection == "DOWN" and -(i-1)*(NSRT.PARaidSettings.Height+NSRT.PARaidSettings.Spacing)) or 0
            self.PARaidPreviewIcons[i]:SetSize(NSRT.PARaidSettings.Width, NSRT.PARaidSettings.Height)
            self.PARaidPreviewIcons[i]:SetPoint("CENTER", self.PARaidPreviewFrame, "CENTER", xOffset, yOffset)
            self.PARaidPreviewIcons[i]:Show()
        else
            self.PARaidPreviewIcons[i]:Hide()
        end
    end
end