#!/usr/bin/python3
#
# Copyright (C) 2021 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.

import os
import sys

# import Cockpit's machinery for test VMs and its browser test API
TEST_DIR = os.path.dirname(__file__)
sys.path.append(os.path.join(TEST_DIR, "common"))
sys.path.append(os.path.join(TEST_DIR, "helpers"))
sys.path.append(os.path.join(os.path.dirname(TEST_DIR), "bots/machine"))

from installer import Installer
from language import Language
from testlib import MachineCase, nondestructive, test_main  # pylint: disable=import-error
from machine_install import VirtInstallMachine

@nondestructive
class TestLanguage(MachineCase):
    MachineCase.machine_class = VirtInstallMachine

    def testOtherDefault(self):
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        l = Language(b, m)

        # Set the language in the localization module to German before opening the page
        l.dbus_set_language("de_DE.UTF-8")

        i.open()
        self.addCleanup(l.dbus_set_language, "en_US.UTF-8")
        self.addCleanup(l.select_locale, "en_US")

        # Expect the backend set language to be preselected and the WebUI translated
        l.check_selected_locale("Deutsch (Deutschland)")
        b.wait_in_text("h1", "Anaconda installer")

    def testLanguageSwitching(self):
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        l = Language(b, m)

        self.addCleanup(l.select_locale, "en_US")

        i.open()

        # Expect the default language - this is en at this point - adjust the test when better guess is implemented
        b.wait_in_text("h1", "Anaconda installer")

        l.check_selected_locale("English (United States)")

        l.clear_language_selector()
        b.wait_visible(f".{i.steps.WELCOME}-menu.pf-m-invalid")

        # Check that the language menu shows all menu entries when clicking the toggle button
        l.open_locale_options()
        l.locale_option_visible('en_US')
        l.locale_option_visible('de_DE')
        l.locale_option_visible('cs_CZ')

        # Check filtering on English names
        l.input_locale_select('cze')
        l.locale_option_visible('en_US', False)
        l.locale_option_visible('de_DE', False)
        l.locale_option_visible('cs_CZ')

        # Check filtering on native names
        l.input_locale_select('Deutsch')
        l.locale_option_visible('en_US', False)
        l.locale_option_visible('de_DE')
        l.locale_option_visible('cs_CZ', False)
        l.clear_language_selector()

        # Select the 'German' language
        l.select_locale("de_DE")
        l.check_selected_locale("Deutsch (Deutschland)")

        # Pixel test the language step
        b.assert_pixels("#app", "language-step-basic", ignore=["#betanag-icon"])

        # TODO: Add checks for translations when these are present
        b.wait_in_text("h1", "Anaconda installer")
        # TODO: Add checks for plural translations when these are present

        # Check that the language is updated in the backend
        # TODO: Check that error messages from the backend show up translated
        language_new = l.dbus_get_language()
        self.assertIn('string "de_DE.UTF-8"', language_new)

if __name__ == '__main__':
    test_main()
