-- 12.1.0(69214)
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
				if _G.CommunitiesFrame then
					SetAddonFonts("Blizzard_Communities")
				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, SLUG, FIXEDHEIGHT = "OUTLINE", "THICKOUTLINE", "SLUG", "FIXEDHEIGHT"

	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, ...)
		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+ "..C_AddOns.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
		local outline = ""
		if (...) then
			outline = table.concat({...}, ",")
		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)
	SetFont(SendMailBodyEditBox, 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
	if RaidBossEmoteFrame then
		--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
	end

	-- Raid Warning
	if RaidWarningFrameSlot1 then
		SetFont(RaidWarningFrameSlot1, NIHONGO_FONT)
		SetFont(RaidWarningFrameSlot2, NIHONGO_FONT)
	end

	-- 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_11", NORMAL_TEXT_FONT, nil)
	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_Shadow", 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_Shadow", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game15Font_o1", NORMAL_TEXT_FONT, nil)
	SetFont("Game16Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game17Font_Shadow", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game18Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game19Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game20Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game21Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game22Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game24Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game27Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game30Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game32Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game32Font_Shadow2", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game36Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game36Font_Shadow2", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game40Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game40Font_Shadow2", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game42Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game46Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game46Font_Shadow2", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game48Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game48FontShadow", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game52Font_Shadow2", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game58Font_Shadow2", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game60Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game69Font_Shadow2", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("Game72Font", NORMAL_TEXT_FONT, nil)
	SetFont("Game72Font_Shadow", NORMAL_TEXT_FONT, nil, NORMAL)
	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("Number11Font", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("Number12Font", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("Number12FontOutline", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("Number12Font_o1", NUMBER_TEXT_FONT, 13, NORMAL)
	SetFont("Number13Font", NORMAL_TEXT_FONT, 12, nil) -- Auction Seller
	SetFont("Number15Font", NUMBER_TEXT_FONT, nil, nil)
	SetFont("Number16Font", NUMBER_TEXT_FONT, nil, nil)
	SetFont("Number18Font", NUMBER_TEXT_FONT, nil, nil)
	SetFont("NumberFont_GameNormal", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Normal_Med", NUMBER_TEXT_FONT, 12.5, nil)
	SetFont("NumberFont_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_Large2", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Outline_Med", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_OutlineThick_Mono_Small", NUMBER_TEXT_FONT, nil, NORMAL)
	SetFont("NumberFont_Shadow_Large", 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("ObjectiveTrackerFont12", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont13", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont14", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont15", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont16", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont17", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont18", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont19", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont20", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont21", NORMAL_TEXT_FONT, nil)
	SetFont("ObjectiveTrackerFont22", NORMAL_TEXT_FONT, nil)
	SetFont("PriceFont", NUMBER_TEXT_FONT, 13, nil)
	SetFont("ReputationDetailFont", BOLD_TEXT_FONT, nil)
	SetFont("SpellFont_Small", BOLD_TEXT_FONT, nil)
	SetFont("System15Font", NORMAL_TEXT_FONT, nil)
	SetFont("System15Font_Shadow", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont16_Shadow_ThickOutline", NUMBER_TEXT_FONT, nil, THICK)
	SetFont("SystemFont18_Shadow_ThickOutline", NUMBER_TEXT_FONT, nil, THICK)
	SetFont("SystemFont22_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont22_Shadow_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont22_Shadow_ThickOutline", NUMBER_TEXT_FONT, nil, THICK)
	SetFont("SystemFont_Huge1", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Huge1_Outline", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Huge1_Outline_Slug", BOLD_TEXT_FONT, nil, NORMAL, SLUG)
	SetFont("SystemFont_Huge2", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Huge4", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_InverseShadow_Small", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Large", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Large2", 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_Med3", BOLD_TEXT_FONT, nil)
	--SetFont("SystemFont_NamePlate", NORMAL_TEXT_FONT, nil)
	--SetFont("SystemFont_NamePlateFixed", NORMAL_TEXT_FONT, 15)
	--SetFont("SystemFont_NamePlateLevel", NORMAL_TEXT_FONT, nil)
	--SetFont("SystemFont_NamePlateCastBar", NORMAL_TEXT_FONT, 10)
	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_OutlineThick_WTF2", BOLD_TEXT_FONT, nil, THICK)
	SetFont("SystemFont_Outline_Med2", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Outline_Slug_Huge2", BOLD_TEXT_FONT, nil, NORMAL, SLUG)
	SetFont("SystemFont_Outline_Slug_Large2", BOLD_TEXT_FONT, nil, NORMAL, SLUG)
	SetFont("SystemFont_Outline_Small", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Outline_WTF2", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Huge1", NORMAL_TEXT_FONT, nil) -- Raid Boss Emote
	SetFont("SystemFont_Shadow_Huge1_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Huge2", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Huge2_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Huge3", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Huge4", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Huge4_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Large", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Large2", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Large2_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Large_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Med1_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Med1", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Med2", NORMAL_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Med2_Outline", NORMAL_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Med3", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Med3_Outline", BOLD_TEXT_FONT, nil, NORMAL)
	--SetFont("SystemFont_Shadow_Outline_Huge2", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Outline_Huge3", BOLD_TEXT_FONT, nil, NORMAL)
	--SetFont("SystemFont_Shadow_Outline_Huge4", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Outline_Large", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Outline_Small", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Small", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Small2", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Shadow_Small2_Outline", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Shadow_Small_Outline", BOLD_TEXT_FONT, nil, NORMAL)
	SetFont("SystemFont_Small", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Small2", BOLD_TEXT_FONT, nil)
	SetFont("SystemFont_Tiny", BOLD_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("UserScaledFontSystem_Body", NORMAL_TEXT_FONT, nil)
	SetFont("UserScaledFontSystem_Header", NORMAL_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, NORMAL) -- Action Button (Count), Item (Count)

	-- Chat Tab
	if FCF_SetTemporaryWindowType then
		hooksecurefunc("FCF_SetTemporaryWindowType", function(chatFrame, chatType, chatTarget)
			local id = chatFrame and chatFrame:GetID()
			if id then
				local tab = _G["ChatFrame"..id.."Tab"]
				SetFont(tab.Text, NIHONGO_FONT)
			end
		end)
	end

	-- Floating Chat Window
	if FCF_CreateMinimizedFrame then
		hooksecurefunc("FCF_CreateMinimizedFrame", function(chatFrame)
			local id = chatFrame and chatFrame:GetID()
			if id then
				local minFrame = _G["ChatFrame"..id.."Minimized"]
				if minFrame and minFrame.Text then
					SetFont(minFrame.Text, NIHONGO_FONT)
				end
			end
		end)
	end

	function SetAddonFonts(addonName)
		if SM3_RegisterNihongo then SM3_RegisterNihongo() end
		if addonName == "Blizzard_Communities" then
			if _G.CommunitiesFrame.Chat.MessageFrame then
				SetFont(_G.CommunitiesFrame.Chat.MessageFrame:GetFontObject(), NIHONGO_FONT, 12)
			end
			if _G.CommunitiesFrame.MemberList and _G.CommunitiesFrame.MemberList.ScrollBox then
				local buttons = {}
				_G.CommunitiesFrame.MemberList.ScrollBox:RegisterCallback(ScrollBoxListViewMixin.Event.OnInitializedFrame, function(_, button, elementData)
					if not buttons[button] then
						buttons[button] = true
						fontName, fontHeight, fontFlags = button.Rank:GetFont()
						button.Rank:SetFont(NIHONGO_FONT, fontHeight, fontFlags)
						fontName, fontHeight, fontFlags = button.Note:GetFont()
						button.Note:SetFont(NIHONGO_FONT, fontHeight, fontFlags)
					end
				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.CommunitiesFrameGuildDetailsFrameNews then
				local owner = {view = _G.CommunitiesFrameGuildDetailsFrameNews.ScrollBox.view}
				owner.view:RegisterCallback("OnAcquiredFrame", function(owner, button, data, isNew)
					if isNew and button.text then
						if #owner.view.frames == 1 then -- only MoTD
							button.text:SetFont(NIHONGO_FONT, 12, "")
							owner.view:UnregisterCallback("OnAcquiredFrame", owner)
							wipe(owner)
						end
					end
				end, owner)
			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 "..C_AddOns.GetAddOnMetadata(ADDON_NAME, "Version")
	end
	print(("|cffff8080[%s]:|r |cffffff00%s|r"):format("Nihongo+", s))
end

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