from selenium.webdriver.common.keys import Keys
from .base import FunctionalTest

class ItemValidationTest( FunctionalTest ) :

    def get_error_element( self ) :
        return self.browser.find_element_by_css_selector( '.has-error' )

    def test_cannot_add_empty_list_items(self):
        # Sanyam goes to the home page and accidentally tries to submit
        # an empty list item. He hits Enter on the empty input box
        self.browser.get( self.live_server_url )
        self.get_item_input_box( ).send_keys( Keys.ENTER )

        # The browser intercepts the request, and does not load the
        # list page
        self.wait_for( lambda: self.browser.find_elements_by_css_selector(
            '#id_text:invalid'
        ) )

        # He starts typing some text for the new item and the error disappears
        self.get_item_input_box( ).send_keys( 'Buy milk' )
        self.wait_for( lambda: self.browser.find_elements_by_css_selector(
            '#id_text:valid'
        ) )

        # And he can submit it successfully
        self.get_item_input_box( ).send_keys( Keys.ENTER )
        self.wait_for_row_in_list_table( '1: Buy milk' )

        # Perversely, he now decides to submit a second blank list item
        self.get_item_input_box( ).send_keys( Keys.ENTER )

        # Again, the browser will not comply
        self.wait_for_row_in_list_table( '1: Buy milk' )
        self.wait_for( lambda: self.browser.find_elements_by_css_selector(
            '#id_text:invalid'
        ) )

        # And he can correct it by filling some text in
        self.get_item_input_box().send_keys( 'Make tea' )
        self.wait_for( lambda: self.browser.find_elements_by_css_selector(
            '#id_text:valid'
        ) )
        self.get_item_input_box( ).send_keys( Keys.ENTER )
        self.wait_for_row_in_list_table( '1: Buy milk' )
        self.wait_for_row_in_list_table( '2: Make tea' )

    def test_cannot_add_duplicate_items( self ) :
        # Sanyam goes to the hmoe page and starts a new list
        self.browser.get( self.live_server_url )
        self.get_item_input_box( ).send_keys( 'Buy wellies' )
        self.get_item_input_box( ).send_keys( Keys.ENTER )
        self.wait_for_row_in_list_table( '1: Buy wellies' )

        # He accidentally tries to enter a duplicatte item
        self.get_item_input_box( ).send_keys( 'Buy wellies' )
        self.get_item_input_box( ).send_keys( Keys.ENTER )

        # He sees a helpful error message
        self.wait_for( lambda: self.assertEqual(
            self.get_error_element( ).text ,
            "You've already got this in your list"
        ) )

    def test_error_messages_are_cleared_on_input( self ) :
        # Sanyam starts a list and causes a validation error:
        self.browser.get( self.live_server_url )
        self.get_item_input_box( ).send_keys( 'Banter too thick' )
        self.get_item_input_box( ).send_keys( Keys.ENTER )
        self.wait_for_row_in_list_table( '1: Banter too thick' )
        self.get_item_input_box( ).send_keys( 'Banter too thick' )
        self.get_item_input_box( ).send_keys( Keys.ENTER )
        self.wait_for( lambda: self.assertTrue(
            self.get_error_element( ).is_displayed( )
        ) )

        # He starts typing in the input box to clear the error
        self.get_item_input_box( ).send_keys( 'a' )

        # He is pleased to see that the error message disappears
        self.wait_for( lambda: self.assertFalse(
            self.get_error_element( ).is_displayed( )
        ) )
