
from model.contact import Contact
import re
from fixture.group import GroupHelper
__author__ = 'Max'

class ContactHelper:

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

    def change_field_value(self,field_name,text):
        wd = self.app.wd
        if text is None:
            wd.find_element_by_name(field_name).click()
            wd.find_element_by_name(field_name).clear()
            wd.find_element_by_name(field_name).send_keys()

    contact_cache = None

    def get_contact_list(self):
        if self.contact_cache is None:
            wd = self.app.wd
            self.contact_cache = []
            for row in wd.find_elements_by_name('entry'):
                cells = row.find_elements_by_tag_name('td')
                firstname = cells[2].text
                lastname = cells[1].text
                id = cells[0].find_element_by_tag_name('input').get_attribute('value')
                all_phones = cells[5].text #razer strings with phones
                self.contact_cache.append(Contact(firstname=firstname,lastname=lastname,
                                                  id=id, all_phones_from_home_page=all_phones))
        return list(self.contact_cache)

    def open_contact_to_edit_by_index(self, index):
        wd = self.app.wd
        if (wd.get!=("http://localhost/addressbook/")):
            wd.find_element_by_xpath(".//*[@id='nav']/ul/li[1]/a").click()
        else:
            pass
        row = wd.find_elements_by_name('entry')[index]
        cell = row.find_elements_by_tag_name('td')[7]
        cell.find_element_by_tag_name('a').click()

    def open_contact_view_by_index(self, index):
        wd = self.app.wd
        if (wd.get!=("http://localhost/addressbook/")):
            wd.find_element_by_xpath(".//*[@id='nav']/ul/li[1]/a").click()
        else:
            pass
        row = wd.find_elements_by_name('entry')[index]
        cell = row.find_elements_by_tag_name('td')[6]
        cell.find_element_by_tag_name('a').click()

    def get_contact_info_from_edit_page(self, index):
        wd = self.app.wd
        self.open_contact_to_edit_by_index(index)
        firstname = wd.find_element_by_name('firstname').get_attribute('value')
        lastname = wd.find_element_by_name('lastname').get_attribute('value')
        id = wd.find_element_by_name('id').get_attribute('value')
        homephone = wd.find_element_by_name('home').get_attribute('value')
        workphone = wd.find_element_by_name('work').get_attribute('value')
        secondaryphone = wd.find_element_by_name('phone2').get_attribute('value')
        mobilephone = wd.find_element_by_name('mobile').get_attribute('value')
        return Contact(firstname=firstname,lastname=lastname,id=id,homephone=homephone,workphone=workphone,
                       secondaryphone=secondaryphone,mobilephone=mobilephone)

    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
        homephone = re.search("H: (.*)", text).group(1)
        workphone = re.search("W: (.*)", text).group(1)
        secondaryphone = re.search("P: (.*)", text).group(1)
        mobilephone = re.search("M: (.*)", text).group(1)
        return Contact(homephone=homephone, workphone=workphone, secondaryphone=secondaryphone, mobilephone=mobilephone)
