#!/usr/bin/python3

# checkpoint:
# 1. Selected components in edit blueprint page
# 2. Package detail and add, remove, apply package from detail page

import composerlib
import testlib


@testlib.nondestructive
class TestDependence(composerlib.ComposerCase):

    def testComponent(self):
        b = self.browser

        self.login_and_go("/composer", superuser=True)
        b.wait_visible("#main")

        # go to edit package page
        b.click("li[data-blueprint=openssh-server] a:contains('Edit packages')")
        with b.wait_timeout(120):
            b.wait_visible("ul[aria-label='Available components'] li:nth-child(1)")

        # open component detail
        b.click("#openssh-server-toggle")
        b.wait_attr("#openssh-server-toggle", "aria-expanded", "true")
        # Disable this part of test due to issue
        # https://github.com/osbuild/cockpit-composer/issues/942
        # # depsolving dependencies needs more time
        # with b.wait_timeout(480):
        #     b.wait_visible(".cc-component-summary__deps")
        # # show all
        # b.click("button:contains('Show all')")
        # # show less
        # b.click("button:contains('Show less')")
        # close detail
        b.click("#openssh-server-toggle")
        b.wait_not_visible("#openssh-server-details")

        # dependencies tab
        b.click("#blueprint-tabs-tab-dependencies")
        b.wait_visible("#blueprint-tabs-pane-dependencies")

        # collect code coverage result
        self.check_coverage()

    def testDetails(self):
        b = self.browser

        self.login_and_go("/composer", superuser=True)
        b.wait_visible("#main")

        # go to edit package page
        b.click("li[data-blueprint=openssh-server] a:contains('Edit packages')")
        with b.wait_timeout(120):
            b.wait_visible("ul[aria-label='Available components'] li:nth-child(1)")

        # view openssh-server package detail
        b.click("#openssh-server-kebab")
        b.wait_attr("#openssh-server-kebab", "aria-expanded", "true")
        b.click("ul[aria-labelledby=openssh-server-kebab] a:contains('View')")
        # version selection
        b.set_val("#cmpsr-compon__version-select", "1")
        b.wait_val("#cmpsr-compon__version-select", "1")
        # group actions end
        b.click("button:contains('Apply change')")
        b.wait_visible("a:contains('1 Pending Change')")

        # search for openssh-server pacakge
        b.set_input_text("#cmpsr-blueprint-input-filter", "tmux", blur=False)
        b.key_press("\r")
        with b.wait_timeout(120):
            b.click("#tmux-input")
        b.wait_visible("#cmpsr-compon__version-select")
        b.click("#blueprint-tabs-tab-dependencies")
        # add tmux to blueprint
        b.click(".cmpsr-header__actions button:contains('Add')")
        b.wait_visible("a:contains('2 Pending Change')")
        b.wait_visible("li[data-component=tmux]")

        # view tmux detail
        b.click("li[data-component=tmux] #tmux")
        b.wait_visible("#cmpsr-compon__version-select")
        # back to openssh-server
        b.click("a:contains('Back to openssh-server')")
        b.wait_visible("li[data-component=tmux]")

        # remove from tumx detail
        b.click("li[data-component=tmux] #tmux")
        b.wait_visible("#cmpsr-compon__version-select")
        b.click("button:contains('Remove')")
        b.wait_visible("a:contains('1 Pending Change')")

        # collect code coverage result
        self.check_coverage()


if __name__ == '__main__':
    testlib.test_main()
