--[[
##############################################################################
S V U I   By: Munglunch
############################################################################## ]]-- 
--[[ GLOBALS ]]--
local _G = _G;
local unpack            = _G.unpack;
local select            = _G.select;
local assert            = _G.assert;
local type              = _G.type;
local error             = _G.error;
local pcall             = _G.pcall;
local print             = _G.print;
local ipairs            = _G.ipairs;
local pairs             = _G.pairs;
local next              = _G.next;
local rawset            = _G.rawset;
local rawget            = _G.rawget;
local tostring          = _G.tostring;
local tonumber          = _G.tonumber;
local getmetatable      = _G.getmetatable;
local setmetatable      = _G.setmetatable;
local collectgarbage    = _G.collectgarbage;
local tinsert   = _G.tinsert;
local string    = _G.string;
local math      = _G.math;
local table     = _G.table;
local wipe      = _G.wipe;
--[[ STRING METHODS ]]--
local format, find, lower, match, gsub = string.format, string.find, string.lower, string.match, string.gsub;
--[[ MATH METHODS ]]--
local floor, abs, min, max = math.floor, math.abs, math.min, math.max;
--[[ TABLE METHODS ]]--
local tremove, tcopy, twipe, tsort, tconcat = table.remove, table.copy, table.wipe, table.sort, table.concat;
local tprint = table.tostring;
--[[ 
########################################################## 
GET ADDON DATA
##########################################################
]]--
local SV = _G["SVUI"];
local L = SV.L;
local SVUILib = Librarian("Registry");

SV.ProfileInterface = _G["SVUI_ProfileInterface"];
local ProfileInterfaceDialog = _G["SVUI_ProfileInterfaceDialog"];
local ProfileInterfaceScrollBar = _G["SVUI_ProfileInterfaceDialogScrollBar"];
--[[ 
########################################################## 
CUSTOM MESSAGE WINDOW
##########################################################
]]--
local ProfileInterface_OnTextChanged = function(self, userInput)
    if userInput then return end 
    local _, max = ProfileInterfaceScrollBar:GetMinMaxValues()
    for i = 1, max do
      ScrollFrameTemplate_OnMouseWheel(ProfileInterfaceDialog, -1)
    end
end

function SV.ProfileInterface:Toggle()
    if not self:IsShown() then
        self:Show()
    else
        self:Hide()
    end
end

function SV:LinkProfile(key)
    self.SystemAlert["COPY_PROFILE_PROMPT"].text = "Are you sure you want to link to the profile '" .. key .. "'?"
    self.SystemAlert["COPY_PROFILE_PROMPT"].OnAccept = function() SVUILib:CopyDatabase(key, true) end
    self:StaticPopup_Show("COPY_PROFILE_PROMPT")
end

function SV:CopyProfile(key)
    self.SystemAlert["COPY_PROFILE_PROMPT"].text = "Are you sure you want to copy the profile '" .. key .. "'?"
    self.SystemAlert["COPY_PROFILE_PROMPT"].OnAccept = function() SVUILib:CopyDatabase(key) end
    self:StaticPopup_Show("COPY_PROFILE_PROMPT")
end

function SV:ExportProfile()
    local t = SVUILib:ExportDatabase()
    ProfileInterfaceDialog.Input:SetText(t)
end

function SV:ImportProfile()
    self.SystemAlert["IMPORT_PROFILE_PROMPT"].OnAccept = function()
      local input = ProfileInterfaceDialog.Input:GetText()
      SVUILib:ImportDatabase(input) 
    end
end

local function InitializeProfileInterface()
    SV.ProfileInterface:SetParent(SV.Screen)
    SV.ProfileInterface.Source = "";
    SV.ProfileInterface:SetStyle("!_Frame", "Transparent")
    SV.ProfileInterface.Export:SetStyle("Button")
    SV.ProfileInterface.Import:SetStyle("Button")
    ProfileInterfaceDialog:SetStyle("!_Frame", "Transparent")
    ProfileInterfaceDialog.Input:SetScript("OnTextChanged", ProfileInterface_OnTextChanged)
    SV.ProfileInterface:RegisterForDrag("LeftButton");
end

SV.Events:On("LOAD_ALL_ESSENTIALS", InitializeProfileInterface);