################################################################################
# taken from http://harry.pythonanywhere.com/tutorial/3/ (with modifications)
################################################################################

from functional_tests import FunctionalTest, ROOT
from selenium.webdriver.common.keys import Keys

class TestRegistration(FunctionalTest):

    season_name = 'Winter 2011-2012'
    season_abbreviation = 'W11'
    season_message = 'Welcome! Blah blah season information'

    division_1_name = 'Woo'
    division_2_name = 'Hoo'

    def _setup_season_via_admin(self):
        '''Logs an administrator into the admin UI and creates a new season.'''

        # Gertrude navigates to the admin page
        self.browser.get(ROOT + '/admin/')

        # She sees the familiar 'Django administration' heading
        self.assertIn('Django administration', self.browser.find_element_by_tag_name('body').text)

        # She types in her username and passwords and hits return
        self.browser.find_element_by_name('username').send_keys('admin')
        password_field = self.browser.find_element_by_name('password')
        password_field.send_keys('adm1n')
        password_field.send_keys(Keys.RETURN)

        # She now sees a link that says "Seasons" and clicks it
        self.browser.find_element_by_link_text('Seasons').click()

        # She is taken to the seasons listing page and clicks to add a new season
        self.browser.find_element_by_link_text('Add season').click()

        # She sees some input fields, fills them in, and saves them
        self.browser.find_element_by_id('id_name').send_keys(self.season_name)
        self.browser.find_element_by_id('id_abbreviation').send_keys(self.season_abbreviation)
        self.browser.find_element_by_id('id_message').send_keys(self.season_message)
        self.browser.find_element_by_css_selector("input[value='Save']").click()

        # She is returned to the "Seasons" listing, where she can see her
        # new season
        self.assertEquals(len(self.browser.find_elements_by_link_text(self.season_abbreviation)), 1)

    def _setup_division_via_admin(self, name):
        ''' Creates a division. The admin user must already be logged in.'''

        # Gertrude navigates to the admin page
        self.browser.get(ROOT + '/admin/')

        # She sees a link that says "Divisions" and clicks it
        self.browser.find_element_by_link_text('Divisions').click()

        # She is taken to the divisions listing page and clicks to add a new division
        self.browser.find_element_by_link_text('Add division').click()

        # She sees some input fields, fills them in, and saves them
        self.browser.find_element_by_id('id_name').send_keys(name)
        self.browser.find_element_by_id('id_abbreviation').send_keys(name[0])
        self.browser.find_element_by_css_selector("input[value='Save']").click()

        # She is returned to the "Divisions" listing, where she can see her
        # new division
        self.assertEquals(len(self.browser.find_elements_by_link_text(name)), 1)

    def test_new_user_registration(self):

        self._setup_season_via_admin()
        self._setup_division_via_admin(self.division_1_name)
        self._setup_division_via_admin(self.division_2_name)

        # Hortense navigates to the registration page
        self.browser.get(ROOT + '/adelie/register')
        self.assertEquals(self.browser.find_element_by_tag_name('h3').text,
                          'Registration for {0}'.format(self.season_name))

        # Since she is not logged in, she sees the account login/creation link
        self.browser.find_element_by_link_text('Create new account').click()

        # She creates a new account for herself
        self.browser.find_element_by_id('first_name_input').send_keys('Hortense')
        self.browser.find_element_by_id('last_name_input').send_keys('New')
        self.browser.find_element_by_id('email_input').send_keys('hnew@example.com')
        self.browser.find_element_by_id('birthDate_month_input').send_keys('1')
        self.browser.find_element_by_id('birthDate_day_input').send_keys('1')
        self.browser.find_element_by_id('birthDate_year_input').send_keys('1970')
        self.browser.find_element_by_id('USAHockeyNumber').send_keys('25765673NEW')
        self.browser.find_element_by_id('cellNumber_input').send_keys('510-555-1212')
        self.browser.find_element_by_id('altNumber_input').send_keys('')
        self.browser.find_element_by_id('username_input').send_keys('hortensenew')
        self.browser.find_element_by_id('password_input').send_keys('passnew')
        self.browser.find_element_by_id('password_confirm_input').send_keys('passnew')
        self.browser.find_element_by_id('save_new_user').click()

        # Now she fills out all of the registration information and submits
        self.assertEquals(self.brower.find_element_by_id('season_message').text, self.season_message)
        # Hortense has never registered previously, so she does not have a previous division
        selectObj = Select(self.browser.find_element_by_id('desired_division_select'))
        selectObj.select_by_visible_text(division_2_name)
        self.browser.find_element_by_id('referrer_input').send_keys('Isabella Existing')
        self.browser.find_element_by_id('save_registration').click()

        # Hortense sees the registration confirmation page and is excited for the next season
        self.assertIn('Thank you for registering!', self.browser.find_element_by_tag_name('body'))

        raise Exception('test not finished')

    def test_existing_user_registration(self):

        raise Exception('test not implemented')
