#!/usr/bin/python3
#
# Copyright (C) 2022 Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

from anacondalib import VirtInstallMachineCase, pixel_tests_ignore
from installer import Installer
from password import Password
from review import Review
from storage import BOOT_PARTITION_DEFAULT_SIZE_GB, Storage
from testlib import nondestructive, test_main  # pylint: disable=import-error
from utils import pretend_live_workstation_iso


@nondestructive
class TestReview(VirtInstallMachineCase):

    def testWorkstation(self):
        """
        Description:
            Check the review step for a Live ISO installation

        Expected results:
            - The review step is shown
            - The review step shows the correct information
            - The review step shows the correct information after setting encryption
            - The hostname is shown for Live ISO
            - The timezone is not shown for Live ISO
        """

        b = self.browser
        m = self.machine
        i = Installer(b, m)
        s = Storage(b, m)
        p = Password(b, s.encryption_id_prefix)
        r = Review(b, m)
        pretend_live_workstation_iso(self, i, m)
        i.open()
        # After clicking 'Next' on the storage step, partitioning is done, thus changing the available space on the disk
        # Since this is a non-destructive test we need to make sure the we reset partitioning to how it was before the test started
        i.reach(i.steps.REVIEW)

        # check language is shown
        r.check_language("English (United States)")

        # check selected disks are shown
        r.check_disk("vda", "16.1 GB vda (Virtio Block Device)")
        r.check_disk_row("vda", "/boot", "vda2", BOOT_PARTITION_DEFAULT_SIZE_GB, True, "xfs")
        r.check_disk_row("vda", "/", "vda3, LVM", "13.3 GB", True, "xfs")

        # check storage configuration
        r.check_storage_config("Use entire disk")

        # check hostname is visible for Live ISO with DHCP default
        r.check_hostname("transient, will use DHCP")

        # check timezone is not visible for Live ISO
        r.check_timezone_not_present()

        # Pixel test the review step
        b.assert_pixels(
            "#app",
            "review-step-basic",
            ignore=pixel_tests_ignore,
        )

        i.reach_on_sidebar(i.steps.STORAGE_CONFIGURATION)
        s.set_encryption_selected(True)
        strong_password = "Rwce82ybF7dXtCzFumanchu!!!!!!!!"
        p.set_password(strong_password)
        p.set_password_confirm(strong_password)
        i.reach(i.steps.REVIEW)

        # check selected disks are shown
        r.check_disk("vda", "16.1 GB vda (Virtio Block Device)")
        r.check_disk_row("vda", "/boot/efi", "vda1", "629 MB", True, "efi", is_encrypted=False)
        r.check_disk_row("vda", "/boot", "vda2", BOOT_PARTITION_DEFAULT_SIZE_GB, True, "xfs", is_encrypted=False)
        r.check_disk_row("vda", "/", "vda3, LVM", "13.3 GB", True, "xfs", is_encrypted=True)

        # move the mouse away to avoid highlighting any UI element (pixel ref does not expect that)
        b.mouse("#app", "mouseenter")

        # Pixel test the review step with encrypted disk
        b.assert_pixels(
            "#app",
            "review-step-basic-encrypted",
            ignore=pixel_tests_ignore,
        )

    def testHostnameHandling(self):
        """
        Description:
            Check the hostname handling in the review step

        Expected results:
            - The hostname can be set to empty string
            - The hostname can be set to a custom value
        """

        b = self.browser
        m = self.machine
        i = Installer(b, m)
        r = Review(b, m)

        i.open()

        self.addCleanup(r.dbus_reset_hostname)
        i.reach(i.steps.REVIEW)

        # Save hostname in modal - variant for empty hostname
        b.click("button:contains('edit')")
        b.wait_in_text("#system_information_change_hostname", "Set custom hostname")
        b.set_input_text("#review-handle-hostname-hostname", "Test false", append=False, value_check=False)
        b.set_input_text("#review-handle-hostname-hostname", "test123.first", append=False, value_check=True)
        b.click("button:contains('Save')")
        r.check_hostname("test123.first")

        # Try to set empty hostname - variant for changing hostname
        b.click("button:contains('edit')")
        b.wait_in_text("#system_information_change_hostname", "Change hostname")
        b.set_input_text("#review-handle-hostname-hostname", "", append=False, value_check=True)
        b.click("button:contains('Change')")
        r.check_hostname("")

        hostname = r.dbus_get_hostname()
        self.assertIn('s ""\n', hostname)

        # Change hostname in modal - variant for changing hostname
        b.click("button:contains('edit')")
        b.wait_in_text("#system_information_change_hostname", "Set custom hostname")
        b.set_input_text("#review-handle-hostname-hostname", "test123", append=False, value_check=True)
        b.click("button:contains('Save')")
        r.check_hostname("test123")

        hostname = r.dbus_get_hostname()
        self.assertIn('s "test123"\n', hostname)


if __name__ == '__main__':
    test_main()
