local LanguageUI = class("LanguageUI", BaseUI)

local LANGUAGE_CELL = "app/ui/game_setting/cell/language_cell"

function LanguageUI:isFullScreen()
	return false
end

function LanguageUI:ctor()
    self.languageList = I18N:getSupportLanguageList()
end

function LanguageUI:getPrefabPath()
	return "assets/prefabs/ui/setting/language_ui.prefab"
end

function LanguageUI:onPressBackspace()
	self:closeUI()
end

function LanguageUI:onLoadRootComplete()
    self.uiMap = self.root:genAllChildren()

    self:_display()
    self:_addListeners()

    self:refreshScrollRect()
end

function LanguageUI:_display()
    local titleTx = self.uiMap["language_ui.bg.title_tx"]
    titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.LANGUAGE_DESC))
end

function LanguageUI:_addListeners()
    local closeBtn = self.uiMap["language_ui.bg.close_btn"]
    closeBtn:addClickListener(function()
		self:closeUI()
	end)
end

function LanguageUI:refreshScrollRect()
    local scroll = self.uiMap["language_ui.bg.scrollrect"]
    self.scrollRect = scroll:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRect:addInitCallback(function()
        return LANGUAGE_CELL
    end)
    self.scrollRect:addRefreshCallback(function(index, cell)
        local language = self.languageList[index]
        cell:refresh(language)
        cell:addClickListener(function()
            self:closeUI()
            local changeStatus = I18N:setLanguage(language)
            if changeStatus then
                local FontManager = require "app/common/font_manager"
                FontManager:changeLanguage(I18N:getCurLanguage() or GConst.LANGUAGE.ENGLISH, function()
                    EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.I18N_CHANGE_LANGUAGE)
					UIManager:closeAllUI()
					UIManager:hideToastAndMessageBox()
					UIManager:clearUIPrefabCache()
					UIManager:refreshOnChangeLanguage()
					ModuleManager.MaincityManager:showMainCityUI()
                end)
            end
        end)
    end)
    self.scrollRect:clearCells()
    self.scrollRect:refillCells(#self.languageList)
end

return LanguageUI