"""
End to end tests for HTML Components
"""
from uuid import uuid4

from bok_choy.web_app_test import WebAppTest

from regression.pages.studio.course_outline_page import (
    CourseOutlinePageExtended
)
from regression.pages.studio.unit_page import UnitPageExtended
from regression.pages.studio.studio_home import DashboardPageExtended
from regression.pages.lms.utils import get_course_key
from regression.pages.lms.lms_courseware import CoursewarePageExtended
from regression.tests.helpers.utils import (
    get_course_info, get_data_id_of_component, get_data_locator
)

from regression.tests.helpers.api_clients import (
    StudioLoginApi,
    LmsLoginApi
)


class StudioLmsComponentBaseTest(WebAppTest):
    """
    Base class for component tests
    """
    def setUp(self):
        """
        Common setup for component tests
        """
        super(StudioLmsComponentBaseTest, self).setUp()

        studio_login = StudioLoginApi()
        studio_login.authenticate(self.browser)

        lms_login = LmsLoginApi()
        lms_login.authenticate(self.browser)

        self.unit_container_page = UnitPageExtended(
            self.browser, None
        )

        self.studio_home_page = DashboardPageExtended(self.browser)

        self.course_info = get_course_info()

        self.studio_course_outline = CourseOutlinePageExtended(
            self.browser, self.course_info['org'], self.course_info['number'],
            self.course_info['run'])

        self.lms_courseware = CoursewarePageExtended(
            self.browser,
            get_course_key(self.course_info)
        )
        self.studio_course_outline.visit()
        # Delete any existing sections
        self.studio_course_outline.delete_all_sections()


class StudioLmsAdvancedComponentTest(StudioLmsComponentBaseTest):
    """
    Advanced Components tests that require lms verification with studio
    """
    def setUp(self):
        """
        Call setUp in parent
        """
        super(StudioLmsAdvancedComponentTest, self).setUp()

    def test_word_cloud_advanced_component(self):
        """
        Verifies that user can add Word Cloud component on Studio and LMS
        """
        section_name = 'Section :{}'.format(uuid4().hex)
        self.studio_course_outline.add_section_with_name(section_name)
        self.assertIn(
            section_name,
            self.studio_course_outline.q(
                css='.incontext-editor-value'
            ).text
        )

        subsection_name = 'Subsection :{}'.format(uuid4().hex)
        self.studio_course_outline.add_subsection_with_name(
            subsection_name
        )
        self.assertIn(
            subsection_name,
            self.studio_course_outline.q(
                css='.incontext-editor-value'
            ).text
        )

        self.studio_course_outline.click_add_unit_button()
        self.unit_container_page.wait_for_page()

        self.unit_container_page.add_word_cloud_component(True)
        word_cloud_data_locator = get_data_locator(
            self.unit_container_page
        )

        # View Live
        self.unit_container_page.view_live_version()
        self.lms_courseware.wait_for_page()
        self.assertEqual(
            word_cloud_data_locator,
            get_data_locator(self.lms_courseware)
        )
        # Remove the added section
        self.studio_course_outline.visit()
        self.studio_course_outline.delete_section()

    def test_custom_js_display_advanced_component(self):
        """
        Verifies that user can add Custom JavaScript Display and Grading
        component on Studio and LMS
        """
        section_name = 'Section :{}'.format(uuid4().hex)
        self.studio_course_outline.add_section_with_name(section_name)
        self.assertIn(
            section_name,
            self.studio_course_outline.q(
                css='.incontext-editor-value').text
        )

        subsection_name = 'Subsection :{}'.format(uuid4().hex)
        self.studio_course_outline.add_subsection_with_name(
            subsection_name
        )
        self.assertIn(
            subsection_name,
            self.studio_course_outline.q(
                css='.incontext-editor-value').text
        )

        self.studio_course_outline.click_add_unit_button()
        self.unit_container_page.wait_for_page()

        self.assertEqual(
            self.unit_container_page.add_custom_js_display_and_grading(),
            'Custom JavaScript Display and Grading'
        )

        studio_custom_js = get_data_id_of_component(
            self.unit_container_page
        )

        # Publish Unit
        self.studio_course_outline.publish()
        # View Live
        self.unit_container_page.view_live_version()
        self.assertEqual(
            studio_custom_js,
            get_data_id_of_component(self.lms_courseware)
        )
        # Remove the added section
        self.studio_course_outline.visit()
        self.studio_course_outline.delete_section()


class StudioViewTest(StudioLmsComponentBaseTest):
    """
    HTML Components tests related to 'studio view' of component.
    """
    def setUp(self):
        """
        Call setUp in parent
        """
        super(StudioViewTest, self).setUp()

    def test_unit_studio_view(self):
        """
        Scenario: To test studio view of component from LMS
        Given that I am at the LMS side of the edX.
        And I open a component
        And I click on the 'View unit in Studio' button
        Then correct component should open.
        """
        section_name = 'Section :{}'.format(uuid4().hex)
        subsection_name = 'Subsection :{}'.format(uuid4().hex)
        # Add a section.
        self.studio_course_outline.add_section_with_name(section_name)
        # Add a subsection.
        self.studio_course_outline.add_subsection_with_name(subsection_name)
        # Add a unit ( In this case Word Cloud Advance component) and publish.
        self.studio_course_outline.click_add_unit_button()
        self.unit_container_page.wait_for_page()
        self.unit_container_page.add_word_cloud_component(True)
        # Get unique data locator id of the unit added).
        data_locator = get_data_locator(self.unit_container_page)
        self.lms_courseware.visit()
        self.lms_courseware.go_to_section(section_name, subsection_name)
        # View unit in the studio
        self.lms_courseware.view_unit_in_studio()
        self.unit_container_page.wait_for_page()
        # Correct unit component should open.
        self.assertEqual(
            get_data_locator(self.unit_container_page),
            data_locator, 'Correct component is opened'
        )
        # Remove the added section
        self.studio_course_outline.visit()
        self.studio_course_outline.delete_section()

@@ -525,34 +525,8 @@
 nfo,
- get_data_id_of_component,
  get
@@ -3421,1568 +3421,8 @@
 ()%0A%0A
-    def test_custom_js_display_advanced_component(self):%0A        %22%22%22%0A        Verifies that user can add Custom JavaScript Display and Grading%0A        component on Studio and LMS%0A        %22%22%22%0A        section_name = 'Section :%7B%7D'.format(uuid4().hex)%0A        self.studio_course_outline.add_section_with_name(section_name)%0A        self.assertIn(%0A            section_name,%0A            self.studio_course_outline.q(%0A                css='.incontext-editor-value').text%0A        )%0A%0A        subsection_name = 'Subsection :%7B%7D'.format(uuid4().hex)%0A        self.studio_course_outline.add_subsection_with_name(%0A            subsection_name%0A        )%0A        self.assertIn(%0A            subsection_name,%0A            self.studio_course_outline.q(%0A                css='.incontext-editor-value').text%0A        )%0A%0A        self.studio_course_outline.click_add_unit_button()%0A        self.unit_container_page.wait_for_page()%0A%0A        self.assertEqual(%0A            self.unit_container_page.add_custom_js_display_and_grading(),%0A            'Custom JavaScript Display and Grading'%0A        )%0A%0A        studio_custom_js = get_data_id_of_component(%0A            self.unit_container_page%0A        )%0A%0A        # Publish Unit%0A        self.studio_course_outline.publish()%0A        # View Live%0A        self.unit_container_page.view_live_version()%0A        self.assertEqual(%0A            studio_custom_js,%0A            get_data_id_of_component(self.lms_courseware)%0A        )%0A        # Remove the added section%0A        self.studio_course_outline.visit()%0A        self.studio_course_outline.delete_section()%0A%0A
 %0Acla

Remove flaky test_custom_js_display_advanced_component test