local E, L, V, P, G = unpack(ElvUI);
local SLE = E:GetModule('SLE')
local BG = E:GetModule('SLE_BackGrounds')
local DTP = E:GetModule('SLE_DTPanels')
local CH = E:GetModule("Chat")
local UB = E:GetModule('SLE_UIButtons')
local RM = E:GetModule('SLE_RaidMarks')
local RF = E:GetModule('SLE_RaidFlares')
local F = E:GetModule('SLE_Farm')
local LT = E:GetModule('SLE_Loot')
local UF = E:GetModule('UnitFrames')
local M = E:GetModule('SLE_Media')
local I = E:GetModule('SLE_InstDif')
local S = E:GetModule("SLE_ScreenSaver")
local dev = ""
local lib = LibStub("LibRealmInfo")
local LocTable = {}
LocTable[1], LocTable[2], LocTable[3], LocTable[4], LocTable[5], LocTable[6], LocTable[7] = GetAvailableLocales()

local GetContainerNumSlots, GetContainerItemID = GetContainerNumSlots, GetContainerItemID

function SLE:BagSearch(itemId)
	for container = 0, NUM_BAG_SLOTS do
		for slot = 1, GetContainerNumSlots(container) do
			if itemId == GetContainerItemID(container, slot) then
				return container, slot
			end
		end
	end
end

function SLE:ValueTable(table, item)
	for i, _ in pairs(table) do
		if i == item then return true end
	end

	return false
end

function SLE:SimpleTable(table, item)
	for i = 1, #table do
		if table[i] == item then  
			return true 
		end
	end

	return false
end

function SLE:Print(msg)
	print(E["media"].hexvaluecolor..'S&L:|r', msg)
end

function SLE:Scale(f)
		return f*GetCVar('uiScale')
end

local function UpdateAll()
	BG:UpdateFrames()
	BG:RegisterHide()
	DTP:Update()
	DTP:DashboardShow()
	DTP:DashWidth()
	if E.private.unitframe.enable then
		UF:Update_CombatIndicator()
	end
	LT:LootShow()
	LT:Update()
	UB:FrameSize()
	RM:Update()
	RF:Update()
	F:UpdateLayout()
	CH:GMIconUpdate()
	M:TextWidth()
	I:UpdateFrame()
	S:Reg(true)

	collectgarbage('collect');
end

function SLE:OpenExport()
	E:ToggleConfig()
	if not SLEExImFrame then SLE:CreateExport() end
	if not SLEExImFrame:IsShown() then 
		SLEExportEditBox:SetText(L["SLE_Exp_Desc"])
		SLEImportEditBox:SetText(L["SLE_Imp_Desc"])
		SLEExImFrame:Show() 
	end
end

local datable = {}

function SLE:ExportPrint()
	local text = ''
	for i = 1, #datable do
		text = text..datable[i]
	end
	
	return text
end

function SLE:SettingTable(t, s, root)
	if not root then root = "" end
	for k, v in pairs(t) do
		if type(v) == "string" then
			if root[k] ~= v or E.global.sle.export.full then
				tinsert(datable, #(datable)+1, s.."."..k..' = "'..v..'"\n')
			end
		elseif type(v) == "number" then
			if root[k] ~= v or E.global.sle.export.full then
				tinsert(datable, #(datable)+1, s.."."..k.." = "..v.."\n")
			else
			end
		elseif type(v) == "boolean" then
			if root[k] ~= v or E.global.sle.export.full then
				local b = v == true and "true" or "false"
				tinsert(datable, #(datable)+1, s.."."..k.." = "..b.."\n")	
			end
		else
			local new = "."..k
			SLE:SettingTable(t[k], s..new, root[k])
		end
	end
end

function SLE:ImportTableReplace(msg)
	if not string.find(msg, "E.db") and not string.find(msg, "E.private") and not string.find(msg, "E.global") then
		return nil
	end
	if string.find(msg, "E.db") then
		msg = gsub(msg, "E.db", "ElvUI[1].db")
	end
	if string.find(msg, "E.private") then
		msg = gsub(msg, "E.private", "ElvUI[1].private")
	end
	if string.find(msg, "E.global") then
		msg = gsub(msg, "E.global", "ElvUI[1].global")
	end

	return msg
end

function SLE:Exporting()
	local msg = ''
	datable = {}
	if E.global.sle.export.profile then
		tinsert(datable, #(datable)+1, "--Profile--\n")
		SLE:SettingTable(E.db, "E.db", P)
	end
	if E.global.sle.export.private then
		tinsert(datable, #(datable)+1, "--Character--\n")
		SLE:SettingTable(E.private, "E.private", V)
	end
	if E.global.sle.export.global then
		tinsert(datable, #(datable)+1, "--Global--\n")
		SLE:SettingTable(E.global, "E.global", G)
	end
	msg = SLE:ExportPrint()
	local editbox = SLEExportEditBox
	editbox:SetText(msg)
	editbox:SetFocus()
	editbox:HighlightText()
end
hooksecurefunc(E, "UpdateAll", UpdateAll)

function SLE:GetRegion()
	-- local pid = BNGetInfo()
	-- local rid = select(5, BNGetToonInfo(pid))
	-- local region = select(6, lib:GetRealmInfo(rid))
	local rid = GetCurrentRegion()
	local region = {
		[1] = "US",
		[2] = "KR",
		[3] = "EU",
		[4] = "TW",
		[5] = "CN",
	}
	SLE.region = region[rid]
	if not SLE.region then SLE.region = format("An error happened. Your region is unknown. Realm: %s. Please report your realm name and the region you are playing in to |cff1784d1Shadow & Light|r authors.", E.myrealm)  end
	-- if not SLE.region then SLE.region = format("An error happened while processing your realm. Please report error id, realm and the region you are playing to |cff1784d1Shadow & Light|r authors! Error id: %s. Realm: %s", rid, E.myrealm) end
	if dev == "" then dev = SLE.Dev[SLE.region] end
	if not dev then
		SLE:Print(SLE.region)
		SLE.region = ""
	end
end


function SLE:Auth(sender)
	local senderName, senderRealm
	if sender then
		senderName, senderRealm = string.split('-', sender)
	else
		senderName = E.myname
	end
	
	senderRealm = senderRealm or E.myrealm
	senderRealm = senderRealm:gsub(' ', '')

	if dev and dev[senderRealm] and dev[senderRealm][senderName] then
		return dev[senderRealm][senderName]
	end

	return false
end