-- 3.4.1(47014)
local ADDON_NAME = ...
local SM3_RegisterNihongo
local SetAddonFonts

do
	local function OnEvent(self, event, arg1, ...)
		if event == "ADDON_LOADED" then
			if arg1 == ADDON_NAME then
				if combattext then DAMAGE_TEXT_FONT = "Interface\\AddOns\\" .. ADDON_NAME .. "\\font\\damagetext.ttf" end
			end
			if SetAddonFonts then
				SetAddonFonts(arg1, ...)
			end
		end
	end

	if ActionStatus then
		ActionStatus:RegisterEvent("ADDON_LOADED")
		ActionStatus:HookScript("OnEvent", function(...) OnEvent(...) end)
	end
end

do
	local scale = 1.0
	local path = "Interface\\AddOns\\" .. ADDON_NAME .. "\\font\\"
	local NIHONGO_FONT = path .. "osaka.ttf"
	local NORMAL_TEXT_FONT = path .. "NotoSans\\NotoSans-Regular.ttf"
	local BOLD_TEXT_FONT = path .. "NotoSans\\NotoSans-Bold.ttf"
	local BOLDITALIC_TEXT_FONT = path .. "NotoSans\\NotoSans-BoldItalic.ttf"
	local ITALIC_TEXT_FONT = path .. "NotoSans\\NotoSans-Italic.ttf"
	local NUMBER_TEXT_FONT = path .. "NotoSans\\NotoSans-Bold.ttf"

	local fontName, fontHeight, fontFlags
	local NORMAL, THICK = "OUTLINE", "THICKOUTLINE"

	SM3_RegisterNihongo = function()
		local SM3 = LibStub and LibStub("LibSharedMedia-3.0", true)
		if SM3 then
			SM3:Register("font", "Nihongo", NIHONGO_FONT)
			SM3:Register("font", "Noto Sans", NORMAL_TEXT_FONT)
			SM3:Register("font", "Noto Sans Bold", BOLD_TEXT_FONT)
			SM3:Register("font", "Noto Sans Italic", ITALIC_TEXT_FONT)
			SM3:Register("font", "Noto Sans Bold Italic", BOLDITALIC_TEXT_FONT)
			SM3_RegisterNihongo = nil
		end
	end

	SM3_RegisterNihongo()

	local function SetFont(name, font, height, outline)
		local self = type(name) == "table" and name or _G[name]
		if not self then
			return print(("|cffff8080[%s]:|r |cffffff00%s - %s|r"):format("Nihongo+ "..GetAddOnMetadata(ADDON_NAME, "Version"), "Font not found", name))
		end
		if type(name) == "string" then
			local parent = self:GetFontObject()
			if parent and not parent:GetName() then
				self = parent
			end
		end
		fontName, fontHeight, fontFlags = self:GetFont()
		if type(name) == "string" then
			height = (height or fontHeight) * scale
		end
		self:SetFont(font or fontName, height or fontHeight, outline or fontFlags)
	end

	-- Chat
	SetFont(ChatFontNormal, NIHONGO_FONT, 14)
	do
		SetFont("System_IME", NIHONGO_FONT, nil)
		local chatFrame, font, height
		for i = 1, NUM_CHAT_WINDOWS do
			chatFrame = _G["ChatFrame"..i]
			if chatFrame then
				fontName, fontHeight, fontFlags = chatFrame:GetFontObject():GetFont()
				chatFrame:GetFontObject():SetFont(NIHONGO_FONT, fontHeight, fontFlags)
			end
		end
	end
	SetFont(ChatBubbleFont, NIHONGO_FONT, 11)

	-- Mail
	SetFont(MailFont_Large, NIHONGO_FONT, 14)
	SetFont(OpenMailSubject, NIHONGO_FONT, 14)
	for i = 1, INBOXITEMS_TO_DISPLAY do
		local m = _G["MailItem"..i.."Subject"]
		if m then SetFont(m, NIHONGO_FONT, 12) end
	end

	-- Raid Boss Emote
	--GameFontNormalHuge:SetFont(NORMAL_TEXT_FONT, 20)
	fontName, fontHeight, fontFlags = RaidBossEmoteFrame.slot1:GetFont()
	RaidBossEmoteFrame.slot1:SetFont(NORMAL_TEXT_FONT, fontHeight, fontFlags)
	RaidBossEmoteFrame.slot2:SetFont(NORMAL_TEXT_FONT, fontHeight, fontFlags)
	if scale ~= 1.0 then RaidBossEmoteFrame:SetScale(scale)	end

	-- Raid Warning
	SetFont(RaidWarningFrameSlot1, NIHONGO_FONT)
	SetFont(RaidWarningFrameSlot2, NIHONGO_FONT)

	-- UI Fonts
	STANDARD_TEXT_FONT = NORMAL_TEXT_FONT
	UNIT_NAME_FONT = BOLD_TEXT_FONT
	--NAMEPLATE_FONT = NORMAL_TEXT_FONT
	--NAMEPLATE_SPELLCAST_FONT = NORMAL_TEXT_FONT

	-- System Fonts
	SetFont("AchievementFont_Small", BOLD_TEXT_FONT, nil)
	--SetFont("ChatBubbleFont", NIHONGO_FONT, 11)
	SetFont("FriendsFont_Large", NORMAL_TEXT_FONT, nil)
	SetFont("FriendsFont_Normal", NORMAL_TEXT_FONT, nil)
	SetFont("FriendsFont_Small", BOLD_TEXT_FONT, nil)
	SetFont("FriendsFont_UserText", NORMAL_TEXT_FONT, nil)
	SetFont("Game10Font_o1", NORMAL_TEXT_FONT, nil)
	SetFont("Game11Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game11Font_o1", NORMAL_TEXT_FONT, nil)
	SetFont("Game12Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game12Font_o1", NORMAL_TEXT_FONT, nil)
	SetFont("Game13Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game13FontShadow", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game13Font_o1", NORMAL_TEXT_FONT, nil)
	SetFont("Game15Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game15Font_o1", NORMAL_TEXT_FONT, nil)
	SetFont("Game16Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game18Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game20Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game24Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game27Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game30Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game30Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game30Font2", NORMAL_TEXT_FONT, nil)
	SetFont("Game30Font2Outline", NORMAL_TEXT_FONT, nil, THICK)
	SetFont("Game32Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game36Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game42Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game46Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game48Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game48FontShadow", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game60Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game72Font", NORMAL_TEXT_FONT, nil)
	SetFont("GameFont_Gigantic", BOLD_TEXT_FONT, nil)
	SetFont("GameTooltipHeader", BOLD_TEXT_FONT, 14)
	SetFont("InvoiceFont_Med", ITALIC_TEXT_FONT, nil)
	SetFont("InvoiceFont_Small", ITALIC_TEXT_FONT, nil)
	SetFont("Number12Font_o1", NUMBER_TEXT_FONT, 13, NORMAL)
	SetFont("NumberFont_GameNormal", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Normal_Med", NUMBER_TEXT_FONT, 12.5, nil)
	SetFont("NumberFont_OutlineThick_Mono_Small", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Outline_Huge", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Outline_Large", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Outline_Med", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Shadow_Med", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Shadow_Small", NUMBER_TEXT_FONT, nil, NORMAL)
	-- SetFont("NumberFont_Shadow_Tiny", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Small", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("ReputationDetailFont", BOLD_TEXT_FONT, nil)
	SetFont("SpellFont_Small", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Huge1", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Huge1_Outline", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Huge2", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_InverseShadow_Small", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Large", NORMAL_TEXT_FONT, nil)
	--SetFont("SystemFont_LargeNamePlate", NORMAL_TEXT_FONT, nil)
	--SetFont("SystemFont_LargeNamePlateFixed", NORMAL_TEXT_FONT, 20)
 	SetFont("SystemFont_Med1", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Med2", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Med2_HSpacing", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Med3", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Med4", BOLD_TEXT_FONT, nil)
	--SetFont("SystemFont_NamePlate", NORMAL_TEXT_FONT, nil)
	--SetFont("SystemFont_NamePlateCastBar", NORMAL_TEXT_FONT, 10)
	--SetFont("SystemFont_NamePlateFixed", NORMAL_TEXT_FONT, 15)
	--SetFont("SystemFont_NamePlateLevel", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_OutlineThick_Huge2", BOLD_TEXT_FONT, nil, THICK)
	SetFont("SystemFont_OutlineThick_Huge4", BOLD_TEXT_FONT, nil, THICK)
	SetFont("SystemFont_OutlineThick_WTF", BOLD_TEXT_FONT, nil, THICK)
	SetFont("SystemFont_Outline_Small", BOLD_TEXT_FONT, nil, NORMAL)
	--SetFont("SystemFont_OutlineThick_WTF2", BOLD_TEXT_FONT, nil, THICK)
	SetFont("SystemFont_Outline_WTF2", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Huge1", NORMAL_TEXT_FONT, nil) -- Raid Boss Emote
	SetFont("SystemFont_Shadow_Huge2", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Huge3", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Large", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Large2", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Large_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Med1", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Med1_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Med2", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Med3", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Med4", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Outline_Huge2", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Outline_Huge3", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Small", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Small2", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Small", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Small2", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Tiny", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Tiny2", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_WTF2", BOLD_TEXT_FONT, nil)
	--SetFont("SystemFont_World", BOLD_TEXT_FONT, 64)
	--SetFont("SystemFont_World_ThickOutline", BOLD_TEXT_FONT, 64, THICK)
	--SetFont("System_IME", NORMAL_TEXT_FONT, nil)
	SetFont("Tooltip_Med", NORMAL_TEXT_FONT, 13)
	SetFont("Tooltip_Small", BOLD_TEXT_FONT, nil)

	SetFont("FriendsFont_Normal", NIHONGO_FONT, nil)
	SetFont("FriendsFont_Small", NIHONGO_FONT, nil)
	SetFont("FriendsFont_Large", NIHONGO_FONT, nil)

	-- Styles
	SetFont("QuestFontNormalSmall", BOLD_TEXT_FONT, 11.5) -- Quest Objective
	SetFont("QuestFont", ITALIC_TEXT_FONT, 12.5) -- Quest Description
	--SetFont("GameFontHighlightSmallOutline", BOLD_TEXT_FONT, 11.5) -- Action Button (Macro Name)
	SetFont("NumberFontNormal", NUMBER_TEXT_FONT, 13) -- Action Button (Count), Item (Count)

	function SetAddonFonts(addonName)
		if SM3_RegisterNihongo then SM3_RegisterNihongo() end
		if addonName == "Blizzard_Communities" then
			fontName, fontHeight, fontFlags = GameFontHighlightSmall:GetFont()
			if _G.CommunitiesFrame.MemberList.ListScrollFrame then
				for k, button in pairs(_G.CommunitiesFrame.MemberList.ListScrollFrame.buttons) do
					button.Rank:SetFont(NIHONGO_FONT, fontHeight, fontFlags)
					button.Note:SetFont(NIHONGO_FONT, fontHeight, fontFlags)
				end
			end
			if _G.CommunitiesFrame.GuildMemberDetailFrame then
				fontName, fontHeight, fontFlags = GameFontHighlight:GetFont()
				_G.CommunitiesFrame.GuildMemberDetailFrame.NoteBackground.PersonalNoteText:SetFont(NIHONGO_FONT, fontHeight, fontFlags)
			end

			if _G.CommunitiesFrameGuildDetailsFrameInfo then
				fontName, fontHeight, fontFlags = GameFontNormalSmall:GetFont()
				_G.CommunitiesFrameGuildDetailsFrameInfo.MOTDScrollFrame.MOTD:SetFont("p", NIHONGO_FONT, fontHeight, fontFlags)
				_G.CommunitiesFrameGuildDetailsFrameInfo.DetailsFrame:GetScrollChild().Details:SetFont(NIHONGO_FONT, fontHeight, fontFlags)
			end

			if _G.CommunitiesFrameGuildDetailsFrameNewsContainer then
				fontName, fontHeight, fontFlags = GameFontHighlightSmall:GetFont()
				for k, button in pairs(_G.CommunitiesFrameGuildDetailsFrameNewsContainer.buttons) do
					button.text:SetFont(NIHONGO_FONT, fontHeight, fontFlags)
				end
			end
		end
	end
end

local function SlashCommandHandler(msg)
	local s
	if msg == "combattext" or msg == "damagetext" then
		combattext = not combattext or nil
		s = "変更は次回のログイン時から適用されます．"
	else
		s = "ver "..GetAddOnMetadata(ADDON_NAME, "Version")
	end
	print(("|cffff8080[%s]:|r |cffffff00%s|r"):format("Nihongo+", s))
end

SlashCmdList["nihongo"] = SlashCommandHandler
SLASH_nihongo1 = "/nihongo"
