from model.project import Project


class ProjectHelper:

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

    def open_page_manage_projects(self):
        wd = self.app.wd
        wd.find_element_by_link_text("Manage").click()
        wd.find_element_by_link_text("Manage Projects").click()

    def create(self, project):
        wd = self.app.wd
        self.open_page_manage_projects()
        wd.find_element_by_xpath("//table[3]/tbody/tr[1]/td/form/input[2]").click()
        self.fill_add_rpoject_form(project)
        wd.find_element_by_css_selector("input.button").click()
        wd.find_element_by_link_text("Proceed").click()
        self.project_cache = None

    def fill_add_rpoject_form(self, project):
        wd = self.app.wd
        self.change_field_value("name", project.name)
        self.change_field_value("description", project.description)

    def change_field_value(self, field_name, text):
        wd = self.app.wd
        if text is not 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(text)

    def delete(self, project):
        



    def get_project_list(self):
        wd = self.app.wd
        self.open_page_manage_projects()
        projects_list = wd.find_elements_by_xpath("//table[3]/tbody/tr")[2:]
        return [Project(name=p.find_element_by_xpath("td[1]").text) for p in projects_list]


    def count(self):
        wd = self.app.wd
        self.open_page_manage_projects()
        return len(wd.find_elements_by_css_selector(".fa.fa-check.fa-lg"))

    project_cache = None


wd = self.app.wd
        self.open_page_manage_projects()
        wd.find_element_by_link_text(project.name).click()
        wd.find_element_by_css_selector("form > input.button").click()
        wd.find_element_by_css_selector("input.button").click()