local FontManager = {
    defaultFontAssets = nil,
    fallbackFontAssetsName = nil,
    fallbackFontAssets = nil,
}

local DEFAULT_FONT_PATH = "assets/arts/fonts/tmpfonts/default/tmpfont/font_sdf.asset"
-- local DEFAULT_FONT_TITLE_PATH = "assets/arts/fonts/tmpfonts/default/tmpfont/font_title_sdf.asset"

local LANGUAGE_FONT_PATH = {
    [GConst.LANGUAGE.CHINESE] = "assets/arts/fonts/tmpfonts/cn/tmpfont/font_sdf.asset",
    [GConst.LANGUAGE.ENGLISH] = "assets/arts/fonts/tmpfonts/en/tmpfont/font_sdf.asset",
    [GConst.LANGUAGE.CHINESE_TC] = "assets/arts/fonts/tmpfonts/zh/tmpfont/font_sdf.asset",
    [GConst.LANGUAGE.RUSSIAN] = "assets/arts/fonts/tmpfonts/ru/tmpfont/font_sdf.asset",
    [GConst.LANGUAGE.THAILAND] = "assets/arts/fonts/tmpfonts/th/tmpfont/font_sdf.asset",
    [GConst.LANGUAGE.INDONESIA] = "assets/arts/fonts/tmpfonts/id/tmpfont/font_sdf.asset",
    [GConst.LANGUAGE.VIETNAMESE] = "assets/arts/fonts/tmpfonts/vi/tmpfont/font_sdf.asset",
}

local LANGUAGE_FONT_TITLE_PATH = {
    [GConst.LANGUAGE.CHINESE] = "assets/arts/fonts/tmpfonts/cn/tmpfont/font_title_sdf.asset",
    [GConst.LANGUAGE.ENGLISH] = "assets/arts/fonts/tmpfonts/en/tmpfont/font_title_sdf.asset",
    [GConst.LANGUAGE.CHINESE_TC] = "assets/arts/fonts/tmpfonts/zh/tmpfont/font_title_sdf.asset",
    [GConst.LANGUAGE.RUSSIAN] = "assets/arts/fonts/tmpfonts/ru/tmpfont/font_title_sdf.asset",
    [GConst.LANGUAGE.THAILAND] = "assets/arts/fonts/tmpfonts/th/tmpfont/font_title_sdf.asset",
    [GConst.LANGUAGE.INDONESIA] = "assets/arts/fonts/tmpfonts/id/tmpfont/font_title_sdf.asset",
    [GConst.LANGUAGE.VIETNAMESE] = "assets/arts/fonts/tmpfonts/vi/tmpfont/font_title_sdf.asset",
}

function FontManager:changeLanguage(language, callback)
    self.fontLoaded = false
    -- if not self.defaultFontAssets or not self.defaultFontTitleAssets then
    if not self.defaultFontAssets then
        local count = 0
        -- local totalCount = 2
        local totalCount = 1
        local function finish()
            count = count + 1
            if count == totalCount then
                self:_changeLanguage(language, callback)
            end
        end

        ResourceManager:loadOriginAssetAsync(DEFAULT_FONT_PATH, GConst.TYPEOF_UNITY_CLASS.TMP_FONT_ASSET, function (path, obj)
            self.defaultFontAssets = obj
            -- --特殊处理下划线
            -- local underlineCharater = self.defaultFontAssets:GetTMPCharacter(0x5F)
            -- self.defaultFontAssets:ClearTMPCharacter()
            -- self.defaultFontAssets:AddTMPCharacter(0x5F, underlineCharater)
            finish()
        end)

        -- ResourceManager:loadOriginAssetAsync(DEFAULT_FONT_TITLE_PATH, GConst.TYPEOF_UNITY_CLASS.TMP_FONT_ASSET, function (path, obj)
        --     self.defaultFontTitleAssets = obj
            -- --特殊处理下划线
            -- local underlineCharater = self.defaultFontTitleAssets:GetTMPCharacter(0x5F)
            -- self.defaultFontTitleAssets:ClearTMPCharacter()
            -- self.defaultFontTitleAssets:AddTMPCharacter(0x5F, underlineCharater)
        --     finish()
        -- end)
    else
        --特殊处理下划线
        -- local underlineCharater = self.defaultFontAssets:GetTMPCharacter(0x5F)
        -- self.defaultFontAssets:ClearTMPCharacter()
        -- self.defaultFontAssets:AddTMPCharacter(0x5F, underlineCharater)

        -- underlineCharater = self.defaultFontTitleAssets:GetTMPCharacter(0x5F)
        -- self.defaultFontTitleAssets:ClearTMPCharacter()
        -- self.defaultFontTitleAssets:AddTMPCharacter(0x5F, underlineCharater)

        self:_changeLanguage(language, callback)
    end
end

function FontManager:_changeLanguage(language, callback)
    -- self.defaultFontAssets.fallbackFontAssetTable:Clear()
    -- self.defaultFontTitleAssets.fallbackFontAssetTable:Clear()

    -- local fontPath = LANGUAGE_FONT_PATH[language]
    -- local fontTitlePath = LANGUAGE_FONT_TITLE_PATH[language]
    -- if fontPath and fontTitlePath then
    -- if fontPath then
    --     local count = 0
    --     local totalCount = 2
    --     local totalCount = 1
    --     local function finish()
    --         count = count + 1
    --         if count == totalCount then
    --             self.fontLoaded = true
    --             if callback then
    --                 callback()
    --             end
    --             if self.onFontLoadedCallback then
    --                 local func = self.onFontLoadedCallback
    --                 self.onFontLoadedCallback = nil
    --                 func()
    --             end
    --         end
    --     end

    --     if self.fallbackFontAssetsName then
    --         ResourceManager:unload(self.fallbackFontAssetsName)
    --     end
    --     ResourceManager:loadOriginAssetAsync(fontPath, GConst.TYPEOF_UNITY_CLASS.TMP_FONT_ASSET, function (path, obj)
    --         self.fallbackFontAssetsName = fontPath
    --         self.fallbackFontAssets = obj
    --         local faceInfo = self.fallbackFontAssets.faceInfo
    --         self.defaultFontAssets.faceInfo = faceInfo
    --         -- 编辑模式下就实例化一下，否则对字体资源的fallback的修改会保存到本地
    --         if EDITOR_MODE then
    --             self.defaultFontAssets.fallbackFontAssetTable:Add(CS.UnityEngine.Object.Instantiate(self.fallbackFontAssets))
    --         else
    --             self.defaultFontAssets.fallbackFontAssetTable:Add(self.fallbackFontAssets)
    --         end
    --         finish()
    --     end)

    --     if self.fallbackFontTitleAssetsName then
    --         ResourceManager:unload(self.fallbackFontTitleAssetsName)
    --     end
    --     ResourceManager:loadOriginAssetAsync(fontTitlePath, GConst.TYPEOF_UNITY_CLASS.TMP_FONT_ASSET, function (path, obj)
    --         self.fallbackFontTitleAssetsName = fontTitlePath
    --         self.fallbackFontTitleAssets = obj
    --         local faceInfo = self.fallbackFontTitleAssets.faceInfo
    --         self.defaultFontTitleAssets.faceInfo = faceInfo
    --         -- 编辑模式下就实例化一下，否则对字体资源的fallback的修改会保存到本地
    --         if EDITOR_MODE then
    --             self.defaultFontTitleAssets.fallbackFontAssetTable:Add(CS.UnityEngine.Object.Instantiate(self.fallbackFontTitleAssets))
    --         else
    --             self.defaultFontTitleAssets.fallbackFontAssetTable:Add(self.fallbackFontTitleAssets)
    --         end
    --         finish()
    --     end)
    -- else
        -- self.fontLoaded = true
        -- if callback then
        --     callback()
        -- end
        -- if self.onFontLoadedCallback then
        --     local func = self.onFontLoadedCallback
        --     self.onFontLoadedCallback = nil
        --     func()
        -- end
        -- Logger.logError("there is not this font path in const")
    -- end

    -- 动态字体图集
    self.fontLoaded = true
    if callback then
        callback()
    end
    if self.onFontLoadedCallback then
        local func = self.onFontLoadedCallback
        self.onFontLoadedCallback = nil
        func()
    end
end

function FontManager:onFontLoaded(callback)
    if self.fontLoaded then
        return callback and callback()
    end
    self.onFontLoadedCallback = callback
end

return FontManager