import re
from django.test import tag
from playwright.sync_api import expect

from .frontend_base import EnviPyStaticLiveServerTestCase


class TestPackagePage(EnviPyStaticLiveServerTestCase):
    @tag("frontend")
    def test_create_package(self):
        page = self.login()
        page = self.create_package(page)
        # Check the package name is correct
        expect(page.locator("h2")).to_contain_text("test package")

    @tag("frontend")
    def test_package_permissions(self):
        page = self.login()
        page = self.create_package(page)
        page.get_by_role("button", name="Actions").click()
        page.get_by_role("button", name="Edit Permissions").click()
        # Add read and write permission to enviPath Users group
        search_input = page.locator('input[placeholder="Search users or groups..."]')
        search_input.fill("enviPath")

        # Wait for the results list to appear and be populated
        page.wait_for_selector("#resultsList", state="visible")

        # Click the first button in the results list
        first_button = page.locator("#resultsList button").first
        first_button.click()

        page.locator("#read_new").check()
        page.locator("#write_new").check()
        page.get_by_role("button", name="+", exact=True).click()
        page.get_by_role("button", name="Actions").click()
        page.get_by_role("button", name="Edit Permissions").click()
        # Check the permissions saved when re-opening the permissions box
        expect(page.get_by_text("enviPath Users")).to_be_visible()

    @tag("frontend")
    def test_predict_in_package(self):
        page = self.login()
        page = self.create_package(page)
        pathway_button = page.get_by_role("link", name="Pathways")
        # Find number of current pathways by extracting it from pathway button
        num_pathways = int(re.search(r"Pathways \((\d+)\)", pathway_button.inner_text()).group(1))
        pathway_button.click()
        page.get_by_role("link", name="New Pathway").click()
        # Check that the predict page 'in [package_name]' text shows the current package
        expect(page.get_by_role("strong").get_by_text("test package")).to_be_visible()
        page.get_by_role("textbox", name="Name").click()
        page.get_by_role("textbox", name="Name").fill("Test Pathway")
        page.get_by_role("textbox", name="Description").click()
        page.get_by_role("textbox", name="Description").fill("Test description")
        page.get_by_role("textbox", name="SMILES").click()
        page.get_by_role("textbox", name="SMILES").fill("OCCCN")
        page.locator("#predict-submit-button").click()
        # Check a pathway is visible
        expect(page.locator("rect")).to_be_visible()
        page.get_by_role("link", name="test package").click()
        # Check that the package now has one more pathway than initially
        expect(page.locator("#docContent")).to_contain_text(f"Pathways ({num_pathways + 1})")

    @staticmethod
    def create_package(page):
        """Make a new empty package with name 'test package'"""
        page.get_by_role("button", name="Browse").click()
        page.get_by_role("link", name="Package", exact=True).click()
        page.locator("#new-package-button").click()
        page.get_by_role("textbox", name="Name").click()
        page.get_by_role("textbox", name="Name").fill("test package")
        page.get_by_role("textbox", name="Description").click()
        page.get_by_role("textbox", name="Description").fill("test description")
        page.get_by_role("button", name="Submit").click()
        return page
