-- Based on global string version 2.0.12
--<< NEW VERSION FOR STANDALONE >>--
--<< VERSION : 3.0 >>--
local _G = getfenv(0)

if FixGlobalStrings then return end

-- Create our event registering frame
local FixGlobalStrings = CreateFrame("Frame", "FixGlobalStrings")

-- Local valiables
-- no need FixGlobalStrings.rarestFixPattens = {}
FixGlobalStrings.forceFixPatterns = {}
FixGlobalStrings.loaded = false

-- Initialize Function
function FixGlobalStrings.Initialization()
	if GetLocale() == "koKR" then
		-- wrong patterns
		FixGlobalStrings.forceFixPatterns = {
			COMBATLOG_HONORGAIN = "%s|1이;가; 죽었습니다. 계급: %s (예상 명예 점수: %d)", 						-- "%1$s|1이;가; 죽었습니다. 계급: %s (예상 명예 점수: %d)";
			LOOT_ITEM_CREATED_SELF = "당신은 %s|1을;를; 만들었습니다.", 										-- "%s|1을;를; 만들었습니다.";
			PERIODICAURAHEALSELFSELF = "당신은 자신의 %2$s|1으로;로; 인해 생명력이 %1$d만큼 회복되었습니다.", 	-- "당신은 %2$s|1으로;로; 인해 생명력이 %1$d만큼 회복되었습니다.";
			POWERGAINSELFSELF = "당신은 자신의 %3$s|1으로;로; 인해 %1$d의 %2$s|1을;를; 얻었습니다.", 			-- "당신은 %3$s|1으로;로; 인해 %1$d의 %2$s|1을;를; 얻었습니다.";
		}
	elseif GetLocale() == "enUS" then
	end
end

-- Add a function to fix some global strings.
function FixGlobalStrings.SetGlobalString()
	local fixedString
	
	-- fix all wrong patten only
	if FixGlobalStrings.forceFixPatterns then
		local patternTable = FixGlobalStrings.forceFixPatterns
		for pattern, fixedString in pairs(patternTable) do
			local str = _G[pattern]
			if str and type(str) == 'string' then
				if not str:find(fixedString, 1, true) then
					setglobal(pattern, fixedString) -- force update
				end
			end
		end
	end
end

-- Get fixed string function
function FixGlobalStrings.GetFixedString(pattern)
	local fixedString
	
	if pattern then
		if FixGlobalStrings.forceFixPatterns[pattern] then	-- fix worng patten
			fixedString = FixGlobalStrings.forceFixPatterns[pattern]
		end
	
		return fixedString -- string.gsub(str, "(%%%d?$?s)(의)", "%1% %2") -- no need 2.0.3
	end
end

-- Event handlers
function FixGlobalStrings.OnEvent()
	if event == "ADDON_LOADED" then
		if arg1 == "!!FixGlobalString" then
			if FixGlobalStrings.loaded then
				return
			end
			
			FixGlobalStrings.Initialization()
			FixGlobalStrings.SetGlobalString()
			FixGlobalStrings.loaded = true
			FixGlobalStrings.Print("Fixed GlobalStrings!")
		end
	end
end

function FixGlobalStrings.Print(text)
	if type(DEFAULT_CHAT_FRAME) == "table" and type(DEFAULT_CHAT_FRAME.AddMessage) == "function" then
		DEFAULT_CHAT_FRAME:AddMessage(text, 0, 1, 0)
	end
end

-- Now register for our needed events
FixGlobalStrings:RegisterEvent("ADDON_LOADED")
FixGlobalStrings:SetScript("OnEvent", FixGlobalStrings.OnEvent)

--<< GLOBAL FUNCTION FOR OLD VERSION >>--
function FixGlobalString()
	if FixGlobalStrings.loaded then
		FixGlobalStrings.Print("Deteced FixGlobalStrings!!")
	else
		FixGlobalStrings.Print("Failed FixGlobalStrings!!")
	end
end