#
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2005 Shuttleworth Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
"""
Functional tests for schooltool.intervention

"""

import unittest
import os

from schooltool.app.browser.ftests import setup
from schooltool.testing.functional import collect_ftests
from schooltool.testing.functional import ZCMLLayer
from schooltool.skin.skin import ISchoolToolSkin
from schooltool.basicperson.browser.skin import IBasicPersonLayer


class IInterventionFtestingSkin(IBasicPersonLayer, ISchoolToolSkin):
    """Skin for Intervention functional tests."""


dir = os.path.abspath(os.path.dirname(__file__))
filename = os.path.join(dir, 'ftesting.zcml')

intervention_functional_layer = ZCMLLayer(filename,
                                 __name__,
                                 'intervention_functional_layer')


def addPerson(first_name, last_name, username, password, groups=None, browser=None):
    """Add a person.

    If username is not specified, it will be taken to be name.lower().

    If password is not specified, it will be taken to be username + 'pwd'.
    """
    if browser is None:
        browser = setup.logInManager()
    browser.getLink('Manage').click()
    browser.getLink('Persons').click()
    browser.getLink('New Person').click()
    browser.getControl('First name').value = first_name
    browser.getControl('Last name').value = last_name
    browser.getControl('Username').value = username
    browser.getControl('Password').value = password
    browser.getControl('Confirm').value = password
    browser.getControl('Add').click()

    if groups:
        browser.open('http://localhost/persons/%s' % username)
        browser.getLink('edit groups').click()
        for group in groups:
            browser.getControl(name='add_item.%s' % group).value = True
        browser.getControl('Add').click()
        browser.getControl('OK').click()

    fillInContactInfo(username, browser=browser,
                      email='%s@example.com' % username)
    browser.open('http://localhost/persons')


def fillInContactInfo(username, browser=None, email=None, language=None):
    if browser is None:
        browser = setup.logInManager()
    browser.open('http://localhost/persons/%s' % username)
    # TODO: other needed fields
    contact_fields = {
        'form.widgets.email': email,
        'form.widgets.language': language,
        }
    browser.getLink('Contacts').click()
    browser.getLink('Edit').click()
    for name, value in contact_fields.items():
        if value is not None:
            browser.getControl(name=name).value = value
    browser.getControl('Apply').click()


def addCourseSectionMembers(course, section, teachers, students):
    setup.addCourse(course, '2005-2006')
    setup.addSection(course, '2005-2006', 'Spring', section)
    manager = setup.logInManager()
    manager.getLink('Manage').click()
    manager.getLink('School Years').click()
    manager.getLink('2005-2006').click()
    manager.getLink('Spring').click()
    manager.getLink('Sections').click()
    manager.getLink(section).click()
    manager.getLink('edit instructors').click()
    for teacher in teachers:
        manager.getControl(teacher).click()
    manager.getControl('Add').click()
    manager.getControl('OK').click()
    manager.getLink('edit individuals').click()
    for student in students:
        manager.getControl(student).click()
    manager.getControl('Add').click()
    manager.getControl('OK').click()


def test_suite():
    return collect_ftests(layer=intervention_functional_layer)


if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')
