__author__ = 'Arseniy'
from model.contact import Contact
from selenium.webdriver.support.select import Select
import re


class ContactHelper:

    def __init__(self, app):
        self.app = app

    def load_home_page(self):
        wd = self.app.wd
        if len(wd.find_elements_by_link_text("Last name")) > 0:
            return
        wd.find_element_by_link_text("home").click()

    def select_by_index(self, index):
        wd = self.app.wd
        wd.find_elements_by_name("selected[]")[index].click()

    def select_by_id(self, id):
        wd = self.app.wd
        wd.find_element_by_css_selector("input[value='%s']" % id).click()

    def open_contact_to_edit_by_index(self, index):
        wd = self.app.wd
        self.load_home_page()
        # go to edit page
        wd.find_elements_by_css_selector('img[alt="Edit"]')[index].click()

    def open_contact_to_edit_by_id(self, id):
        wd = self.app.wd
        self.load_home_page()
        # go to edit page
        for row in wd.find_elements_by_name("entry"):
            cells = row.find_elements_by_tag_name("td")
            row_id = cells[0].find_element_by_name("selected[]").get_attribute("value")
            if id == row_id:
                cells[7].find_element_by_css_selector('img[alt="Edit"]').click()
                break

    def open_contact_view_by_index(self, index):
        wd = self.app.wd
        self.load_home_page()
        wd.find_elements_by_css_selector('img[alt="Details"]')[index].click()

    def open_contact_view_by_id(self, id):
        wd = self.app.wd
        self.load_home_page()
        for row in wd.find_elements_by_name("entry"):
            cells = row.find_elements_by_tag_name("td")
            row_id = cells[0].find_element_by_name("selected[]").get_attribute("value")
            if id == row_id:
                cells[6].find_element_by_css_selector('img[alt="Details"]').click()
                break

    def create(self, contact):
        wd = self.app.wd
        # go to new contact page
        wd.find_element_by_link_text("add new").click()
        # fill in names
        wd.find_element_by_name("firstname").click()
        wd.find_element_by_name("firstname").clear()
        wd.find_element_by_name("firstname").send_keys(contact.firstname)
        wd.find_element_by_name("middlename").click()
        wd.find_element_by_name("middlename").clear()
        wd.find_element_by_name("middlename").send_keys(contact.middlename)
        wd.find_element_by_name("lastname").click()
        wd.find_element_by_name("lastname").clear()
        wd.find_element_by_name("lastname").send_keys(contact.lastname)
        wd.find_element_by_name("nickname").click()
        wd.find_element_by_name("nickname").clear()
        wd.find_element_by_name("nickname").send_keys(contact.nickname)
        wd.find_element_by_name("title").click()
        wd.find_element_by_name("title").clear()
        wd.find_element_by_name("title").send_keys(contact.title)
        # fill in company data
        wd.find_element_by_name("company").click()
        wd.find_element_by_name("company").clear()
        wd.find_element_by_name("company").send_keys(contact.company)
        wd.find_element_by_name("address").click()
        wd.find_element_by_name("address").clear()
        wd.find_element_by_name("address").send_keys(contact.address)
        # fill in telephone numbers
        wd.find_element_by_name("home").click()
        wd.find_element_by_name("home").clear()
        wd.find_element_by_name("home").send_keys(contact.home_num)
        wd.find_element_by_name("mobile").click()
        wd.find_element_by_name("mobile").clear()
        wd.find_element_by_name("mobile").send_keys(contact.mobile_num)
        wd.find_element_by_name("work").click()
        wd.find_element_by_name("work").clear()
        wd.find_element_by_name("work").send_keys(contact.work_num)
        wd.find_element_by_name("fax").click()
        wd.find_element_by_name("fax").clear()
        wd.find_element_by_name("fax").send_keys(contact.fax_num)
        # fill in emails
        wd.find_element_by_name("email2").click()
        wd.find_element_by_name("email2").clear()
        wd.find_element_by_name("email2").send_keys(contact.email2)
        wd.find_element_by_name("email3").click()
        wd.find_element_by_name("email3").clear()
        wd.find_element_by_name("email3").send_keys(contact.email3)
        wd.find_element_by_name("homepage").click()
        wd.find_element_by_name("homepage").clear()
        wd.find_element_by_name("homepage").send_keys(contact.homepage)
        # fill in dates
        if not wd.find_element_by_xpath("//div[@id='content']/form/select[1]//option[17]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form/select[1]//option[17]").click()
        if not wd.find_element_by_xpath("//div[@id='content']/form/select[2]//option[4]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form/select[2]//option[4]").click()
        wd.find_element_by_name("byear").click()
        wd.find_element_by_name("byear").clear()
        wd.find_element_by_name("byear").send_keys(contact.byear)
        if not wd.find_element_by_xpath("//div[@id='content']/form/select[3]//option[16]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form/select[3]//option[16]").click()
        if not wd.find_element_by_xpath("//div[@id='content']/form/select[4]//option[12]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form/select[4]//option[12]").click()
        wd.find_element_by_name("ayear").click()
        wd.find_element_by_name("ayear").clear()
        wd.find_element_by_name("ayear").send_keys(contact.ayear)
        # fill in additional info
        wd.find_element_by_name("address2").click()
        wd.find_element_by_name("address2").clear()
        wd.find_element_by_name("address2").send_keys(contact.address2)
        wd.find_element_by_name("phone2").click()
        wd.find_element_by_name("phone2").clear()
        wd.find_element_by_name("phone2").send_keys(contact.phone2)
        wd.find_element_by_name("notes").click()
        wd.find_element_by_name("notes").clear()
        wd.find_element_by_name("notes").send_keys(contact.notes)
        # submit
        wd.find_element_by_xpath("//div[@id='content']/form/input[21]").click()
        self.load_home_page()
        self.contact_cache = None

    def edit_first(self, contact):
        self.edit_by_index(0, contact)

    def edit_by_index(self, index, contact):
        wd = self.app.wd
        self.load_home_page()
        self.open_contact_to_edit_by_index(index)
        # edit data
        self.edit(contact)
        # submit changes
        wd.find_element_by_xpath("//div[@id='content']/form[1]/input[22]").click()
        self.load_home_page()
        self.contact_cache = None

    def edit_by_id(self, id, contact):
        wd = self.app.wd
        self.load_home_page()
        self.open_contact_to_edit_by_id(id)
        # edit data
        self.edit(contact)
        # submit changes
        wd.find_element_by_xpath("//div[@id='content']/form[1]/input[22]").click()
        self.load_home_page()
        self.contact_cache = None

    def edit(self, contact):
        wd = self.app.wd
        if contact.firstname:
            wd.find_element_by_name("firstname").click()
            wd.find_element_by_name("firstname").clear()
            wd.find_element_by_name("firstname").send_keys(contact.firstname)
        if contact.middlename:
            wd.find_element_by_name("middlename").click()
            wd.find_element_by_name("middlename").clear()
            wd.find_element_by_name("middlename").send_keys(contact.middlename)
        if contact.lastname:
            wd.find_element_by_name("lastname").click()
            wd.find_element_by_name("lastname").clear()
            wd.find_element_by_name("lastname").send_keys(contact.lastname)
        if contact.nickname:
            wd.find_element_by_name("nickname").click()
            wd.find_element_by_name("nickname").clear()
            wd.find_element_by_name("nickname").send_keys(contact.nickname)
        if contact.company:
            wd.find_element_by_name("company").click()
            wd.find_element_by_name("company").clear()
            wd.find_element_by_name("company").send_keys(contact.company)
        if contact.title:
            wd.find_element_by_name("title").click()
            wd.find_element_by_name("title").clear()
            wd.find_element_by_name("title").send_keys(contact.title)
        if contact.address:
            wd.find_element_by_name("address").click()
            wd.find_element_by_name("address").clear()
            wd.find_element_by_name("address").send_keys(contact.address)
        if contact.home_num:
            wd.find_element_by_name("home").click()
            wd.find_element_by_name("home").clear()
            wd.find_element_by_name("home").send_keys(contact.home_num)
        if contact.mobile_num:
            wd.find_element_by_name("mobile").click()
            wd.find_element_by_name("mobile").clear()
            wd.find_element_by_name("mobile").send_keys(contact.mobile_num)
        if contact.work_num:
            wd.find_element_by_name("work").click()
            wd.find_element_by_name("work").clear()
            wd.find_element_by_name("work").send_keys(contact.work_num)
        if contact.fax_num:
            wd.find_element_by_name("fax").click()
            wd.find_element_by_name("fax").clear()
            wd.find_element_by_name("fax").send_keys(contact.fax_num)
        if contact.email:
            wd.find_element_by_name("email").click()
            wd.find_element_by_name("email").clear()
            wd.find_element_by_name("email").send_keys(contact.email)
        if contact.email2:
            wd.find_element_by_name("email2").click()
            wd.find_element_by_name("email2").clear()
            wd.find_element_by_name("email2").send_keys(contact.email2)
        if contact.email3:
            wd.find_element_by_name("email3").click()
            wd.find_element_by_name("email3").clear()
            wd.find_element_by_name("email3").send_keys(contact.email3)
        if contact.homepage:
            wd.find_element_by_name("homepage").click()
            wd.find_element_by_name("homepage").clear()
            wd.find_element_by_name("homepage").send_keys(contact.homepage)
        if not wd.find_element_by_xpath("//div[@id='content']/form[1]/select[1]//option[19]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form[1]/select[1]//option[19]").click()
        if not wd.find_element_by_xpath("//div[@id='content']/form[1]/select[2]//option[10]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form[1]/select[2]//option[10]").click()
        if contact.byear:
            wd.find_element_by_name("byear").click()
            wd.find_element_by_name("byear").clear()
            wd.find_element_by_name("byear").send_keys(contact.byear)
        if not wd.find_element_by_xpath("//div[@id='content']/form[1]/select[3]//option[7]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form[1]/select[3]//option[7]").click()
        if not wd.find_element_by_xpath("//div[@id='content']/form[1]/select[4]//option[9]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form[1]/select[4]//option[9]").click()
        if contact.ayear:
            wd.find_element_by_name("ayear").click()
            wd.find_element_by_name("ayear").clear()
            wd.find_element_by_name("ayear").send_keys(contact.ayear)
        if contact.address2:
            wd.find_element_by_name("address2").click()
            wd.find_element_by_name("address2").clear()
            wd.find_element_by_name("address2").send_keys(contact.address2)
        if contact.phone2:
            wd.find_element_by_name("phone2").click()
            wd.find_element_by_name("phone2").clear()
            wd.find_element_by_name("phone2").send_keys(contact.phone2)
        if contact.notes:
            wd.find_element_by_name("notes").click()
            wd.find_element_by_name("notes").clear()
            wd.find_element_by_name("notes").send_keys(contact.notes)

    def delete_first(self):
        self.delete_by_index(0)

    def delete_by_index(self, index):
        wd = self.app.wd
        self.select_by_index(index)
        # submit deletion
        wd.find_element_by_xpath("//div[@id='content']/form[2]/div[2]/input").click()
        # accept and close dialogue window
        wd.switch_to_alert().accept()
        self.load_home_page()
        self.contact_cache = None

    def delete_by_id(self, id):
        wd = self.app.wd
        self.select_by_id(id)
        # submit deletion
        wd.find_element_by_xpath("//div[@id='content']/form[2]/div[2]/input").click()
        # accept and close dialogue window
        wd.switch_to_alert().accept()
        self.load_home_page()
        self.contact_cache = None

    def add_to_group(self, contact, group):
        wd = self.app.wd
        self.load_home_page()
        self.select_by_id(contact.id)
        select = Select(wd.find_element_by_name("to_group"))
        select.select_by_visible_text(group.name.strip())
        wd.find_element_by_name("add").click()

    def remove_from_group(self, contact, group):
        wd = self.app.wd
        self.load_home_page()
        select = Select(wd.find_element_by_name("group"))
        select.select_by_visible_text(group.name.strip())
        self.select_by_id(contact.id)
        wd.find_element_by_name("remove").click()

    def count(self):
        wd = self.app.wd
        self.load_home_page()
        return len(wd.find_elements_by_name("selected[]"))

    contact_cache = None

    def get_contact_list(self):
        if self.contact_cache is None:
            wd = self.app.wd
            self.load_home_page()
            self.contact_cache = []
            for row in wd.find_elements_by_name("entry"):
                cells = row.find_elements_by_tag_name("td")
                last_name = cells[1].text
                first_name = cells[2].text
                id = cells[0].find_element_by_tag_name("input").get_attribute("value")
                all_emails = cells[4].text
                all_phones = cells[5].text
                self.contact_cache.append(Contact(id=id, firstname=first_name, lastname=last_name,
                                                  all_phones_from_home_page=all_phones,
                                                  all_emails_from_home_page=all_emails))
        return list(self.contact_cache)

    def get_contact_info_from_edit_page(self, index):
        wd = self.app.wd
        self.open_contact_to_edit_by_index(index)
        id = wd.find_element_by_name("id").get_attribute("value")
        firstname = wd.find_element_by_name("firstname").get_attribute("value")
        lastname = wd.find_element_by_name("lastname").get_attribute("value")

        home_num = wd.find_element_by_name("home").get_attribute("value")
        mobile_num = wd.find_element_by_name("mobile").get_attribute("value")
        work_num = wd.find_element_by_name("work").get_attribute("value")
        phone2 = wd.find_element_by_name("phone2").get_attribute("value")

        email = wd.find_element_by_name("email").get_attribute("value")
        email2 = wd.find_element_by_name("email2").get_attribute("value")
        email3 = wd.find_element_by_name("email3").get_attribute("value")
        return Contact(id=id, firstname=firstname, lastname=lastname,
                       home_num=home_num, mobile_num=mobile_num,
                       work_num=work_num, phone2=phone2,
                       email=email, email2=email2, email3=email3)

    def get_contact_from_view_page(self, index):
        wd = self.app.wd
        self.open_contact_view_by_index(index)
        text = wd.find_element_by_id("content").text
        home_num = re.search("H: (.*)", text).group(1)
        mobile_num = re.search("M: (.*)", text).group(1)
        work_num = re.search("W: (.*)", text).group(1)
        phone2 = re.search("P: (.*)", text).group(1)
        return Contact(home_num=home_num, mobile_num=mobile_num,
                       work_num=work_num, phone2=phone2)

    def get_text_from_view_page(self, index):
        wd = self.app.wd
        self.open_contact_view_by_index(index)
        return wd.find_element_by_id("content").text

    def get_text_from_view_page_by_id(self, id):
        wd = self.app.wd
        self.open_contact_view_by_id(id)
        return wd.find_element_by_id("content").text



__author__ = 'Arseniy'
from model.contact import Contact
from selenium.webdriver.support.select import Select
import re


class ContactHelper:

    def __init__(self, app):
        self.app = app

    def load_home_page(self):
        wd = self.app.wd
        if len(wd.find_elements_by_link_text("Last name")) > 0:
            return
        wd.find_element_by_link_text("home").click()

    def select_by_index(self, index):
        wd = self.app.wd
        wd.find_elements_by_name("selected[]")[index].click()

    def select_by_id(self, id):
        wd = self.app.wd
        wd.find_element_by_css_selector("input[value='%s']" % id).click()

    def open_contact_to_edit_by_index(self, index):
        wd = self.app.wd
        self.load_home_page()
        # go to edit page
        wd.find_elements_by_css_selector('img[alt="Edit"]')[index].click()

    def open_contact_to_edit_by_id(self, id):
        wd = self.app.wd
        self.load_home_page()
        # go to edit page
        for row in wd.find_elements_by_name("entry"):
            cells = row.find_elements_by_tag_name("td")
            row_id = cells[0].find_element_by_name("selected[]").get_attribute("value")
            if id == row_id:
                cells[7].find_element_by_css_selector('img[alt="Edit"]').click()
                break

    def open_contact_view_by_index(self, index):
        wd = self.app.wd
        self.load_home_page()
        wd.find_elements_by_css_selector('img[alt="Details"]')[index].click()

    def open_contact_view_by_id(self, id):
        wd = self.app.wd
        self.load_home_page()
        for row in wd.find_elements_by_name("entry"):
            cells = row.find_elements_by_tag_name("td")
            row_id = cells[0].find_element_by_name("selected[]").get_attribute("value")
            if id == row_id:
                cells[6].find_element_by_css_selector('img[alt="Details"]').click()
                break

    def create(self, contact):
        wd = self.app.wd
        # go to new contact page
        wd.find_element_by_link_text("add new").click()
        # fill in names
        wd.find_element_by_name("firstname").click()
        wd.find_element_by_name("firstname").clear()
        wd.find_element_by_name("firstname").send_keys(contact.firstname)
        wd.find_element_by_name("middlename").click()
        wd.find_element_by_name("middlename").clear()
        wd.find_element_by_name("middlename").send_keys(contact.middlename)
        wd.find_element_by_name("lastname").click()
        wd.find_element_by_name("lastname").clear()
        wd.find_element_by_name("lastname").send_keys(contact.lastname)
        wd.find_element_by_name("nickname").click()
        wd.find_element_by_name("nickname").clear()
        wd.find_element_by_name("nickname").send_keys(contact.nickname)
        wd.find_element_by_name("title").click()
        wd.find_element_by_name("title").clear()
        wd.find_element_by_name("title").send_keys(contact.title)
        # fill in company data
        wd.find_element_by_name("company").click()
        wd.find_element_by_name("company").clear()
        wd.find_element_by_name("company").send_keys(contact.company)
        wd.find_element_by_name("address").click()
        wd.find_element_by_name("address").clear()
        wd.find_element_by_name("address").send_keys(contact.address)
        # fill in telephone numbers
        wd.find_element_by_name("home").click()
        wd.find_element_by_name("home").clear()
        wd.find_element_by_name("home").send_keys(contact.home_num)
        wd.find_element_by_name("mobile").click()
        wd.find_element_by_name("mobile").clear()
        wd.find_element_by_name("mobile").send_keys(contact.mobile_num)
        wd.find_element_by_name("work").click()
        wd.find_element_by_name("work").clear()
        wd.find_element_by_name("work").send_keys(contact.work_num)
        wd.find_element_by_name("fax").click()
        wd.find_element_by_name("fax").clear()
        wd.find_element_by_name("fax").send_keys(contact.fax_num)
        # fill in emails
        wd.find_element_by_name("email2").click()
        wd.find_element_by_name("email2").clear()
        wd.find_element_by_name("email2").send_keys(contact.email2)
        wd.find_element_by_name("email3").click()
        wd.find_element_by_name("email3").clear()
        wd.find_element_by_name("email3").send_keys(contact.email3)
        wd.find_element_by_name("homepage").click()
        wd.find_element_by_name("homepage").clear()
        wd.find_element_by_name("homepage").send_keys(contact.homepage)
        # fill in dates
        if not wd.find_element_by_xpath("//div[@id='content']/form/select[1]//option[17]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form/select[1]//option[17]").click()
        if not wd.find_element_by_xpath("//div[@id='content']/form/select[2]//option[4]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form/select[2]//option[4]").click()
        wd.find_element_by_name("byear").click()
        wd.find_element_by_name("byear").clear()
        wd.find_element_by_name("byear").send_keys(contact.byear)
        if not wd.find_element_by_xpath("//div[@id='content']/form/select[3]//option[16]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form/select[3]//option[16]").click()
        if not wd.find_element_by_xpath("//div[@id='content']/form/select[4]//option[12]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form/select[4]//option[12]").click()
        wd.find_element_by_name("ayear").click()
        wd.find_element_by_name("ayear").clear()
        wd.find_element_by_name("ayear").send_keys(contact.ayear)
        # fill in additional info
        wd.find_element_by_name("address2").click()
        wd.find_element_by_name("address2").clear()
        wd.find_element_by_name("address2").send_keys(contact.address2)
        wd.find_element_by_name("phone2").click()
        wd.find_element_by_name("phone2").clear()
        wd.find_element_by_name("phone2").send_keys(contact.phone2)
        wd.find_element_by_name("notes").click()
        wd.find_element_by_name("notes").clear()
        wd.find_element_by_name("notes").send_keys(contact.notes)
        # submit
        wd.find_element_by_xpath("//div[@id='content']/form/input[21]").click()
        self.load_home_page()
        self.contact_cache = None

    def edit_first(self, contact):
        self.edit_by_index(0, contact)

    def edit_by_index(self, index, contact):
        wd = self.app.wd
        self.load_home_page()
        self.open_contact_to_edit_by_index(index)
        # edit data
        self.edit(contact)
        # submit changes
        wd.find_element_by_xpath("//div[@id='content']/form[1]/input[22]").click()
        self.load_home_page()
        self.contact_cache = None

    def edit_by_id(self, id, contact):
        wd = self.app.wd
        self.load_home_page()
        self.open_contact_to_edit_by_id(id)
        # edit data
        self.edit(contact)
        # submit changes
        wd.find_element_by_xpath("//div[@id='content']/form[1]/input[22]").click()
        self.load_home_page()
        self.contact_cache = None

    def edit(self, contact):
        wd = self.app.wd
        if contact.firstname:
            wd.find_element_by_name("firstname").click()
            wd.find_element_by_name("firstname").clear()
            wd.find_element_by_name("firstname").send_keys(contact.firstname)
        if contact.middlename:
            wd.find_element_by_name("middlename").click()
            wd.find_element_by_name("middlename").clear()
            wd.find_element_by_name("middlename").send_keys(contact.middlename)
        if contact.lastname:
            wd.find_element_by_name("lastname").click()
            wd.find_element_by_name("lastname").clear()
            wd.find_element_by_name("lastname").send_keys(contact.lastname)
        if contact.nickname:
            wd.find_element_by_name("nickname").click()
            wd.find_element_by_name("nickname").clear()
            wd.find_element_by_name("nickname").send_keys(contact.nickname)
        if contact.company:
            wd.find_element_by_name("company").click()
            wd.find_element_by_name("company").clear()
            wd.find_element_by_name("company").send_keys(contact.company)
        if contact.title:
            wd.find_element_by_name("title").click()
            wd.find_element_by_name("title").clear()
            wd.find_element_by_name("title").send_keys(contact.title)
        if contact.address:
            wd.find_element_by_name("address").click()
            wd.find_element_by_name("address").clear()
            wd.find_element_by_name("address").send_keys(contact.address)
        if contact.home_num:
            wd.find_element_by_name("home").click()
            wd.find_element_by_name("home").clear()
            wd.find_element_by_name("home").send_keys(contact.home_num)
        if contact.mobile_num:
            wd.find_element_by_name("mobile").click()
            wd.find_element_by_name("mobile").clear()
            wd.find_element_by_name("mobile").send_keys(contact.mobile_num)
        if contact.work_num:
            wd.find_element_by_name("work").click()
            wd.find_element_by_name("work").clear()
            wd.find_element_by_name("work").send_keys(contact.work_num)
        if contact.fax_num:
            wd.find_element_by_name("fax").click()
            wd.find_element_by_name("fax").clear()
            wd.find_element_by_name("fax").send_keys(contact.fax_num)
        if contact.email:
            wd.find_element_by_name("email").click()
            wd.find_element_by_name("email").clear()
            wd.find_element_by_name("email").send_keys(contact.email)
        if contact.email2:
            wd.find_element_by_name("email2").click()
            wd.find_element_by_name("email2").clear()
            wd.find_element_by_name("email2").send_keys(contact.email2)
        if contact.email3:
            wd.find_element_by_name("email3").click()
            wd.find_element_by_name("email3").clear()
            wd.find_element_by_name("email3").send_keys(contact.email3)
        if contact.homepage:
            wd.find_element_by_name("homepage").click()
            wd.find_element_by_name("homepage").clear()
            wd.find_element_by_name("homepage").send_keys(contact.homepage)
        if not wd.find_element_by_xpath("//div[@id='content']/form[1]/select[1]//option[19]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form[1]/select[1]//option[19]").click()
        if not wd.find_element_by_xpath("//div[@id='content']/form[1]/select[2]//option[10]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form[1]/select[2]//option[10]").click()
        if contact.byear:
            wd.find_element_by_name("byear").click()
            wd.find_element_by_name("byear").clear()
            wd.find_element_by_name("byear").send_keys(contact.byear)
        if not wd.find_element_by_xpath("//div[@id='content']/form[1]/select[3]//option[7]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form[1]/select[3]//option[7]").click()
        if not wd.find_element_by_xpath("//div[@id='content']/form[1]/select[4]//option[9]").is_selected():
            wd.find_element_by_xpath("//div[@id='content']/form[1]/select[4]//option[9]").click()
        if contact.ayear:
            wd.find_element_by_name("ayear").click()
            wd.find_element_by_name("ayear").clear()
            wd.find_element_by_name("ayear").send_keys(contact.ayear)
        if contact.address2:
            wd.find_element_by_name("address2").click()
            wd.find_element_by_name("address2").clear()
            wd.find_element_by_name("address2").send_keys(contact.address2)
        if contact.phone2:
            wd.find_element_by_name("phone2").click()
            wd.find_element_by_name("phone2").clear()
            wd.find_element_by_name("phone2").send_keys(contact.phone2)
        if contact.notes:
            wd.find_element_by_name("notes").click()
            wd.find_element_by_name("notes").clear()
            wd.find_element_by_name("notes").send_keys(contact.notes)

    def delete_first(self):
        self.delete_by_index(0)

    def delete_by_index(self, index):
        wd = self.app.wd
        self.select_by_index(index)
        # submit deletion
        wd.find_element_by_xpath("//div[@id='content']/form[2]/div[2]/input").click()
        # accept and close dialogue window
        wd.switch_to_alert().accept()
        self.load_home_page()
        self.contact_cache = None

    def delete_by_id(self, id):
        wd = self.app.wd
        self.select_by_id(id)
        # submit deletion
        wd.find_element_by_xpath("//div[@id='content']/form[2]/div[2]/input").click()
        # accept and close dialogue window
        wd.switch_to_alert().accept()
        self.load_home_page()
        self.contact_cache = None

    def add_to_group(self, contact, group):
        wd = self.app.wd
        self.load_home_page()
        self.select_by_id(contact.id)
        select = Select(wd.find_element_by_name("to_group"))
        select.select_by_visible_text(group.name.strip())
        wd.find_element_by_name("add").click()

    def remove_from_group(self, contact, group):
        wd = self.app.wd
        self.load_home_page()
        select = Select(wd.find_element_by_name("group"))
        select.select_by_visible_text(group.name.strip())
        self.select_by_id(contact.id)
        wd.find_element_by_name("remove").click()

    def count(self):
        wd = self.app.wd
        self.load_home_page()
        return len(wd.find_elements_by_name("selected[]"))

    contact_cache = None

    def get_contact_list(self):
        if self.contact_cache is None:
            wd = self.app.wd
            self.load_home_page()
            self.contact_cache = []
            for row in wd.find_elements_by_name("entry"):
                cells = row.find_elements_by_tag_name("td")
                last_name = cells[1].text
                first_name = cells[2].text
                id = cells[0].find_element_by_tag_name("input").get_attribute("value")
                all_emails = cells[4].text
                all_phones = cells[5].text
                self.contact_cache.append(Contact(id=id, firstname=first_name, lastname=last_name,
                                                  all_phones_from_home_page=all_phones,
                                                  all_emails_from_home_page=all_emails))
        return list(self.contact_cache)

    def get_contact_info_from_edit_page(self, index):
        wd = self.app.wd
        self.open_contact_to_edit_by_index(index)
        id = wd.find_element_by_name("id").get_attribute("value")
        firstname = wd.find_element_by_name("firstname").get_attribute("value")
        lastname = wd.find_element_by_name("lastname").get_attribute("value")

        home_num = wd.find_element_by_name("home").get_attribute("value")
        mobile_num = wd.find_element_by_name("mobile").get_attribute("value")
        work_num = wd.find_element_by_name("work").get_attribute("value")
        phone2 = wd.find_element_by_name("phone2").get_attribute("value")

        email = wd.find_element_by_name("email").get_attribute("value")
        email2 = wd.find_element_by_name("email2").get_attribute("value")
        email3 = wd.find_element_by_name("email3").get_attribute("value")
        return Contact(id=id, firstname=firstname, lastname=lastname,
                       home_num=home_num, mobile_num=mobile_num,
                       work_num=work_num, phone2=phone2,
                       email=email, email2=email2, email3=email3)

    def get_contact_from_view_page(self, index):
        wd = self.app.wd
        self.open_contact_view_by_index(index)
        text = wd.find_element_by_id("content").text
        home_num = re.search("H: (.*)", text).group(1)
        mobile_num = re.search("M: (.*)", text).group(1)
        work_num = re.search("W: (.*)", text).group(1)
        phone2 = re.search("P: (.*)", text).group(1)
        return Contact(home_num=home_num, mobile_num=mobile_num,
                       work_num=work_num, phone2=phone2)

    def get_text_from_view_page(self, index):
        wd = self.app.wd
        self.open_contact_view_by_index(index)
        return wd.find_element_by_id("content").text

    def get_text_from_view_page_by_id(self, id):
        wd = self.app.wd
        self.open_contact_view_by_id(id)
        return wd.find_element_by_id("content").text



# go to edit page # go to edit page # go to new contact page # fill in names # fill in company data # fill in telephone numbers # fill in emails # fill in dates # fill in additional info # submit # edit data # submit changes # edit data # submit changes # submit deletion # accept and close dialogue window # submit deletion # accept and close dialogue window