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

from anacondalib import INSTALLER_VM_MEMORY_MB, VirtInstallMachineCase, disk_images, run_boot
from installer import Installer
from operating_systems import DualBootHelper_E2E
from review import Review
from storage import Storage
from testlib import test_main, timeout  # pylint: disable=import-error


class TestStorageUseFreeSpace_E2E(DualBootHelper_E2E, VirtInstallMachineCase):
    provision = {"machine1": {"memory_mb": INSTALLER_VM_MEMORY_MB}}

    @disk_images([("debian-testing", 20)])
    @run_boot("bios", "efi")
    @timeout(1200)
    def testScenarioUseFreeSpace(self):
        """
        Description:
            Test dual boot installation with use free space scenario with debian

        Expected results:
            - The installer should be able to install the system using free space
            - A warning is shown when partition sizes are below recommended minimums
            - The warning can be bypassed by clicking Next again
        """
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        r = Review(b, m)
        s = Storage(b, m)

        i.open()
        i.reach(i.steps.INSTALLATION_METHOD)
        s.check_scenario_selected("use-free-space")
        # Click Next to reach storage configuration
        i.next()
        s.check_encryption_selected(False)

        if self.is_efi:
            # Click Next to apply storage and trigger validation
            i.next(should_fail=True)
            # Wait for warning notification about /boot/efi partition size
            b.wait_in_text(f"#{i.steps.STORAGE_CONFIGURATION}-step-notification",
                           "Your /boot/efi partition is less than 500 MiB which is lower than recommended for a normal Fedora install.")
        i.reach(i.steps.REVIEW)
        r.check_checkbox_not_present()

        self.install(needs_confirmation=False)
        root_one_size_bytes = m.execute("blockdev --getsize64 /dev/vda3")
        root_one_size = round(int(root_one_size_bytes) / 1024 / 1024 / 1024, 1)
        if int(root_one_size) == float(root_one_size):
            root_one_size = int(root_one_size)

        boot_one_size_bytes = m.execute("blockdev --getsize64 /dev/vda2")
        boot_one_size = round(int(boot_one_size_bytes) / 1024 / 1024 / 1024, 1)

        root_two_size = round(20 - root_one_size - boot_one_size - 0.1, 1)

        self.verifyDualBootDebian(root_one_size=root_one_size, root_two_size=root_two_size)


if __name__ == '__main__':
    test_main()
