local CdkeyUI = class("CdkeyUI", BaseUI)

function CdkeyUI:getPrefabPath()
	return "assets/prefabs/ui/setting/cdkey_ui.prefab"
end

function CdkeyUI:isFullScreen()
	return false
end

function CdkeyUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["cdkey_ui.title_bg_img.title_text"]:setText(I18N:getGlobalText(I18N.GlobalConst.CDKEY_NAME))

	uiMap["cdkey_ui.title_bg_img.close_btn"]:addClickListener(function()
		self:closeUI()
	end)
	uiMap["cdkey_ui.title_bg_img.cancel_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_CANCEL))
	uiMap["cdkey_ui.title_bg_img.cancel_btn"]:addClickListener(function()
		self:closeUI()
	end)

	uiMap["cdkey_ui.title_bg_img.confirm_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK))
	uiMap["cdkey_ui.title_bg_img.confirm_btn"]:addClickListener(function()
		local text = self.inputField:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TMP_INPUT_FIELD).text
		if not text or text == "" then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.CDKEY_DESC_1))
			return
		end
		ModuleManager.GameSettingManager:exchangeCdkey(text)
	end)

	uiMap["cdkey_ui.title_bg_img.desc_1"]:setText(I18N:getGlobalText(I18N.GlobalConst.CDKEY_DESC_2))
	self.desc2 = uiMap["cdkey_ui.title_bg_img.desc_2"]
	self.desc2:setText(GConst.EMPTY_STRING)

	local placeholder = uiMap["cdkey_ui.title_bg_img.input_field.text_area.placeholder"]
	placeholder:setText(I18N:getGlobalText(I18N.GlobalConst.CDKEY_DESC_1))

	self.inputField = uiMap["cdkey_ui.title_bg_img.input_field"]
	self.inputField:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TMP_INPUT_FIELD).text = ""

	self:addEventListener(EventManager.CUSTOM_EVENT.CDKEY_FINISH, function(code)
		if not code or code == 0 then
			self.inputField:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TMP_INPUT_FIELD).text = ""
		end
		self:updateErrorCode(code or 0)
	end)
end

function CdkeyUI:updateErrorCode(errorCode)
	local str = GConst.EMPTY_STRING
	if errorCode == 1700 then
		str = I18N:getGlobalText(I18N.GlobalConst.CDKEY_ERROR_MSG_1)
	elseif errorCode == 1701 then
		str = I18N:getGlobalText(I18N.GlobalConst.CDKEY_ERROR_MSG_2)
	elseif errorCode == 1702 then
		str = I18N:getGlobalText(I18N.GlobalConst.CDKEY_ERROR_MSG_3)
	elseif errorCode == 1703 then
		str = I18N:getGlobalText(I18N.GlobalConst.CDKEY_ERROR_MSG_4)
	end
	self.desc2:setText(str)
end

return CdkeyUI